Python – Numpy Dot Product

In mathematics, the dot product or scalar product is an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors) and returns a single number.

For example:

import numpy as np

#define a vector of 1 and 2

a = np.array([1,2])

#define a vector of 2 and 1

b = np.array([2,1])

dot = 0

for e, f in zip(a,b):

    dot += e*f

#prints 4
print(dot)

#also prints 4
print(np.sum(a*b))

#also prints 4
print(np.dot(a,b))

#also prints 4
print(a.dot(b))

#also prints 4
print(b.dot(a))

**This article is written for Python 3.6

<< Numpy Arrays

Numpy Indexing, Slicing, and Iterating >>

%d bloggers like this: