Swap Numpy row vector to column vector

This time we teach ourselves how to swap Numpy row vector to column vector.

Reshape method

To convert a row vector in Numpy to a column vector, you can use the reshape method. The reshape method allows you to reshape the array into a different shape. To convert a 1-dimensional row vector to a column vector, you can use reshape with the argument (-1, 1), which means to reshape the array into a 2-dimensional array with one row and one column. Here’s an example:

import numpy as np

row_vector = np.array([1, 2, 3, 4, 5])
column_vector = row_vector.reshape(-1, 1)

print("Row vector:", row_vector)
print("Column vector:")
print(column_vector)

This would output:

Row vector: [1 2 3 4 5]
Column vector:
[[1]
 [2]
 [3]
 [4]
 [5]]
See also  Ultimate tutorial on how to round in Numpy