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,) : 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
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
array([, , , , ])