简体   繁体   English

形状不匹配Numpy

[英]Shape Mismatch Numpy

I am continously getting the error: 我不断得到错误:

"(shapes (10, 1), (10,) mismatch)" “(形状(10,1),(10,)不匹配)”

when doing a NumPy operation and I am somewhat confused. 在进行NumPy操作时我感到有些困惑。

Wouldn't (10,1) and (10,) be identical shapes? (10,1)和(10)不是相同的形状吗? And if for whatever reason this is not valid, is there a way to convert (10,1) to (10,)? 如果由于某种原因这是无效的,有没有办法将(10,1)转换为(10,)? I cannot seem to find it in the NumPy doucmentation. 我似乎无法在NumPy doucmentation中找到它。

Thanks 谢谢

The difference between (10,1) and (10,) is the dimensions of your vector. (10,1)(10,)之间的差异是矢量的维数。 The first ( (10,1) ) is a two-dimensional array while the latter ( (10,) ) is one-dimensional: 第一个( (10,1) )是二维数组,而后者( (10,) )是一维的:

>>> import numpy as np
>>> x = np.zeros((10,1))
>>> x
array([[ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.]])
>>> x.shape = (10,)
>>> x
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

So long as the total number of elements remain the same, you can re-shape your array however you'd like via numpy.ndarray.shape 只要元素的总数保持不变,您就可以通过numpy.ndarray.shape重新塑造数组。

In numpy , (10, 1), (10,) are not the same at all: numpy ,( numpy ),(10,)根本相同:

  • (10, 1) is a two dimensional array, with a single column. (10,1)是二维阵列,具有单列。

  • (10, ) is a one dimensional array (10,)是一维数组

If you have an array a , and print out len(a.shape), you'll see the difference. 如果你有一个数组a ,并打印出len(a.shape),你会看到差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM