Data PreprocessingIntro
Clip Features to a Range
~8 mincode completion
Implement clip_features(X, low, high) that clips all values in X to the range [low, high].
Examples
Clip negatives and values above 10
- Input
- clip_features([-5, 0, 3, 7, 12], 0, 10)
- Output
- [0, 0, 3, 7, 10]
Float bounds
- Input
- clip_features([1.5, 2.5, 3.5], 2, 3)
- Output
- [2, 2.5, 3]
Clip large absolute values
- Input
- clip_features([100, 50, 0, -50, -100], -30, 30)
- Output
- [30, 30, 0, -30, -30]
Hints
Hint 1
bounds an array in one call.
Hint 2
Reach for clip rather than mask.
Requirements
X: NumPy array (any shape)low: Minimum allowed valuehigh: Maximum allowed valueReturn Array of same shape with values in [low, high].
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 min
••••••••••••
6 employers weight this skill
2 AI product companies, 2 health and bio companies, 1 defense company, 1 enterprise vendor. Top match scores 75.
Python
import numpy as np
def clip_features(X: np.ndarray, low: float, high: float) -> np.ndarray:
"""
Clip all values in X to the range [low, high].
Args:
X: NumPy array (any shape)
low: Minimum allowed value
high: Maximum allowed value
Returns:
Array of same shape with values in [low, high].
"""
# YOUR CODE HERE
pass