This course has been archived: A newer version with updated syllabus and improved content is now available.
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.
The dot product is defined for two vectors of the same dimension. If you have two vectors, say a and b, both with n elements:
a=a1a2⋮an,b=b1b2⋮bnTheir dot product, often denoted as a⋅b or aTb, is calculated by multiplying corresponding elements and summing the results:
a⋅b=a1b1+a2b2+⋯+anbn=i=1∑naibiExample:
Let's calculate the dot product of two 3-dimensional vectors: v=[1,2,3] w=[4,−5,6]
Using the formula: v⋅w=(1×4)+(2×−5)+(3×6) v⋅w=4−10+18 v⋅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.
NumPy provides convenient ways to compute the dot product.
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
@ 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.
The dot product has a powerful geometric interpretation related to the angle between the two vectors. The formula is:
a⋅b=∥a∥∥b∥cos(θ)Where:
This formula allows us to find the angle between two vectors if we know their components:
cos(θ)=∥a∥∥b∥a⋅bFrom this, we can infer relationships between vectors based on their dot product:
The dot product is also related to the concept of projection. The scalar projection of vector a onto vector b (how much of a points in the direction of b) can be calculated using the dot product: ∥b∥a⋅b.
The dot product appears frequently in machine learning algorithms:
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.
Was this section helpful?
numpy.dot function, including its usage for vector dot products and the @ operator.© 2026 ApX Machine LearningEngineered with