2

Reproducible code:

x = np.array([0.9, 0.9, 1])
y = np.array([0, 0, 1])
y[x<1] = 1 - x[x<1]
print(y)

Output

array([0, 0, 1])

Desired output

array([0.1, 0.1, 1])

I know np.where will get me the desired output. I'm just curious about why boolean indexing doesn't work in this case.

1 Answer 1

2

You must specify the dtype for y:

y = np.array([0, 0, 1], dtype=np.float64)

When you did not specify that, it assume integer as dtype. So each modification on this will be converted to original dtype of this array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.