0
0
NumPydata~20 mins

np.concatenate() for joining arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.concatenate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[[1 2 3 4 5]]
B
[[1 2 3]
 [4 5]]
C
[1 2 3]
[4 5]
D[1 2 3 4 5]
Attempts:
2 left
💡 Hint
np.concatenate joins arrays along an existing axis. For 1D arrays, it joins elements end to end.
data_output
intermediate
2:00remaining
Shape after concatenating 2D arrays along axis 0
Given two 2D arrays:
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)
A(3, 2)
B(2, 3)
C(3, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
Concatenating along axis 0 stacks rows, so rows add up, columns stay same.
🔧 Debug
advanced
2: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)
AValueError: all the input array dimensions except for the concatenation axis must match exactly
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: all the input arrays must have same number of dimensions
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
Arrays must have matching shapes except along the concatenation axis.
🚀 Application
advanced
2: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)
A
[[1 2]
 [3 4]
 [5 6]]
B
[[1 2]
 [3 4]
 [5]
 [6]]
C
[[1 2 5]
 [3 4 6]]
D[[1 2 3 4 5 6]]
Attempts:
2 left
💡 Hint
Concatenating along axis 1 adds columns side by side.
🧠 Conceptual
expert
2:00remaining
Understanding axis parameter in np.concatenate()
Which statement best describes the role of the axis parameter in np.concatenate()?
AIt specifies the axis along which the arrays will be split before joining.
BIt specifies the axis along which the arrays will be joined, and all other dimensions must match exactly.
CIt determines the number of arrays that can be concatenated at once.
DIt controls whether the arrays are joined vertically or horizontally regardless of their shape.
Attempts:
2 left
💡 Hint
Think about how arrays are stacked and what must be the same for concatenation to work.