How to find most frequent values in numpy ndarray?

前端 未结 5 2154
栀梦
栀梦 2020-12-15 05:38

I have a numpy ndarray with shape of (30,480,640), the 1th and 2th axis representing locations(latitude and longitute), the 0th axis contains actual data points.I want to us

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 06:15

    Explaining @ecatmurs part

    u[np.argmax(np.apply_along_axis(np.bincount, axis, indices.reshape(arr.shape),
                                    None, np.max(indices) + 1), axis=axis)]
    

    a little bit more and restructuring it to be more concise when re-reading it (because I used this solution and after a few weeks I was wondering what had happened in this function):

    axis = 0
    uniques, indices = np.unique(arr, return_inverse=True)
    
    args_for_bincount_fn = None, np.max(indices) + 1
    binned_indices = np.apply_along_axis(np.bincount,
                                last_axis, 
                                indices.reshape(arr.shape),
                                *args_for_bincount_fn)
    
    most_common = uniques[np.argmax(binned_indices,axis=axis)]
    

提交回复
热议问题