Array Multiplication in NumPy
Dot Product, Element-wise, and Matrix Ops
Next Topic ⮕Array Transpose in NumPy - Examples
Introduction
Multiplication in NumPy isn't a one-size-fits-all operation. Depending on the context, you may be multiplying values element-wise, performing a matrix multiplication, or applying the dot product. In this tutorial, we’ll peel back the layers of each type and teach you not just the syntax, but the logic that drives these powerful operations.
1. Element-wise Array Multiplication
This is the most intuitive form of multiplication—each element in one array is multiplied by the corresponding element in the other. The arrays must be the same shape (or compatible for broadcasting).
import numpy as np
a = np.array([2, 4, 6])
b = np.array([1, 3, 5])
result = a * b
print(result)
[ 2 12 30 ]
Explanation:
The output is simply [2×1, 4×3, 6×5]
. This is called Hadamard product in linear algebra terms. Element-wise multiplication is used frequently in data scaling, cost calculations, and signal processing.
2. Matrix Multiplication (2D Arrays)
When working with 2D arrays (matrices), multiplication shifts from element-by-element to a row-column pairing structure. You can use either np.dot()
or the @
operator (introduced in Python 3.5+).
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = A @ B
print(result)
[[19 22]
[43 50]]
Explanation:
Each row of A
is multiplied with each column of B
and summed:
- Row 1 × Col 1: (1×5 + 2×7) = 19
- Row 1 × Col 2: (1×6 + 2×8) = 22
- Row 2 × Col 1: (3×5 + 4×7) = 43
- Row 2 × Col 2: (3×6 + 4×8) = 50
3. Using np.dot()
vs np.matmul()
vs @
Let’s clear the confusion:
np.dot(a, b)
: Handles dot product and matrix multiplication depending on dimensions.np.matmul(a, b)
: Strictly matrix multiplication.a @ b
: Same asnp.matmul()
, Pythonic syntax.
4. Safety Checks
Before performing matrix multiplication, always verify the shapes. The inner dimensions must match: (m×n) @ (n×p)
results in (m×p)
.
print(A.shape) # (2, 2)
print(B.shape) # (2, 2)
# Inner dimensions are both 2 — valid for matrix multiplication
5. Broadcasting with Multiplication
NumPy smartly handles size mismatches using broadcasting rules. Here’s a valid multiplication between a 2D and 1D array:
X = np.array([[1, 2], [3, 4]])
Y = np.array([10, 100])
result = X * Y
print(result)
[[ 10 200]
[ 30 400]]
The 1D array is stretched across each row of the 2D array. This broadcasting behavior is powerful and often used in batch computations.
6. Common Pitfalls and Gotchas
- Mismatch in dimensions: Leads to
ValueError
. - Using
*
for matrix multiplication: Incorrect! Always use@
ornp.dot()
. - Shape misunderstanding: Always print
shape
before operations in complex logic.
7. Summary
Array multiplication in NumPy is more than just math—it's a foundational operation for data science, machine learning, and numerical simulations. Whether you're scaling numbers or solving linear equations, choosing the right multiplication method is key to clean and efficient code.
Next Steps
- Practice with mismatched shapes and reshape them to make multiplication valid.
- Try
np.dot()
on 3D arrays and observe the behavior. - Test performance differences between pure Python loops and NumPy multiplication.