Introduction to Dot Product in NumPy
Understanding the dot product is fundamental to working with vectors and matrices in machine learning, data science, and numerical computing. In NumPy, the dot()
function lets you perform this operation efficiently and with minimal code.
What is a Dot Product?
In simple terms, the dot product multiplies corresponding elements of two arrays and then sums them. It can be used in:
- Vector × Vector: Returns a scalar
- Matrix × Vector: Returns a 1D array
- Matrix × Matrix: Returns a matrix
Step-by-Step: Vector Dot Product
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.dot(a, b)
print("Dot product of vectors:", result)
Dot product of vectors: 32
Explanation:
The calculation is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
. The result is a single number (scalar), which is typical for 1D vectors.
Matrix × Vector Dot Product
A = np.array([[1, 2], [3, 4]])
B = np.array([5, 6])
result = np.dot(A, B)
print("Dot product of matrix and vector:", result)
Dot product of matrix and vector: [17 39]
Explanation:
Each row of matrix A is multiplied by the vector:
1*5 + 2*6 = 17
3*5 + 4*6 = 39
The result is a 1D array with one dot product per row.
Matrix × Matrix Dot Product
X = np.array([[1, 2], [3, 4]])
Y = np.array([[2, 0], [1, 2]])
result = np.dot(X, Y)
print("Dot product of two matrices:\n", result)
Dot product of two matrices:
[[ 4 4]
[10 8]]
Explanation:
This is true matrix multiplication. For the element at position (0, 0):
1*2 + 2*1 = 4
And similarly for the other elements. This matches the behavior of algebraic matrix multiplication rules.
Shape Verification – Why It Matters
Before performing a dot product, ensure the inner dimensions match. In A.dot(B)
, the number of columns in A
must equal the number of rows in B
.
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10], [11, 12]])
print("Shape A:", A.shape)
print("Shape B:", B.shape)
print("Dot Product:\n", np.dot(A, B))
Shape A: (2, 3)
Shape B: (3, 2)
Dot Product:
[[ 58 64]
[139 154]]
Tip:
Trying to dot arrays with mismatched dimensions will raise a ValueError
. Always inspect shapes using .shape
before dotting.
Checks and Common Mistakes
- Shape Mismatch: Double-check dimensions when dotting
- Confusing Element-wise * vs dot(): Use
*
for element-wise multiplication,dot()
for matrix multiplication - Vector vs Matrix: A 1D array and a 2D row/column vector behave differently in dot operations
When to Use np.dot()
Use np.dot()
when you want:
- Classical vector algebra operations
- Efficient implementation of matrix multiplication
- Readable and performance-optimized code for ML and scientific computing
Conclusion
The np.dot()
function is more than just a multiplication tool—it's a bridge to linear algebra in Python. With NumPy, you can write expressive, efficient, and robust matrix operations that form the backbone of real-world data and AI workflows.