In [1]:
import numpy as np
In [2]:
#In Python, one way to calculate the dot product would be taking the
# sum of a list comprehension performing element wise multiplication.

def inner_dot(x, y):
    return sum(x_i * y_i for x_i, y_i in zip(x, y))
In [3]:
x = np.array([2, 7, 1])
y = np.array([8, 2, 8])

print("The dot product of x and y is: ", inner_dot(x, y))
The dot product of x and y is:  38
In [4]:
#Alternatively, we can use the np.inner() function.
dot_product = np.inner(x, y)
print("The dot product of x and y is: ", dot_product)
The dot product of x and y is:  38
In [5]:
# We can also use numpy.dot() function
# This must be used for 2D and 3D arrays
print("The dot product of x and y is: ", np.dot(x, y))
The dot product of x and y is:  38
In [6]:
# From Python 3.5 we can use an explicit operator @
# for the dot product, so you can write the following
print("The dot product of x and y is: ", x @ y)
The dot product of x and y is:  38