0
0
NumPydata~10 mins

np.vstack() and np.hstack() 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 stack arrays vertically using numpy.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.[1]([arr1, arr2])
print(result)
Drag options to blanks, or click blank then click option'
Ahstack
Bvstack
Cconcatenate
Dstack
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.hstack() which stacks arrays horizontally.
Using np.concatenate() without specifying axis.
Using np.stack() which requires axis argument.
2fill in blank
medium

Complete the code to stack arrays horizontally using numpy.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.[1]([arr1, arr2])
print(result)
Drag options to blanks, or click blank then click option'
Aconcatenate
Bvstack
Chstack
Dstack
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.vstack() which stacks arrays vertically.
Using np.concatenate() without specifying axis.
Using np.stack() which requires axis argument.
3fill in blank
hard

Fix the error in the code to stack two 2D arrays vertically.

NumPy
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.[1]([arr1, arr2])
print(result)
Drag options to blanks, or click blank then click option'
Ahstack
Bconcatenate
Cstack
Dvstack
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.hstack() which requires matching row counts.
Using np.concatenate() without axis=0.
Using np.stack() without axis argument.
4fill in blank
hard

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

NumPy
words = ['cat', 'house', 'dog', 'elephant']
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) for value.
Using '<' instead of '>' in condition.
Not filtering words by length.
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 original keys without uppercase.
Filtering values less than or equal to zero.
Swapping keys and values in dictionary comprehension.