numpy array that is (n,1) and (n,)

拟墨画扇 提交于 2019-11-30 13:04:14

This is a 1D array:

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

This array is a 2D but there is only one element in the first dimension:

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

Transposing gives the shape you are asking for:

>>> np.array([[1, 2, 3]]).T.shape
(3, 1)

Now, look at the array. Only the first column of this 2D array is filled.

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

Given these two arrays:

>>> a = np.array([[1, 2, 3]])
>>> b = np.array([[1, 2, 3]]).T
>>> a
array([[1, 2, 3]])
>>> b
array([[1],
       [2],
       [3]])

You can take advantage of broadcasting:

>>> a * b
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

The missing numbers are filled in. Think for rows and columns in table or spreadsheet.

>>> a + b
array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]]) 

Doing this with higher dimensions gets tougher on your imagination.

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