Challenge - 5 Problems
np.clip Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
np.clip() limits values below the minimum to the minimum, and above the maximum to the maximum.
✗ Incorrect
Values less than 5 become 5, values greater than 15 become 15, others stay the same.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Clipping does not change the shape of the array.
✗ Incorrect
np.clip returns an array of the same shape as input.
🔧 Debug
advanced1: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)
Attempts:
2 left
💡 Hint
np.clip requires both minimum and maximum values unless using keyword arguments.
✗ Incorrect
np.clip requires both a_min and a_max arguments unless specified by keywords.
🚀 Application
advanced2: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])
Attempts:
2 left
💡 Hint
np.clip needs minimum and maximum values in correct order.
✗ Incorrect
Option B correctly clips values below 0 to 0 and above 100 to 100.
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Clipping limits extreme values but does not change mean or variance directly.
✗ Incorrect
Clipping caps extreme values, which reduces the effect of outliers and can reduce skewness.