I have a 2d numpy array of integers, and now I want to change all the elements greater than 5 to 5.
For example,
[[2, 6],
[7, 3]]
to
[[2, 5],
[5, 3]]
Now, my current approach is to access all the elements using two for loops and then checking if each element is greater then 5, like this:
h, w = arr.shape[:2]
for x in range(h):
for y in range(w):
if arr[x,y] > 5:
arr[x,y] = 5
Is there any more pythonic approach for doing this?