0

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
0

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

np.where(b == [0.7, 0.3, 0.1], [1, 1, 1], b)
answered Jun 18, 2021 at 13:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.