Unlike the dot product, which collapses two vectors down to a single scalar, the outer product expands them into a matrix: for vectors of length m and n, np.outer(a, b) produces an m by n matrix, regardless of the inputs' original shape, since both are flattened to 1D first. Row i of the result is exactly a[i] times the entire vector b, which makes the outer product useful for constructing rank-1 matrices, certain covariance-style calculations, and broadcasting patterns that need every pairwise product between two vectors.
1Understanding np.outer()
Unlike the dot product, which collapses two vectors down to a single scalar, the outer product expands them into a matrix: for vectors of length m and n, np.outer(a, b) produces an m by n matrix, regardless of the inputs' original shape, since both are flattened to 1D first. Row i of the result is exactly a[i] times the entire vector b, which makes the outer product useful for constructing rank-1 matrices, certain covariance-style calculations, and broadcasting patterns that need every pairwise product between two vectors.
The outer product's result matrix always has shape (len(a), len(b)) — remember it grows the data into a larger matrix, the opposite direction from the dot product, which shrinks two vectors down to a single number.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))2Practical Example
Here is a real-world application of np.outer() showing how it is used in production NumPy code.
import numpy as np
weights = np.array([1, 2])
inputs = np.array([10, 20, 30])
print(np.outer(weights, inputs))3Best Practices
Follow these guidelines when working with np.outer():
1. Use np.outer() when you need every pairwise product between two vectors arranged in a grid, rather than manually building nested loops
2. Remember np.outer() always flattens its inputs first, so passing 2D arrays still produces a 1D-vector-based outer product, not a higher-dimensional result
3. Distinguish outer(), which expands two vectors into a matrix, from dot()/inner(), which collapse two vectors into a scalar, clearly when choosing between them
Tip: The outer product's result matrix always has shape (len(a), len(b)) — remember it grows the data into a larger matrix, the opposite direction from the dot product, which shrinks two vectors down to a single number.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))