How to create a vector in Python using NumPy

In this instructional exercise, we will figure out how we can make a vector utilizing Numpy library. We will likewise investigate essential activity of vector like performing expansion of two vectors, deduction of two vectors, division of two vectors, duplication of two vectors, vector speck item and vector scalar item.

What is Vector?

A vector is known as a solitary aspect cluster. In Python, vector is a solitary one-aspect cluster of records and acts same as a Python list. As per a Google, vector addresses heading just as size; particularly it decides the position one point in a space comparative with another.

Vectors are vital in the Machine learning since they have greatness and furthermore the bearing elements. How about we see how we can make the vector in Python.

At the core of a Numpy library is the cluster object or the ndarray object (n-dimensional exhibit). You will utilize Numpy clusters to perform sensible, measurable, and Fourier changes. As a feature of working with Numpy, one of the main things you will do is make Numpy clusters. The primary target of this aide is to illuminate an information proficient, you, about the various instruments accessible to make Numpy clusters.

NumPy is a broadly useful cluster handling bundle. It gives a superior presentation multidimensional cluster article, and devices for working with these exhibits. It is the basic bundle for logical registering with Python. Numpy is essentially utilized for making exhibit of n aspects.

Vector are worked from parts, which are standard numbers. We can consider a vector a rundown of numbers, and vector variable based math as tasks performed on the numbers in the rundown. All in all vector is the numpy 1-D cluster.

Consideration! Fortify your establishments with the Python Programming Foundation Course and gain proficiency with the essentials.

Regardless, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. Also regardless your Machine Learning Journey, join the Machine Learning – Basic Level Course

Syntax : np.array(list)
Argument : It take 1-D list it can be 1 row and n columns or n rows and 1 column
Return : It returns vector which is numpy.ndarray

Note: We can make vector with other strategy too which return 1-D numpy cluster for instance np.arange(10), np.zeros((4, 1)) gives 1-D exhibit, however most proper way is utilizing np.array with the 1-D rundown.

Examples:

# importing numpy
import numpy as np
 
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
 
# creating a 1-D list (Vertical)
list2 = [[10],
        [20],
        [30]]
 
# creating a vector1
# vector as row
vector1 = np.array(list1)
 
# creating a vector 2
# vector as column
vector2 = np.array(list2)
 
 
# showing horizontal vector
print("Horizontal Vector")
print(vector1)
 
print("----------------")
 
# showing vertical vector
print("Vertical Vector")
print(vector2)

Output :

Horizontal Vector
[1 2 3]
----------------
Vertical Vector
[[10]
 [20]
 [30]]

In this model we will make a level vector and an upward vector

# importing numpy
import numpy as np
 
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
 
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
 
# creating first vector
vector1 = np.array(list1)
 
# printing vector1
print("First Vector          : " + str(vector1))
 
# creating second vector
vector2 = np.array(list2)
 
# printing vector2
print("Second Vector         : " + str(vector2))
 
# adding both the vector
# a + b = (a1 + b1, a2 + b2, a3 + b3)
addition = vector1 + vector2
 
# printing addition vector
print("Vector Addition       : " + str(addition))
 
# subtracting both the vector
# a - b = (a1 - b1, a2 - b2, a3 - b3)
subtraction = vector1 - vector2
 
# printing addition vector
print("Vector Subtraction   : " + str(subtraction))
 
# multiplying  both the vector
# a * b = (a1 * b1, a2 * b2, a3 * b3)
multiplication = vector1 * vector2
 
# printing multiplication vector
print("Vector Multiplication : " + str(multiplication))
 
# dividing  both the vector
# a / b = (a1 / b1, a2 / b2, a3 / b3)
division = vector1 / vector2
 
# printing division vector
print("Vector Division       : " + str(division))

Output :

First Vector: [5 6 9]
Second Vector: [1 2 3]
Vector Addition: [ 6  8 12]
Vector Subtraction: [4 4 6]
Vector Multiplication: [ 5 12 27]
Vector Division: [5 3 3]

Essential Arithmetic activity:

In this model we will see do math tasks which are component savvy between two vectors of equivalent length to bring about another vector with a similar length

First Vector: [5 6 9]
Second Vector: [1 2 3]
Vector Addition: [ 6  8 12]
Vector Subtraction: [4 4 6]
Vector Multiplication: [ 5 12 27]
Vector Division: [5 3 3]

Vector Dot Product
In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number.
For this we will use dot method.

# importing numpy
import numpy as np
 
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
 
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
 
# creating first vector 
vector1 = np.array(list1)
 
# printing vector1
print("First Vector  : " + str(vector1))
 
# creating second vector
vector2 = np.array(list2)
 
# printing vector2
print("Second Vector : " + str(vector2))
 
# getting dot product of both the vectors
# a . b = (a1 * b1 + a2 * b2 + a3 * b3)
# a . b = (a1b1 + a2b2 + a3b3)
dot_product = vector1.dot(vector2)
 
# printing dot product
print("Dot Product   : " + str(dot_product))

Output:

First Vector  : [5 6 9]
Second Vector : [1 2 3]
Dot Product   : 44

Vector-Scalar Multiplication
Multiplying a vector by a scalar is called scalar multiplication. To perform scalar multiplication, we need to multiply the scalar by each component of the vector.

# importing numpy
import numpy as np
 
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
 
# creating first vector 
vector = np.array(list1)
 
# printing vector1
print("Vector  : " + str(vector))
 
# scalar value 
scalar = 2
 
# printing scalar value
print("Scalar  : " + str(scalar))
  
# getting scalar multiplication value
# s * v = (s * v1, s * v2, s * v3)
scalar_mul = vector * scalar
 
# printing dot product
print("Scalar Multiplication : " + str(scalar_mul))
 
 
  

Output 

Vector  : [1 2 3]
Scalar  : 2
Scalar Multiplication : [2 4 6]

Vector Dot Product:

In math, the speck item or scalar item is a mathematical activity that takes two equivalent length successions of numbers and returns a solitary number.
For this we will utilize speck strategy.

Create a Vector

  • 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]])

It’s Your Turn Now!!!

  • Feel free to ask any doubts or questions in the comments.
  • Moreover, if you have a cooler approach to do above operations, please do share the code in comments.
  • In addition to the above, if you need any help in your Python or Machine learning journey, comment box is all yours.
  • Further, you can also send us an email.
  • For more cool stuff, follow thatascience on social media Twitter, Facebook, Linkedin, Instagram.

Also Read: How to convert a single character to string in C++?

Leave a Reply

Your email address will not be published. Required fields are marked *