How to stack all columns in a 2-dimensional Numpy array into a 1-dimensional array.
I.e. I have:
x = np.array([[1, 3, 5],[2, 4, 6]])
And I want to get:
np.array([1, 2, 3, 4, 5, 6])
Is there a way to achieve this without a loop or list comprehension?
You can use ravel:
x = np.array([[1, 3, 5],[2, 4, 6]])
res = x.ravel('F') # or x.T.ravel()
# array([1, 2, 3, 4, 5, 6])
Using flatten with 'F'
x.flatten('F')
Out[114]: array([1, 2, 3, 4, 5, 6])
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