如何计算numpy数组中元素的频率? [英] How to count frequency of a element in numpy array?

查看:1458
本文介绍了如何计算numpy数组中元素的频率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D numpy数组,其中包含重复的元素. counterTraj.shape (13530, 1, 1 例如counterTraj包含以下元素:我仅显示了几个元素:

I have a 3 D numpy array which contains elements with repetition. counterTraj.shape (13530, 1, 1 For example counterTraj contains such elements: I have shown few elements only:

         array([[[136.]],

       [[129.]],

       [[130.]],

       ...,

       [[103.]],

       [[102.]],

       [[101.]]])
```

我需要找到不同元素的频率:示例:136计数5(例如),101计数12(例如).数组元素不是固定的,而是随输入数据而变化.我尝试以下操作: from collections import Counter Counter(counterTraj) 产生以下错误:

I need to find frequency of different element: Example: 136 count 5 (say), 101 count 12 (say). The array elements are not fixed and changes with input data. I try following: from collections import Counter Counter(counterTraj) Following error generates:

> TypeError                                 Traceback (most recent 
    call last)
    <ipython-input-408-e3584b29b0bd> in <module>()
         11 counterTraj=np.vstack(counterTraj)
        12 counterTraj=counterTraj.reshape(len(counterTraj),1,1)
    ---> 13 Counter(counterTraj)

    /usr/lib/python3.6/collections/__init__.py in __init__(*args, 
   **kwds)
         533             raise TypeError('expected at most 1 arguments, 
      got %d' % len(args))
          534         super(Counter, self).__init__()
      --> 535         self.update(*args, **kwds)
         536 
       537     def __missing__(self, key):

     /usr/lib/python3.6/collections/__init__.py in update(*args, 
    **kwds)
         620                     super(Counter, self).update(iterable) # 
     fast path when counter is empty
         621             else:
      --> 622                 _count_elements(self, iterable)
       623         if kwds:
       624             self.update(kwds)

     TypeError: unhashable type: 'numpy.ndarray'

如何找到具有频率的元素的出现并找到频率最高的元素?

How to find occurence of element with frequency and find highest frequency element?

推荐答案

使用return_counts=True参数的nofollow noreferrer> numpy.unique ,它将返回数组中每个元素的计数.

Use numpy.unique with return_counts=True parameter, which will return the count of each of the elements in the array.

# sample array
In [89]: np.random.seed(23)
In [90]: arr = np.random.randint(0, 10, 20)

In [92]: a, cnts = np.unique(arr, return_counts=True)
In [94]: high_freq, high_freq_element = cnts.max(), a[cnts.argmax()]

In [95]: high_freq, high_freq_element
Out[95]: (4, 9)


仅选择出现在某个频率阈值以上的元素,可以使用:


For selecting only the elements which appear above a certain frequency threshold, you can use:

In [96]: threshold = 2

# select elements which occur only more than 2 times
In [97]: a[cnts > threshold]
Out[97]: array([3, 5, 6, 9])

这篇关于如何计算numpy数组中元素的频率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆