Transforming a row vector into a column vector in Numpy

MY_G picture MY_G · Apr 3, 2016 · Viewed 69.7k times · Source

Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy?

Answer

Mahdi Ghelichi picture Mahdi Ghelichi · Jan 19, 2018

We can simply use the reshape functionality of numpy:

a=np.array([[1,2,3,4]])
a:
array([[1, 2, 3, 4]])

a.shape
(1,4)
b=a.reshape(-1,1)
b:
array([[1],
       [2],
       [3],
       [4]])

b.shape
(4,1)