0
0
NumPydata~20 mins

np.clip() for bounding values in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.clip Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.clip() with 1D array
What is the output of this code snippet using np.clip()?
NumPy
import numpy as np
arr = np.array([1, 5, 10, 15, 20])
result = np.clip(arr, 5, 15)
print(result)
A[ 1 5 10 15 15]
B[ 1 5 10 15 20]
C[ 5 5 10 10 15]
D[ 5 5 10 15 15]
Attempts:
2 left
💡 Hint
np.clip() limits values below the minimum to the minimum, and above the maximum to the maximum.
data_output
intermediate
1:30remaining
Resulting array shape after clipping 2D array
Given this 2D array and clipping operation, what is the shape of the resulting array?
NumPy
import numpy as np
arr = np.array([[2, 8, 12], [20, 5, 0]])
clipped = np.clip(arr, 3, 10)
print(clipped.shape)
A(2, 3)
B(3, 2)
C(6,)
D(3,)
Attempts:
2 left
💡 Hint
Clipping does not change the shape of the array.
🔧 Debug
advanced
1:30remaining
Identify the error in np.clip usage
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.clip(arr, 5)
print(result)
ATypeError: clip() missing 1 required positional argument: 'a_max'
BSyntaxError: invalid syntax
CValueError: min cannot be greater than max
DNo error, outputs [5 5 5]
Attempts:
2 left
💡 Hint
np.clip requires both minimum and maximum values unless using keyword arguments.
🚀 Application
advanced
2:00remaining
Using np.clip to limit sensor readings
You have sensor readings in an array. You want to limit all values below 0 to 0 and above 100 to 100. Which code correctly does this?
NumPy
import numpy as np
sensor_data = np.array([-10, 20, 150, 50, 0])
Anp.clip(sensor_data, 100, 0)
Bnp.clip(sensor_data, 0, 100)
Cnp.clip(sensor_data, min=0, max=100)
Dnp.clip(sensor_data, a_min=0)
Attempts:
2 left
💡 Hint
np.clip needs minimum and maximum values in correct order.
🧠 Conceptual
expert
2:30remaining
Effect of np.clip on data distribution
If you apply np.clip() to a large dataset to limit values between the 5th and 95th percentile, what is the main effect on the data distribution?
AIt randomly samples 5% of data from both ends.
BIt normalizes the data to have mean 0 and standard deviation 1.
CIt removes outliers by capping extreme values, reducing skewness.
DIt increases the variance by stretching values beyond original range.
Attempts:
2 left
💡 Hint
Clipping limits extreme values but does not change mean or variance directly.