fmonegaglia
fmonegaglia

Reputation: 2819

Difference between python numpy's where and boolean array

I have two numpy arrays: myarray and mask, which are both bitewise arrays(1s and 0s only).

What is the difference between

myarray[mask] = 0

and

myarray = np.where( mask, 0, myarray )

? Because I get different results and can't figure out why.

Upvotes: 2

Views: 323

Answers (1)

John Zwinck
John Zwinck

Reputation: 249223

Since you say mask contains 1's and 0's, the problem is that NumPy treats these as indexes, not as a mask. You probably want to make mask be of boolean type (True/False), in which case it can be the same length as myarray and will select those elements where mask is True.

np.where() always treats the first argument as a boolean array, so it probably does what you want already.

Upvotes: 3

Related Questions