Signature
np.clip(a, a_min, a_max)What it does
np.clip returns a new array where values below a_min become a_min and values above a_max become a_max. It is a compact way to winsorize features or keep predictions inside valid bounds.
Parameters
aarray_like- Values to limit.
a_minscalar or array_like- Lower bound, or None to leave the lower side alone.
a_maxscalar or array_like- Upper bound, or None to leave the upper side alone.
Returns
ndarray
Example
pythonnp.clip example
import numpy as np
scores = np.array([-3, 4, 9, 12])
print(np.clip(scores, 0, 10))Output
[ 0 4 9 10]
Watch out for
- Clipping hides extreme values instead of explaining them; inspect them first.
- Fit clipping bounds on training data only so test outliers stay honest.