Challenge - 5 Problems
np.concatenate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.concatenate() 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]) result = np.concatenate((arr1, arr2)) print(result)
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5]) result = np.concatenate((arr1, arr2)) print(result)
Attempts:
2 left
💡 Hint
np.concatenate joins arrays along an existing axis. For 1D arrays, it joins elements end to end.
✗ Incorrect
np.concatenate((arr1, arr2)) joins the two 1D arrays into one longer 1D array: [1 2 3 4 5].
❓ data_output
intermediate2:00remaining
Shape after concatenating 2D arrays along axis 0
Given two 2D arrays:
What is the shape of the result?
arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6]]) result = np.concatenate((arr1, arr2), axis=0) print(result.shape)
What is the shape of the result?
NumPy
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6]]) result = np.concatenate((arr1, arr2), axis=0) print(result.shape)
Attempts:
2 left
💡 Hint
Concatenating along axis 0 stacks rows, so rows add up, columns stay same.
✗ Incorrect
arr1 has shape (2,2), arr2 has shape (1,2). Concatenating along axis 0 adds rows: 2+1=3 rows, columns remain 2.
🔧 Debug
advanced2:00remaining
Error when concatenating arrays with mismatched shapes
What error does this code raise?
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6, 7]]) result = np.concatenate((arr1, arr2), axis=0)
NumPy
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6, 7]]) result = np.concatenate((arr1, arr2), axis=0)
Attempts:
2 left
💡 Hint
Arrays must have matching shapes except along the concatenation axis.
✗ Incorrect
arr1 shape is (2,2), arr2 shape is (1,3). They differ in columns (axis 1), so concatenation along axis 0 fails with ValueError about dimension mismatch.
🚀 Application
advanced2:00remaining
Concatenate arrays along axis 1
What is the output of this code?
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5], [6]]) result = np.concatenate((arr1, arr2), axis=1) print(result)
NumPy
import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5], [6]]) result = np.concatenate((arr1, arr2), axis=1) print(result)
Attempts:
2 left
💡 Hint
Concatenating along axis 1 adds columns side by side.
✗ Incorrect
arr1 shape (2,2), arr2 shape (2,1). Concatenating along axis 1 adds columns, resulting in shape (2,3).
🧠 Conceptual
expert2:00remaining
Understanding axis parameter in np.concatenate()
Which statement best describes the role of the
axis parameter in np.concatenate()?Attempts:
2 left
💡 Hint
Think about how arrays are stacked and what must be the same for concatenation to work.
✗ Incorrect
The axis parameter tells np.concatenate which dimension to join along. All other dimensions must be the same size.