How To Calculate Dot Product With Numpy

Next Story

How To Add/Subtract With Numpy Matrices

Numpy dot product

 

Dot product is a common linear algebra matrix operation to multiply vectors and matrices. It is commonly used in machine learning and data science for a variety of calculations.

It can be simply calculated with the help of numpy. This post will go through an example of how to use numpy for dot product.

First, let’s import numpy as np.

import numpy as np

Next, we will create some numpy arrays to do dot product with.

a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])

Then, we can can calculate our dot product.

When we calculate the dot product of a and b, the calculation is as follows:

1*6 + 2*7 + 3*8 + 4*9 + 5*10.

np.dot(a, b)
# 130

# or an alternative way

a.dot(b)
# 130

The full code example can be found below.

import numpy as np

a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])

np.dot(a, b)
# 130

# or an alternative way

a.dot(b)
# 130
https://www.googletagmanager.com/gtag/js?id=UA-63695651-4