0
0
NumPydata~10 mins

outer() for outer operations in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - outer() for outer operations
Input: Vector A
Input: Vector B
Compute outer product
Output: Matrix with shape (len(A), len(B))
The outer() function takes two vectors and computes all pairwise products, producing a matrix.
Execution Sample
NumPy
import numpy as np
A = np.array([1, 2])
B = np.array([3, 4])
result = np.outer(A, B)
print(result)
This code computes the outer product of vectors A and B, resulting in a 2x2 matrix.
Execution Table
StepOperationInput AInput BResult Matrix
1Start with vectors[1, 2][3, 4]N/A
2Multiply A[0] * B[0]13result[0,0] = 3
3Multiply A[0] * B[1]14result[0,1] = 4
4Multiply A[1] * B[0]23result[1,0] = 6
5Multiply A[1] * B[1]24result[1,1] = 8
6Final matrixN/AN/A[[3, 4], [6, 8]]
💡 All pairs of elements multiplied, matrix complete with shape (2,2).
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
A[1, 2][1, 2][1, 2][1, 2][1, 2][1, 2]
B[3, 4][3, 4][3, 4][3, 4][3, 4][3, 4]
resultempty[[3, 0], [0, 0]][[3, 4], [0, 0]][[3, 4], [6, 0]][[3, 4], [6, 8]][[3, 4], [6, 8]]
Key Moments - 2 Insights
Why does the output have shape (len(A), len(B)) and not (len(B), len(A))?
The outer() function multiplies each element of A by each element of B, placing results in rows for A and columns for B, as shown in execution_table rows 2-5.
Is the outer product the same as element-wise multiplication?
No, element-wise multiplication requires vectors of the same length and multiplies elements at the same positions. Outer multiplies all pairs, producing a matrix (see execution_table steps 2-5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of result[1,0]?
A6
B4
C8
D3
💡 Hint
Check the multiplication of A[1] and B[0] in execution_table step 4.
At which step does the matrix result get fully completed?
AStep 5
BStep 3
CStep 6
DStep 4
💡 Hint
Look at the final matrix output in execution_table step 6.
If vector B was changed to [5, 6], what would result[0,1] be?
A4
B6
C7
D5
💡 Hint
Multiply A[0] = 1 by new B[1] = 6 to find result[0,1].
Concept Snapshot
numpy.outer(a, b)
- Takes two 1D arrays a and b
- Returns matrix of shape (len(a), len(b))
- Each element is product of a[i] * b[j]
- Useful for pairwise multiplication
- Output is 2D array (outer product matrix)
Full Transcript
The numpy outer() function takes two vectors and multiplies every element of the first vector by every element of the second vector. This creates a matrix where rows correspond to elements of the first vector and columns correspond to elements of the second vector. For example, with vectors A = [1, 2] and B = [3, 4], the outer product matrix is [[3, 4], [6, 8]]. Each step multiplies one pair of elements and fills the matrix. This is different from element-wise multiplication which multiplies elements at the same positions only. The output matrix shape is (length of A, length of B).