I have a numpy array a of shape [300, 3, 3], I like to change this array - [0.7, 0.3, 0.1] to [1, 1, 1], if occured in a
For example,
Input -
b = np.array([[[0.7, 0.3, 0.1], [0.6, 0.4, 0.2], [0.1, 0.2, 0.1]],
[[0.7, 0.3, 0.1], [0.6, 1.2, 2.1], [1.1, 2.1, 1.1]]
])
Output-
np.array([[[1, 1, 1], [0.6, 0.4, 0.2], [0.1, 0.2, 0.1]],
[[1, 1, 1], [0.6, 1.2, 2.1], [1.1, 2.1, 1.1]]
])
How to achieve this in easiest way, without use of looping, thanks
asked Jun 18, 2021 at 13:48
Karthiyayini Naga
1032 silver badges9 bronze badges
2 Answers 2
Use where method to replace elements of array.
b = np.where(b == [0.7, 0.3, 0.1], [1, 1, 1], b)
answered Jun 18, 2021 at 14:00
Aldrin Saurov Sarker
3333 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try this:
np.where(b == [0.7, 0.3, 0.1], [1, 1, 1], b)
answered Jun 18, 2021 at 13:56
dimay
2,8341 gold badge17 silver badges25 bronze badges
Comments
lang-py