Find indices of elements equal to zero in a NumPy array

前端 未结 8 1339
孤城傲影
孤城傲影 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:49

    import numpy as np
    
    x = np.array([1,0,2,3,6])
    non_zero_arr = np.extract(x>0,x)
    
    min_index = np.amin(non_zero_arr)
    min_value = np.argmin(non_zero_arr)
    

提交回复
热议问题