0
0
NumPydata~10 mins

np.concatenate() for joining arrays in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to join two numpy arrays along the default axis.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.concatenate((arr1, arr2), axis=[1])
print(result)
Drag options to blanks, or click blank then click option'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 causes an error because 1D arrays have only one axis (0).
Forgetting to put the arrays inside a tuple in np.concatenate.
2fill in blank
medium

Complete the code to join two 2D numpy arrays vertically.

NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr1, arr2), axis=[1])
print(result)
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 stacks arrays horizontally (side by side), not vertically.
Using axis=2 causes an error because 2D arrays have only axes 0 and 1.
3fill in blank
hard

Fix the error in the code to concatenate two arrays along columns.

NumPy
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr1, arr2), axis=[1])
print(result)
Drag options to blanks, or click blank then click option'
A1
B-1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 stacks arrays vertically, not horizontally.
Using axis=2 causes an error because 2D arrays have only axes 0 and 1.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) as dictionary value.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

NumPy
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys.
Using '<' instead of '>' in the condition.
Using k instead of v for values.