>>> 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?
You can use 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.
Edit:
Another option is:
np.expand_dims(np.arange(10), axis=1)
numpy.expand_dims
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With