Find indices of elements equal to zero in a NumPy array

前端 未结 8 1326
孤城傲影
孤城傲影 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条回答
  •  Happy的楠姐
    2020-11-29 17:45

    There is np.argwhere,

    import numpy as np
    arr = np.array([[1,2,3], [0, 1, 0], [7, 0, 2]])
    np.argwhere(arr == 0)
    

    which returns all found indices as rows:

    array([[1, 0],    # Indices of the first zero
           [1, 2],    # Indices of the second zero
           [2, 1]],   # Indices of the third zero
          dtype=int64)
    

提交回复
热议问题