ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Linear Algebra Fundamentals for Machine Learning
Chapter 1: Why Linear Algebra Matters in Machine Learning
Representing Data with Vectors and Matrices
Linear Algebra in Machine Learning Algorithms
Core Concepts Overview
Setting Up Your Python Environment
Quiz for Chapter 1
Chapter 2: Getting Started with NumPy for Numerical Computing
Introduction to NumPy Arrays
Creating NumPy Arrays
Array Indexing and Slicing
Basic Array Operations
Array Attributes and Shape Manipulation
Hands-on: NumPy Array Creation and Manipulation
Quiz for Chapter 2
Chapter 3: Working with Vectors
What is a Vector?
Vector Notation
Vectors in Python using NumPy
Vector Addition and Subtraction
Scalar Multiplication
Vector Norms: Measuring Length
The Dot Product
Hands-on: Vector Operations with NumPy
Quiz for Chapter 3
Chapter 4: Working with Matrices
What is a Matrix?
Matrix Notation and Dimensions
Matrices in Python using NumPy
Types of Matrices: Square, Identity, Zero
Diagonal and Triangular Matrices
Hands-on: Matrix Creation with NumPy
Quiz for Chapter 4
Chapter 5: Essential Matrix Operations
Matrix Addition and Subtraction
Scalar Multiplication
Matrix Transpose
Matrix Multiplication: The Dot Product
Properties of Matrix Multiplication
Hands-on: Matrix Operations with NumPy
Quiz for Chapter 5
Chapter 6: Systems of Linear Equations and Matrix Inverses
Representing Linear Equations with Matrices
The Concept of a Solution
The Identity Matrix Revisited
The Matrix Inverse
Conditions for Invertibility
Solving Ax = b using the Inverse
Calculating Inverses with NumPy
Solving Linear Systems with NumPy
Hands-on: Solving Systems with NumPy
Quiz for Chapter 6

This course has been archived: A newer version with updated syllabus and improved content is now available.

The Dot Product

The dot product (also known as the scalar product or inner product) is an essential operation involving vectors. While some vector operations, such as addition or scalar multiplication, produce another vector, the dot product takes two vectors and returns a single scalar number. This operation is fundamental in many areas, including measuring the similarity between vectors and forming the basis of more complex operations like matrix multiplication.

Calculating the Dot Product

The dot product is defined for two vectors of the same dimension. If you have two vectors, say aaa and bbb, both with nnn elements:

a=[a1a2⋮an],b=[b1b2⋮bn]a = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}, \quad b = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix}a=​a1​a2​⋮an​​​,b=​b1​b2​⋮bn​​​

Their dot product, often denoted as a⋅ba \cdot ba⋅b or aTba^T baTb, is calculated by multiplying corresponding elements and summing the results:

a⋅b=a1b1+a2b2+⋯+anbn=∑i=1naibia \cdot b = a_1 b_1 + a_2 b_2 + \dots + a_n b_n = \sum_{i=1}^{n} a_i b_ia⋅b=a1​b1​+a2​b2​+⋯+an​bn​=i=1∑n​ai​bi​

Example:

Let's calculate the dot product of two 3-dimensional vectors: v=[1,2,3]v = [1, 2, 3]v=[1,2,3] w=[4,−5,6]w = [4, -5, 6]w=[4,−5,6]

Using the formula: v⋅w=(1×4)+(2×−5)+(3×6)v \cdot w = (1 \times 4) + (2 \times -5) + (3 \times 6)v⋅w=(1×4)+(2×−5)+(3×6) v⋅w=4−10+18v \cdot w = 4 - 10 + 18v⋅w=4−10+18 v⋅w=12v \cdot w = 12v⋅w=12

The result, 12, is a scalar. Notice that the vectors must have the same number of elements for the dot product to be defined. You cannot compute the dot product of a 3-element vector and a 4-element vector.

Dot Product in Python using NumPy

NumPy provides convenient ways to compute the dot product.

  1. Using the numpy.dot() function:
import numpy as np

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

# Calculate the dot product using np.dot()
dot_product = np.dot(v, w)

print(f"Vector v: {v}")
print(f"Vector w: {w}")
print(f"Dot product (using np.dot): {dot_product}")

Output:

Vector v: [1 2 3]
Vector w: [ 4 -5  6]
Dot product (using np.dot): 12
  1. Using the @ operator: Python 3.5+ introduced the @ operator for matrix multiplication, which also works for calculating the dot product of 1D arrays (vectors).
import numpy as np

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

# Calculate the dot product using the @ operator
dot_product_operator = v @ w

print(f"Vector v: {v}")
print(f"Vector w: {w}")
print(f"Dot product (using @): {dot_product_operator}")

Output:

Vector v: [1 2 3]
Vector w: [ 4 -5  6]
Dot product (using @): 12

Both methods yield the same scalar result, 12, matching our manual calculation. The @ operator is often preferred for its conciseness in complex expressions.

Geometric Interpretation: Angles and Projections

The dot product has a powerful geometric interpretation related to the angle between the two vectors. The formula is:

a⋅b=∥a∥∥b∥cos⁡(θ)a \cdot b = \|a\| \|b\| \cos(\theta)a⋅b=∥a∥∥b∥cos(θ)

Where:

  • a⋅ba \cdot ba⋅b is the dot product of vectors aaa and bbb.
  • ∥a∥\|a\|∥a∥ and ∥b∥\|b\|∥b∥ are the magnitudes (or L2L_2L2​ norms) of vectors aaa and bbb.
  • θ\thetaθ is the angle between the two vectors.

This formula allows us to find the angle between two vectors if we know their components:

cos⁡(θ)=a⋅b∥a∥∥b∥\cos(\theta) = \frac{a \cdot b}{\|a\| \|b\|}cos(θ)=∥a∥∥b∥a⋅b​

From this, we can infer relationships between vectors based on their dot product:

  • If a⋅b>0a \cdot b > 0a⋅b>0: The angle θ\thetaθ between the vectors is less than 90 degrees (cos⁡(θ)>0\cos(\theta) > 0cos(θ)>0). The vectors point in a generally similar direction.
  • If a⋅b=0a \cdot b = 0a⋅b=0: The angle θ\thetaθ is exactly 90 degrees (cos⁡(θ)=0\cos(\theta) = 0cos(θ)=0), meaning the vectors are orthogonal (perpendicular). This is a very important property used throughout linear algebra and machine learning. Note that this assumes aaa and bbb are non-zero vectors.
  • If a⋅b<0a \cdot b < 0a⋅b<0: The angle θ\thetaθ is greater than 90 degrees (cos⁡(θ)<0\cos(\theta) < 0cos(θ)<0). The vectors point in generally opposite directions.

The dot product is also related to the concept of projection. The scalar projection of vector aaa onto vector bbb (how much of aaa points in the direction of bbb) can be calculated using the dot product: a⋅b∥b∥\frac{a \cdot b}{\|b\|}∥b∥a⋅b​.

Why is the Dot Product Important in Machine Learning?

The dot product appears frequently in machine learning algorithms:

  • Similarity Measurement: Cosine similarity, calculated as a⋅b∥a∥∥b∥\frac{a \cdot b}{\|a\| \|b\|}∥a∥∥b∥a⋅b​, uses the dot product to measure the similarity in orientation between two vectors, irrespective of their magnitudes. This is widely used in natural language processing (NLP) for comparing document or word vectors and in recommendation systems.
  • Linear Models: In models like linear regression or logistic regression, the prediction is often calculated as a weighted sum of input features. This weighted sum is precisely a dot product between the feature vector and the model's weight vector.
  • Neural Networks: The core computation within many neural network layers involves matrix multiplications, which are essentially composed of many dot products.

Understanding the dot product calculation and its geometric meaning provides a foundation for comprehending these more advanced applications. In the next section, we'll practice implementing these vector operations using NumPy.

  • Introduction to Linear Algebra, Gilbert Strang, 2016 (Wellesley-Cambridge Press) - Explains the algebraic and geometric definitions of the dot product, its calculation, properties, and applications to angles and projections.
  • numpy.dot, NumPy Developers, 2023 - Official documentation for the numpy.dot function, including its usage for vector dot products and the @ operator.
  • Mathematics for Machine Learning, Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong, 2020 (Cambridge University Press) - Provides a solid foundation in the mathematical concepts, including the dot product, that are fundamental to machine learning algorithms and their applications.

© 2026 ApX Machine Learning