Difference between nonzero(a), where(a) and argwhere(a). When to use which?

nonzero and argwhere both give you information about where in the array the elements are True. where works the same as nonzero in the form you have posted, but it has a second form:

np.where(mask,a,b)

which can be roughly thought of as a numpy “ufunc” version of the conditional expression:

a[i] if mask[i] else b[i]

(with appropriate broadcasting of a and b).

As far as having both nonzero and argwhere, they’re conceptually different. nonzero is structured to return an object which can be used for indexing. This can be lighter-weight than creating an entire boolean mask if the 0’s are sparse:

mask = a == 0  # entire array of bools
mask = np.nonzero(a)

Now you can use that mask to index other arrays, etc. However, as it is, it’s not very nice conceptually to figure out which indices correspond to 0 elements. That’s where argwhere comes in.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)