Find indices of elements equal to zero in a NumPy array

前端 未结 8 1316
孤城傲影
孤城傲影 2020-11-29 17:28

NumPy has the efficient function/method nonzero() to identify the indices of non-zero elements in an ndarray object. What is the most efficient way to obtain th

8条回答
  •  迷失自我
    2020-11-29 17:52

    You can search for any scalar condition with:

    >>> a = np.asarray([0,1,2,3,4])
    >>> a == 0 # or whatver
    array([ True, False, False, False, False], dtype=bool)
    

    Which will give back the array as an boolean mask of the condition.

提交回复
热议问题