I want to reshape
[1,2,3,4]
into:
[[1],[2],[3],[4]]
Is there a simple numpy method to do this?
Use
reshaped_arr = arr.reshape(4,1)
Testing it out:
import numpy as np
arr = np.arange(4)
reshaped_arr = arr.reshape(4,1)
print (reshaped_arr)
Output:
[[0]
[1]
[2]
[3]]
There are a few ways. You can use the reshape
method:
a = np.array([1, 2, 3, 4])
a.reshape(len(a), -1)
Or the reshape
free function:
np.reshape(a, (len(a), -1))
Or np.newaxis
:
a[:, np.newaxis]
All of these give the same output:
array([[1],
[2],
[3],
[4]])