🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.outer()

AI & DATA SCIENCE // np-outer

np.outer() computes the outer product of two vectors, producing a matrix where each element (i, j) is the product of the i-th element of the first vector and the j-th element of the second.

Syntax

np.outer(a, b)

Deep Dive Course

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.outer() showing how it is used in production NumPy code.

editor.html
import numpy as np

weights = np.array([1, 2])
inputs = np.array([10, 20, 30])
print(np.outer(weights, inputs))
localhost:3000

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))
Example 02Advanced Example
import numpy as np

weights = np.array([1, 2])
inputs = np.array([10, 20, 30])
print(np.outer(weights, inputs))

Best Practices

  • Use np.outer() when you need every pairwise product between two vectors arranged in a grid, rather than manually building nested loops
  • 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
  • Distinguish outer(), which expands two vectors into a matrix, from dot()/inner(), which collapse two vectors into a scalar, clearly when choosing between them

Interview Question

For vectors of length 3 and 2, why does np.outer() produce a 3x2 matrix while np.dot() would raise a shape error on the same two vectors?

Hint: Think about what shape requirement each operation actually has.

The outer product has no shape-matching requirement at all — it pairs every element of the first vector with every element of the second, so vectors of length m and n simply produce an m by n result regardless of whether m equals n. The 1D dot product, by contrast, requires the two vectors to have the exact same length, since it sums up element-by-element products at matching positions; vectors of length 3 and 2 have no way to be paired up position by position, which is why attempting a dot product between them raises a shape-mismatch error.

Exercises

MediumPractice using np.outer() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5])
print(np.outer(a, b))

Frequently Asked Questions

For vectors of length 3 and 2, why does np.outer() produce a 3x2 matrix while np.dot() would raise a shape error on the same two vectors?

The outer product has no shape-matching requirement at all — it pairs every element of the first vector with every element of the second, so vectors of length m and n simply produce an m by n result regardless of whether m equals n. The 1D dot product, by contrast, requires the two vectors to have the exact same length, since it sums up element-by-element products at matching positions; vectors of length 3 and 2 have no way to be paired up position by position, which is why attempting a dot product between them raises a shape-mismatch error.

Related Functions

np-dotnp-innernp-matmul