0
0
NumPydata~20 mins

outer() for outer operations in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Outer Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy.outer with simple vectors
What is the output of the following code using numpy.outer?
NumPy
import numpy as np

x = np.array([1, 2])
y = np.array([3, 4])
result = np.outer(x, y)
print(result)
A
[[3 4]
 [6 8]]
B
[[3 6]
 [4 8]]
C
[[4 3]
 [8 6]]
D
[[1 2]
 [3 4]]
Attempts:
2 left
💡 Hint
Remember that np.outer multiplies each element of the first array by each element of the second array.
data_output
intermediate
1:30remaining
Shape of the output from np.outer with 1D arrays
Given two 1D numpy arrays a with shape (4,) and b with shape (3,), what is the shape of np.outer(a, b)?
NumPy
import numpy as np

a = np.arange(4)
b = np.arange(3)
result = np.outer(a, b)
print(result.shape)
A(3, 3)
B(4, 3)
C(4, 4)
D(3, 4)
Attempts:
2 left
💡 Hint
The outer product shape is (len(a), len(b)).
visualization
advanced
2:30remaining
Visualizing the outer product matrix
Which option shows the correct heatmap visualization of np.outer([1, 2, 3], [4, 5])?
NumPy
import numpy as np
import matplotlib.pyplot as plt

matrix = np.outer([1, 2, 3], [4, 5])
plt.imshow(matrix, cmap='viridis')
plt.colorbar()
plt.show()
AA 3x2 heatmap with values [[4, 5], [8, 10], [12, 15]] increasing top to bottom and left to right
BA 2x3 heatmap with values [[4, 8, 12], [5, 10, 15]] increasing left to right and top to bottom
CA 3x2 heatmap with values [[1, 2], [3, 4], [5, 6]] increasing top to bottom and left to right
DA 2x3 heatmap with values [[1, 3, 5], [2, 4, 6]] increasing left to right and top to bottom
Attempts:
2 left
💡 Hint
Check the shape and values of the outer product matrix before visualizing.
🧠 Conceptual
advanced
2:00remaining
Understanding the difference between np.outer and np.dot
Which statement correctly describes the difference between np.outer and np.dot when applied to 1D numpy arrays?
A<code>np.outer</code> and <code>np.dot</code> produce the same output for 1D arrays.
B<code>np.outer</code> performs element-wise multiplication, while <code>np.dot</code> performs matrix multiplication.
C<code>np.outer</code> produces a scalar, while <code>np.dot</code> produces a matrix of pairwise products.
D<code>np.outer</code> produces a matrix of all pairwise products, while <code>np.dot</code> produces a single scalar sum of element-wise products.
Attempts:
2 left
💡 Hint
Think about the shapes of the outputs and what each function computes.
🔧 Debug
expert
2:30remaining
Identify the error in using np.outer with 2D arrays
What error will the following code produce and why?
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
result = np.outer(x, y)
print(result.shape)
NumPy
import numpy as np

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
result = np.outer(x, y)
print(result.shape)
AValueError due to shape mismatch between 2D arrays
BTypeError because np.outer only accepts 1D arrays
CNo error; output shape is (4, 4) because inputs are flattened before outer product
DAttributeError because np.outer is not defined for numpy arrays
Attempts:
2 left
💡 Hint
Check how np.outer handles multi-dimensional arrays internally.