Sunday, December 29, 2024
Google search engine
HomeLanguagesPython Program to Get dot product of multidimensional Vectors using NumPy

Python Program to Get dot product of multidimensional Vectors using NumPy

Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy.

Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1]

Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30

Dot Product is the sum of the product of elements at each position of the vector.

Dot Product of 1-D vectors:

Now let’s implement this in python. But we don’t need to code this from scratch, thanks to Numpy. Numpy module has a method dot which takes 2 vectors and returns the dot product of them

Python3




# import numpy
import numpy as np
  
# initialize vectors
a = np.array([2,5,3])
b = np.array([6,3,1])
  
# calculating Dot product form np.dot
c = np.dot(a,b)
print("Dot product of a and b is: ",c)


Output:

Dot product of a and b is:  30

Dot Product of 2-Dimensional vectors:

The dot product of a 2-dimensional vector is simple matrix multiplication. In one dimensional vector, the length of each vector should be the same, but when it comes to a 2-dimensional vector we will have lengths in 2 directions namely rows and columns. Rows and columns of a 2-D  vector not necessarily should be the same but the number of columns of the first vector should match the number of rows of the second vector.

Example:

Let’s do the same thing with Python (Numpy module)

Python3




# import numpy
import numpy as np
  
# initialize 2d vectors
a = np.array([[3, 9],
              [2, 5]])
b = np.array([[6, 1],
              [4, 8]])
  
# Checking the condition "No. of columns 
# of first vector == No. of rows of the
# second vector"
if a.shape[1] == b.shape[0]:
    
    # calculating Dot product using np.dot
    c = np.dot(a, b)
    print("Dot product of a and b is:\n", c)
else:
    print("No. of columns of first vector should match\
    with No. of rows of the second vector")


Output:

Dot product of a and b is:
 [[54 75]
 [32 42]]

Example:

In this example, with the same approach as shown in the above example, we are working with different data points and adding an if statement in case the code produces an error.

Python3




# import numpy
import numpy as np
  
# initialize vectors
a = np.array([[3,9,6],
              [2,5,2]])
b = np.array([[6,1],
              [4,8],
              [7,5]])
  
# Checking the condition "No. of columns
# of first vector == No. of rows of the 
# second vector"
if a.shape[1] == b.shape[0]:
    # calculating Dot product using np.dot
    c = np.dot(a,b)
    print("Dot product of a and b is:\n",c)
else:
    print("No. of columns of first vector should match with \
    No. of rows of the second vector")


Output:

Dot product of a and b is:
[[ 96 105]
[ 46  52]]

It will throw an error if the condition does not satisfy, let’s remove our if block and calculate dot product with different shapes to see what is the error

Python3




# import numpy
import numpy as np
  
# initialize vectors
a = np.array([[3,9],
              [2,5]])
b = np.array([[6,1],
              [4,8],
              [7,5]])
print("Shape of vector a: ",a.shape)
print("Shape of vector b: ",b.shape)
  
# calculating Dot product using np.dot
c = np.dot(a,b)
print("Dot product of a and b is:\n",c)


Shape of vector a:  (2, 2)

Shape of vector b:  (3, 2)

—————————————————————————

ValueError                                Traceback (most recent call last)

<ipython-input-18-346715f1fc30> in <module>

     12 

     13 # calculating Dot product using np.dot

—> 14 c = np.dot(a,b)

     15 print(“Dot product of a and b is:\n”,c)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (2,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments