numpyで要素を条件指定して変換する時は

np.whereが思い浮かぶけど、

  • 条件に合致したら置換
  • それ以外の要素はそのまま

というときはnp.whereではなく、np.placeの方が都合がいい

https://numpy.org/doc/stable/reference/generated/numpy.place.html

a = np.arange(9).reshape((3, 3))
print(a)

"""
[[0 1 2]
 [3 4 5]
 [6 7 8]]
"""

print(np.place(a, a < 4, 10))

"""
[[10 10 10]
 [10  4  5]
 [ 6  7  8]]
"""