🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI Freenp.outer(): Outer Product in NumPy

To calculate every pairwise product between two inputs, use NumPy’s np.outer function. It multiplies each value in one input by each value in the other and returns a two-dimensional array.
This article explains how the input shapes determine the result and walks through an example comparing np.outer with np.multiply.outer.
TL;DR
np.outer(a, b) multiplies every value in a by every value in b. It returns a two-dimensional array with one row per flattened item in a and one column per flattened item in b. If you need to preserve dimensions from multidimensional inputs, use np.multiply.outer(a, b) instead.
- For input lengths M and N, np.outer returns shape (M, N).
- Non-1-D inputs are flattened before multiplication.
- Use np.dot or @ for an inner or matrix product, not a pairwise outer product.
What is np.outer?
np.outer calculates the outer product. Each cell is result[i, j] = a[i] * b[j], pairing one value from each input.
For lengths three and two, the result has shape (3, 2): the first input selects rows and the second selects columns.
The signature is numpy.outer(a, b, out=None). Inputs may be array-like. NumPy flattens each before calculating, so original axes are not preserved.
Use np.dot for a vector inner product, which sums corresponding products. Use @ or np.matmul for compatible matrix multiplication.
How to use np.outer step by step
For vector inputs, run this example to see the pairwise values and how multidimensional inputs differ. I ran this file and compared np.outer with np.multiply.outer.
Step 1: Create two input vectors
The example uses short vectors, then compares a matrix and vector.
import numpy as np
left = np.array([2, 5, 7])
right = np.array([3, 11])
print("np.outer:")
print(np.outer(left, right))
matrix = np.array([[1, 2], [3, 4]])
vector = np.array([10, 20, 30])
print("\nnp.outer flattens matrix:")
result = np.outer(matrix, vector)
print("shape:", result.shape)
print(result)
print("\nnp.multiply.outer keeps input dimensions:")
tensor = np.multiply.outer(matrix, vector)
print("shape:", tensor.shape)
print(tensor)
Step 2: Run the program and read each row
Save the program as outer_demo.py and run python3 outer_demo.py. Here is the complete output from that run:
np.outer: [[ 6 22] [15 55] [21 77]] np.outer flattens matrix: shape: (4, 3) [[ 10 20 30] [ 20 40 60] [ 30 60 90] [ 40 80 120]] np.multiply.outer keeps input dimensions: shape: (2, 2, 3) [[[ 10 20 30] [ 20 40 60]] [[ 30 60 90] [ 40 80 120]]]

The first row pairs 2 with both right-hand values. Later rows do the same with 5 and 7.
The vector result is (3, 2), matching the input lengths.
Step 3: Match the function to the shape you need
The (2, 2) matrix contributes four flattened values, so np.outer with a length-three vector returns (4, 3).
np.multiply.outer retains both input shapes’ axes and returns (2, 2, 3). Broadcasting can express a custom axis arrangement.
Choose the function by the required output axes, then verify the result shape.

Shape and related-operation edge cases
Check the cases below when choosing an operation or estimating its result.
A matrix input is accepted, then flattened
For matrix inputs, choose between flattened values and preserved axes:
| Input shapes | Function | Output shape |
|---|---|---|
| (2, 2) and (3,) | np.outer | (4, 3) |
| (2, 2) and (3,) | np.multiply.outer | (2, 2, 3) |
| (2,) and (3,) | np.outer | (2, 3) |
Check the intended shape instead of guessing
I checked a (2, 2) matrix and (3,) vector against the desired (2, 2, 3) shape:
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
vector = np.array([10, 20, 30])
result = np.outer(matrix, vector)
expected_shape = (2, 2, 3)
print("input shapes:", matrix.shape, vector.shape)
print("np.outer shape:", result.shape)
print("expected shape:", expected_shape)
if result.shape != expected_shape:
print("Shape mismatch: np.outer flattens each input before multiplying.")
print("Use np.multiply.outer(matrix, vector) for shape (2, 2, 3).")
Save as shape_probe.py and run python3 shape_probe.py:
input shapes: (2, 2) (3,) np.outer shape: (4, 3) expected shape: (2, 2, 3) Shape mismatch: np.outer flattens each input before multiplying. Use np.multiply.outer(matrix, vector) for shape (2, 2, 3).
The call succeeds, but the shape check exposes the mismatch. Use np.multiply.outer when separate axes are required.
Do not substitute dot or matrix multiplication
These operations have different output rules:
| Operation | Typical vector result | Use it for |
|---|---|---|
| np.outer(a, b) | 2-D array | Every pair of values |
| np.dot(a, b) | Scalar | Sum of corresponding products |
| a @ b | Scalar for vectors, matrix for 2-D inputs | Inner or matrix multiplication |
Broadcasting gives explicit axis control: matrix[…, None] * vector adds the vector along a final axis.
Account for data types, empty inputs, and result size
Result dtype and multiplication behavior depend on the input types:
| Input case | What to inspect | Practical check |
|---|---|---|
| Integer arrays | Result dtype and value range | Use a wider dtype when values may exceed the input type’s range |
| Floating-point arrays | Precision and rounding | Choose a dtype suitable for the calculation |
| Object arrays | Each object’s multiplication behavior | Confirm the element types before computing |
Fixed-width integers can overflow, so cast before multiplying when needed. Object arrays use each object’s multiplication behavior.
An empty input yields an empty dimension, while a scalar contributes one value.
Estimate output size by multiplying the flattened input lengths and bytes per value. A compatible out array can reuse storage.
Conclusion
Use np.outer for a 2-D pairwise result, or np.multiply.outer when all input axes belong in the result.
For adjacent NumPy topics, see AskPython’s guide to Pandas DataFrames and NumPy arrays and its explanation of calculating a square root with NumPy. NumPy’s outer reference and ufunc outer reference document the exact behavior and alternatives.
FAQ
These quick answers cover common questions about the function and its returned dimensions.
What does np.outer do?
It multiplies every value in the first flattened input by every value in the second and returns the pairwise results in a two-dimensional array.
Does np.outer preserve the shape of a matrix input?
No. np.outer flattens each input before calculating the pairwise products. Use np.multiply.outer when the output should retain axes from multidimensional inputs.
What is the difference between np.outer and np.dot?
For two vectors, np.outer returns all pairwise products as a matrix, while np.dot multiplies corresponding entries and sums them to one scalar.
How do I get a multidimensional outer product in NumPy?
Use np.multiply.outer(a, b) to apply multiplication across every pair while preserving all axes from both inputs. Check the resulting shape before using it downstream.


