How to Convert Row vector to Column vector in Numpy : Methods

Convert Row vector to Column vector in Numpy

Numpy is a python module for implementing complex mathematical calculations on the array. To do so numpy has many inbuilt functions that manipulate the array. In this entire post, you will know how to convert row vector to column vector in python using numpy.

What is Vector in Python?

A vector is a geometric object that contains both the magnitude and direction.  The vector is represented as a list in python. Each number in the list is component of the vector. For example, a vector with two components would be represented as [10, 20]. The length of the vector is represented by the magnitude of the vector.

Methods to  Convert Row vector to Column Vector in Numpy

Let’s know all the methods that allow you to convert the row vector to a column vector in numpy. Below is the dummy or sample numpy array I am using for conversion.

Row vector

import numpy as np
row_vector = np.array([10,20,30])
print(row_vector)

Output

array([10, 20, 30])

Method 1: Use reshape() method

The reshape() method allows you to convert the row vector to a column vector. Here you have to just pass the two arguments. The First will be the number of columns and the second is the number of rows.

Execute the below lines of code.

import numpy as np
row_vector = np.array([10,20,30])
col_vector = row_vector.reshape((3,1))
col_vector

Output

converting row vector to column vector using the reshape() method
converting row vector to column vector using the reshape() method

Method 2: Using the numpy.column_stack function

The second method to convert row vector to column vector is the use of np.column_stack function. It will accept the row vector as the input inside the square bracket for the conversion.

Run the below lines of code to implement this method.

import numpy as np
row_vector = np.array([10,20,30])
col_vector = np.column_stack([row_vector])
col_vector

Output

comverting row vector to column vector using the column_stack method
converting row vector to column vector using the column_stack method

Method 3:  Convert Row vector to Column vector using the numpy transpose

The method for the conversion is the transpose the row vector. The transpose ( T) changes the shape of the numpy array. You have to use the input_array.T to transpose the array.

You will get the column vector when you will run the below lines of code.

import numpy as np
row_vector = np.array([10,20,30])
col_vector = row_vector.T
col_vector

Output

array([[10],
       [20],
       [30]])

 

Conclusion

Sometimes changing the dimensions or shape of the vector helps you to do matrix or vector calculations very easily. So convert them when you want to perform complex calculations. These are methods to convert the row vector to a column vector.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner