🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #27

Dot Product

The dot product multiplies two vectors' matching components and sums the results — turning two vectors into a single number that measures how much they point in the same direction. It's the single most-used operation in classical ML.

Formula

\[ \vec{a} \cdot \vec{b} = \sum_{i=1}^{n} a_i b_i = a_1b_1 + a_2b_2 + \dots + a_nb_n \] \[ \vec{a} \cdot \vec{b} = \lVert \vec{a} \rVert\, \lVert \vec{b} \rVert \cos\theta \]

\(a_i, b_i\) are the components of vectors \(\vec{a}\) and \(\vec{b}\), and \(\theta\) is the angle between them. The second form is what makes the dot product geometrically meaningful: it directly encodes how aligned two vectors are.

Geometric Intuition

a (along axis) b θ projection = ∥b∥cosθ

The dot product equals \(\lVert a \rVert\) times the length of b's shadow cast onto a — the projection.

When \(\theta = 0°\) (vectors point the same way), \(\cos\theta = 1\) — the dot product is maximized. When \(\theta = 90°\) (perpendicular), \(\cos\theta = 0\) — the dot product is exactly zero. When vectors point in opposite directions, the dot product is negative.

Numerical Example

For \(\vec{a} = [3, 4]\) and \(\vec{b} = [4, 3]\):

\[ \vec{a}\cdot\vec{b} = (3)(4) + (4)(3) = 12 + 12 = 24 \] \[ \lVert a \rVert = \lVert b \rVert = 5, \qquad \cos\theta = \frac{24}{5 \times 5} = 0.96 \ \Rightarrow\ \theta \approx 16.3° \]
import numpy as np

a = np.array([3, 4])
b = np.array([4, 3])

dot = np.dot(a, b)                                  # 24
cos_theta = dot / (np.linalg.norm(a) * np.linalg.norm(b))
print(dot, cos_theta)                                 # 24  0.96

Where the Dot Product Shows Up in ML

  • Linear/logistic regression: a prediction is a dot product of the weight vector and feature vector: \(z = \vec{w}\cdot\vec{x} + b\)
  • SVM: the decision boundary equation \(\vec{w}^T\vec{x}+b=0\) is a dot product
  • Cosine similarity (built from the dot product) measures how similar two documents or embedding vectors are — core to search and recommendation systems
  • Neural networks: every neuron computes a dot product of its inputs and weights before applying an activation function

Common Mistakes

  • Assuming a large dot product always means "similar" — it also grows with vector magnitude, not just direction. Cosine similarity (dividing by both magnitudes) isolates the direction/angle effect.
  • Trying to take a dot product of vectors with mismatched lengths — undefined, just like vector addition.

Interview Relevance

Q: "What does it mean when the dot product of two feature vectors is zero?" The vectors are orthogonal (perpendicular) — geometrically, they share no directional overlap; in ML this often signals the underlying features they represent are unrelated/uncorrelated in that vector space.

Practice Question

Compute the dot product of \(\vec{a}=[1,0]\) and \(\vec{b}=[0,1]\) by hand. What does the result tell you about the angle between them?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Dot Product – FAQs

Quick answers about learning Dot Product in Machine Learning.

This free note from CodingNow 2.0 explains Dot Product in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Dot Product, is 100% free with no signup required.
With focused practice, most students grasp Dot Product in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now