Numpy
Numpy (47)
알 수 없는 사용자
2022. 9. 13. 18:23
728x90
How to replace all values greater than a given value to a given cutoff?
# Input
np.set_printoptions(precision=2)
np.random.seed(100)
a = np.random.uniform(1,50, 20)
# 1: Using np.clip
np.clip(a, a_min=10, a_max=30)
# 2: Using np.where
print(np.where(a < 10, 10, np.where(a > 30, 30, a)))
# output
[ 27.63 14.64 21.8 30. 10. 10. 30. 30. 10. 29.18 30.
11.25 10.08 10. 11.77 30. 30. 10. 30. 14.43]