Create a Vector in Python using numpy array objects and reshape method.
Vectors are backbone of math operations. Vectors are used to pass feature values in Neural Networks in Deep Learning and other machine learning operations. In this mini tutorial we create both row and column vectors. Also, we understand peculiarities of rank 1 array and how to handle those.
# Imports
import numpy as np
# Let's build a vector
vect = np.array([1,1,3,0,1])
vect
array([1, 1, 3, 0, 1])
# Let's check shape of vect
vect.shape
(5,)
# (5,) : this is called a rank 1 array and messes up results
# Always make to sure to reshape arrays to desired dimensions
# Correct approach
# Let's convert to row vector form
rvect = np.array([1,1,3,0,1]).reshape(1,-1)
rvect.shape
(1, 5)
rvect
array([[1, 1, 3, 0, 1]])
# Let's convert to column vector form
cvect = np.array([1,1,3,0,1]).reshape(-1,1)
cvect.shape
(5, 1)
cvect
array([[1], [1], [3], [0], [1]])
Related Resources:
- Numpy Arange Create an Array | Numpy tutorial Numpy Arange Use numpy arange method to create array with sequence of numbers. In the below example we create an...
- Reshape Numpy array | Numpy Tutorial Reshape Numpy Array Reshape numpy array with “reshape” method of numpy library. Reshaping numpy array is useful to convert array...
- Determinant of a Matrix in Python | Numpy Tutorial Determinant of a Matrix Determinant of a Matrix can be calculated by “det” method of numpy’s linalg module. Determinant of...
- Diagonal of Square Matrix in Python | Numpy Tutorial Diagonal of Square Matrix Diagonal of Square Matrix can be fetched by diagonal method of numpy array. Diagonal of Square...
- Create array with Random numbers | Numpy tutorial Create array with Random Numbers Create array with Random Numbers with random module of Numpy library. In this Numpy tutorial...
- Matrix Transpose in Python | Numpy Tutorial Matrix Transpose Matrix Transpose means to switch rows and columns. Transpose of a matrix is a matrix with switched rows...
- Trace of Matrix in Python | Numpy Tutorial Trace of Matrix Trace of Matrix is the sum of main diagonal elements of the matrix. Main Diagonal also known...
- Get value from index in Numpy array | Numpy tutorial Value from Index in Numpy Get value from index in numpy array using python like slicing. In below examples we...
- Echeleon form of Matrix | Numpy tutorial Echeleon form of Matrix Find echeleon form of a matrix using “rref” method of sympy Matrix module Echeleon form of...
- Convert an Array to one dimensional vector Convert an Array to One dimensional Vector | Flatten Convert an Array to One dimensional Vector with flatten method of...