0
0
NumPydata~10 mins

np.clip() for bounding values 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 clip the values in the array to a minimum of 0.

NumPy
import numpy as np
arr = np.array([-5, 0, 5, 10])
clipped = np.clip(arr, [1], None)
print(clipped)
Drag options to blanks, or click blank then click option'
A-1
B10
C5
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using None as minimum value causes error.
Using a positive number greater than 0 clips too many values.
2fill in blank
medium

Complete the code to clip the values in the array between 2 and 8.

NumPy
import numpy as np
arr = np.array([1, 3, 5, 9])
clipped = np.clip(arr, [1], 8)
print(clipped)
Drag options to blanks, or click blank then click option'
A2
B0
C5
D9
Attempts:
3 left
💡 Hint
Common Mistakes
Setting minimum to 0 clips too many values.
Setting minimum higher than 2 clips values incorrectly.
3fill in blank
hard

Fix the error in the code to clip values between 1 and 4.

NumPy
import numpy as np
arr = np.array([0, 2, 5, 7])
clipped = np.clip(arr, 1, [1])
print(clipped)
Drag options to blanks, or click blank then click option'
A7
B4
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a maximum bound larger than 4.
Using a minimum bound larger than maximum.
4fill in blank
hard

Fill both blanks to clip values below 10 to 10 and above 20 to 20.

NumPy
import numpy as np
arr = np.array([5, 10, 15, 25])
clipped = np.clip(arr, [1], [2])
print(clipped)
Drag options to blanks, or click blank then click option'
A10
B15
C20
D25
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping minimum and maximum values.
Using values outside the specified range.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and their lengths clipped to max 4.

NumPy
words = ['apple', 'bat', 'cat', 'dolphin']
lengths = {word: len(word) if len(word) < [1] else [2] for word in words if len(word) > [3]
print(lengths)
Drag options to blanks, or click blank then click option'
A3
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent max length values.
Filtering words incorrectly.