>>> print np.array([np.arange(10)]).transpose()
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
Is there a way to get a vertical arange without having to go through these extra steps?
有没有办法获得垂直的arange而不必经过这些额外的步骤?
2 个解决方案
#1
14
You can use np.newaxis:
你可以使用np.newaxis:
>>> np.arange(10)[:, np.newaxis]
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
np.newaxis
is just an alias for None
, and was added by numpy
developers mainly for readability. Therefore np.arange(10)[:, None]
would produce the same exact result as the above solution.
np.newaxis只是None的别名,由numpy开发人员添加,主要是为了提高可读性。因此,np.arange(10)[:,None]将产生与上述解决方案相同的精确结果。
#2
11
I would do:
我会做:
np.arange(10).reshape((10, 1))
Unlike np.array, reshape is a light weight operation which does not copy the data in the array.
与np.array不同,reshape是一种轻量级操作,不会复制数组中的数据。
#1
14
You can use np.newaxis:
你可以使用np.newaxis:
>>> np.arange(10)[:, np.newaxis]
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
np.newaxis
is just an alias for None
, and was added by numpy
developers mainly for readability. Therefore np.arange(10)[:, None]
would produce the same exact result as the above solution.
np.newaxis只是None的别名,由numpy开发人员添加,主要是为了提高可读性。因此,np.arange(10)[:,None]将产生与上述解决方案相同的精确结果。
#2
11
I would do:
我会做:
np.arange(10).reshape((10, 1))
Unlike np.array, reshape is a light weight operation which does not copy the data in the array.
与np.array不同,reshape是一种轻量级操作,不会复制数组中的数据。