Confusion in size of a numpy array

时光总嘲笑我的痴心妄想 提交于 2019-11-29 17:15:06

No, a numpy.ndarray with shape (1, 3) would look like:

np.array([[1,2,3]])

Think about how the shape corresponds to indexing:

arr[0, ...]  #First row

I still have three more options, namely:

arr[0,0]
arr[0,1]
arr[0,2]

Try doing that with a 1 dimensional array

You should resist the urge to think of numpy arrays as having rows and columns, but instead consider them as having dimensions and shape. This is an important point which differentiates np.array and np.matrix:

x = np.array([1, 2, 3])
print(x.ndim, x.shape)  # 1 (3,)

y = np.matrix([1, 2, 3])
print(y.ndim, y.shape)  # 2 (1, 3)

An n-D array can only use n integer(s) to represent its shape. Therefore, a 1-D array only uses 1 integer to specify its shape.

In practice, combining calculations between 1-D and 2-D arrays is not a problem for numpy, and syntactically clean since @ matrix operation was introduced in Python 3.5. Therefore, there is rarely a need to resort to np.matrix in order to satisfy the urge to see expected row and column counts.

In the rare instances where 2 dimensions are required, you can still use numpy.array with some manipulation:

a = np.array([1, 2, 3])[:, None]  # equivalent to np.array([[1], [2], [3]])
print(a.ndim, a.shape)  # 2 (3, 1)

b = np.array([[1, 2, 3]])  # equivalent to np.array([1, 2, 3])[:, None].T
print(b.ndim, b.shape)  # 2 (1, 3)

I think you meant ndarray.shape. In that case, there's no need for confusion. Quoting the documentation from ndarray.shape:

Tuple of array dimensions.

ndarray.shape simply returns a shape tuple.

In [21]: a.shape
Out[21]: (3,)

This simply means that a is an 1D array with 3 entries.

If the shape tuple returns it as (1,3) then a would become a 2D array. For that you need to use:

In [23]: a = a[np.newaxis, :]

In [24]: a.shape
Out[24]: (1, 3)

Since array b is 2D, the shape tuple has two entries.

In [22]: b.shape
Out[22]: (3, 4)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!