Python 如何获取numpy数组中特定值的频率

Python 如何获取numpy数组中特定值的频率,python,numpy,Python,Numpy,我有一个1001263形状的numpy阵列。数组包含12和127的值,现在我想计算一个特定值的数目,在本例中为12。所以我尝试使用bincount,但这很奇怪。看看我得到了什么: >>> x.shape (1001, 2663) >>> np.bincount(x) Traceback (most recent call last): File "<interactive input>", line 1, in <module> V

我有一个1001263形状的numpy阵列。数组包含12和127的值,现在我想计算一个特定值的数目,在本例中为12。所以我尝试使用bincount,但这很奇怪。看看我得到了什么:

>>> x.shape
(1001, 2663)
>>> np.bincount(x)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: object too deep for desired array
>>> y = np.reshape(x, 2665663)
>>> y.shape
(2665663,)
>>> np.bincount(y)
array([      0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,  529750,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0,       0,       0,       0,       0,       0,       0,
             0, 2135913])
>>> np.nonzero(np.bincount(y))
(array([ 12, 127]),)
我已经没有选择了:亲爱的,那么,如何获得numpy矩阵中特定值的频率呢

编辑

较小的例子。但还是得到了我不太理解的结果。为什么是零

>>> m = np.array([[1,1,2],[2,1,1],[2,1,2]])
>>> np.bincount(m)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: object too deep for desired array
>>> n = np.reshape(m, 9)
>>> n
array([1, 1, 2, 2, 1, 1, 2, 1, 2])
>>> np.bincount(n)
array([0, 5, 4])
>m=np.数组([[1,1,2],[2,1,1],[2,1,2]]
>>>np.bincount(m)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:对象对于所需数组太深
>>>n=np。重塑(m,9)
>>>n
数组([1,1,2,2,1,1,2,1,2])
>>>np.bincount(n)
数组([0,5,4])

我想我明白了。[0,5,4]中的零表示矩阵中没有0值。所以在我的实际情况中,529750是矩阵中的第12个值,矩阵值0-11都是“0”,而get的很多0值(值13-126)和127给出的频率是2135913。但是如何将频率作为numpy数组中特定数字的单个值来获取呢?

您想知道一个简单的
数字在
数据
数组中出现的次数吗?试一试

np.bincount(data)[number]

bincount
返回一个数组,其中
x
的频率为
bincount[x]
,它需要一个平坦的输入,因此您可以使用
bincount(array.ravel())
来处理
array
可能不平坦的情况

如果您的数组只有几个唯一值,即2和127,那么在调用bincount ie之前使用unique减少数组可能是值得的:

import numpy as np
def frequency(array):
    values, array = np.unique(array, return_inverse=True)
    return values, bincount(array.ravel())

array = np.array([[2, 2, 2],
                  [127, 127, 127],
                  [2, 2, 2]])
frequency(array)
# array([  2, 127]), array([6, 3])
最后你能做什么

np.sum(array == 12)
注意
array==12
np之间的区别。其中(array==12)


显然,第二次求和并不能得到您想要的结果。

您可以使用“收集”模块中的“计数器”

from collections import Counter
import numpy as np
my_array=np.asarray(10*np.random.random((10,10)),'int')
my_dict=Counter()
print '\n The Original array \n ', my_array

for i in my_array:
    my_dict=my_dict+Counter(i)

print '\n The Counts \n', my_dict
O/p是这样的

原始数组 [[6 8 3 7 6 9 2 2 3 2] [7 0 1 1 8 0 8 2 6 3] [0 4 0 1 8 7 6 1 1 1] [9 2 9 2 5 9 9 6 6 7] [5 1 1 0 3 0 2 7 6 2] [6 5 9 6 4 7 5 4 8 0] [7 0 8 7 1 8 5 1 3 2] [6 7 7 0 8 3 6 5 6 6] [0 7 1 6 1 2 7 8 4 1] [086717388]]

伯爵 计数器({6:15,1:14,7:14,8:12,0:11,2:10,3:8,5:6,9:6,4:4})

您可以尝试使用most_common() 如果您想要特定元素的出现,只需像字典一样访问即可


示例:my_dict[6]将给出上述代码的15

我建议尝试使用一个较小的(可手动操作)示例,以了解发生了什么。这也是我自己刚刚发现的。谢谢你的回复!但这需要我首先将矩阵重塑为一维数组。这当然不是问题。只要是我的电脑,而不是我。
array = np.array([12, 0, 0, 12])
array == 12
# array([ True, False, False,  True], dtype=bool)
np.where(array == 12)
#(array([0, 3]),)
from collections import Counter
import numpy as np
my_array=np.asarray(10*np.random.random((10,10)),'int')
my_dict=Counter()
print '\n The Original array \n ', my_array

for i in my_array:
    my_dict=my_dict+Counter(i)

print '\n The Counts \n', my_dict