AI Hints
Statistics & Math for ML · Lesson 78 of 138

The Dot Product

Multiply and sum.

⏱ 2 min read▶ Video at 1:32:02🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

The dot product of two vectors is simple. Multiply them position by position, then add everything up. It turns two vectors into a single number. And it's at the heart of almost every machine learning model.

The Dot Product
From the video: The Dot Product
  • Multiply position by position
  • Add it all up
  • One number

A prediction is a dot product

Here's a house, and a set of weights, one for each feature. The dot product multiplies size by its weight, rooms by its weight, and age by its weight, then adds them. Run it. That's exactly how a linear model makes a prediction. The at sign is Python's shortcut for it.

dot.pypython
import numpy as np

house = np.array([120, 4, 10])
weights = np.array([2.0, 10.0, -1.5])

print("by hand:", (house * weights).sum())
print("np.dot: ", np.dot(house, weights))
print("@:      ", house @ weights)
Output
by hand: 265.0
np.dot:  265.0
@:       265.0

What the dot product measures

Besides making predictions, the dot product measures how much two vectors point the same way. It's large and positive when they point in similar directions, zero when they're at right angles, and negative when they point in opposite directions. Neural networks are, at heart, millions of dot products.

keyvalue
FormulaΣ aᵢ × bᵢ
Positivesimilar direction
Zeroat right angles
Negativeopposite directions

Summary

The dot product multiplies and sums, turning two vectors into one number. Linear models and neural networks are built on it. Next, the length of a vector.

Key takeaways
  • np.dot or @
  • Prediction = features · weights
  • Measures alignment
Next up: Length and Distance — norms