How to find most frequent values in numpy ndarray?

前端 未结 5 2162
栀梦
栀梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 06:33

    A slightly better solution in my opinion is the following

    tmpL = np.array([3, 2, 3, 2, 5, 2, 2, 3, 3, 2, 2, 2, 3, 3, 2, 2, 3, 2, 3, 2])
    unique, counts = np.unique(tmpL, return_counts=True)
    return unique[np.argmax(counts)]
    

    Using np.unique we can get the count of each unique elements. The index of the max element in counts will be the corresponding element in unique.

提交回复
热议问题