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
import numpy as np row_vector = np.array([10,20,30]) print(row_vector)
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
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
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
array([, , ])
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.