NumPy dot: How to calculate the inner product of vectors in Python

Here is an example to calculate an inner product of two vectors in Python.

import numpy as np

v = np.array([1, 2])
w = np.array([3, 4])

i = np.dot(v, w)

print(i)  # 11
print(type(i))  # <class 'numpy.int64'>

The dot() returns the inner product of v and w.

\[ 1 \times 3 + 2 \times 4 = 11 \]

Another example.

import numpy as np

v = np.array([1, 2.9])
w = np.array([3, 4.5])

i = np.dot(v, w)

print(i)  # 16.049999999999997
print(type(i))  # <class 'numpy.float64'>

Inner

import numpy as np

a = np.array([1, 2])
b = np.array([-5, 4])

dot = np.dot(a, b)
inner = np.inner(a, b)

print(dot)  # 3
print(inner)  # 3

The inner() returns the the same value as the dot() in 1D arrays (that is "vectors") product.

The dot and inner product of 2D arrays

import numpy as np

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

dot = np.dot(a, b)
inner = np.inner(a, b)

print(dot)
# [[19 22]
#  [43 50]]

print(inner)
# [[17 23]
#  [39 53]]

NumPy Vector

NumPy Tutorial