Challenge - 5 Problems
Stacking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.vstack() with 1D arrays
What is the output of this code?
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = np.vstack((arr1, arr2)) print(result)
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = np.vstack((arr1, arr2)) print(result)
Attempts:
2 left
💡 Hint
np.vstack stacks arrays vertically, adding rows.
✗ Incorrect
np.vstack stacks arrays vertically, so two 1D arrays become two rows in a 2D array.
❓ Predict Output
intermediate2:00remaining
Output of np.hstack() with 2D arrays
What is the output of this code?
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) result = np.hstack((arr1, arr2)) print(result)
NumPy
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) result = np.hstack((arr1, arr2)) print(result)
Attempts:
2 left
💡 Hint
np.hstack stacks arrays horizontally, adding columns.
✗ Incorrect
np.hstack joins arrays side by side, so columns are added horizontally.
❓ data_output
advanced2:00remaining
Shape after stacking arrays with np.vstack and np.hstack
Given these arrays:
What is the shape of the result after applying np.vstack((arr1, arr2)) and np.hstack((arr1, arr2)) respectively?
import numpy as np arr1 = np.array([[1, 2, 3]]) arr2 = np.array([[4, 5, 6]])
What is the shape of the result after applying np.vstack((arr1, arr2)) and np.hstack((arr1, arr2)) respectively?
NumPy
import numpy as np arr1 = np.array([[1, 2, 3]]) arr2 = np.array([[4, 5, 6]]) vstack_result = np.vstack((arr1, arr2)) hstack_result = np.hstack((arr1, arr2)) print(vstack_result.shape, hstack_result.shape)
Attempts:
2 left
💡 Hint
vstack adds rows, hstack adds columns.
✗ Incorrect
vstack stacks arrays vertically increasing rows; hstack stacks horizontally increasing columns.
🔧 Debug
advanced2:00remaining
Error when stacking arrays with incompatible shapes
What error does this code raise?
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[4, 5, 6]]) result = np.vstack((arr1, arr2))
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[4, 5, 6]]) result = np.vstack((arr1, arr2))
Attempts:
2 left
💡 Hint
np.vstack requires arrays to have the same number of dimensions.
✗ Incorrect
vstack requires all arrays to have the same number of dimensions; mixing 1D and 2D arrays causes ValueError.
🚀 Application
expert3:00remaining
Combining arrays for data analysis
You have two numpy arrays representing data from two sensors:
You want to create a single array where sensor2 data is stacked below sensor1 data, and then another array where sensor2 data is stacked to the right of sensor1 data.
Which code correctly creates these two arrays?
sensor1 = np.array([[10, 20], [30, 40]]) sensor2 = np.array([[50, 60], [70, 80]])
You want to create a single array where sensor2 data is stacked below sensor1 data, and then another array where sensor2 data is stacked to the right of sensor1 data.
Which code correctly creates these two arrays?
NumPy
import numpy as np sensor1 = np.array([[10, 20], [30, 40]]) sensor2 = np.array([[50, 60], [70, 80]])
Attempts:
2 left
💡 Hint
axis=0 stacks rows (vertical), axis=1 stacks columns (horizontal).
✗ Incorrect
np.vstack is equivalent to np.concatenate with axis=0; np.hstack is equivalent to np.concatenate with axis=1.