numpy:数组中唯一值的最有效频率计数 [英] numpy: most efficient frequency counts for unique values in an array
本文介绍了numpy:数组中唯一值的最有效频率计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在numpy
/scipy
中,是否有一种有效的方法来获得数组中唯一值的频率计数?
In numpy
/ scipy
, is there an efficient way to get frequency counts for unique values in an array?
大致如下:
x = array( [1,1,1,2,2,2,5,25,1,1] )
y = freq_count( x )
print y
>> [[1, 5], [2,3], [5,1], [25,1]]
(对于你,R 用户,我基本上是在寻找 table()
函数)
( For you, R users out there, I'm basically looking for the table()
function )
推荐答案
看一看np.bincount
:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html
import numpy as np
x = np.array([1,1,1,2,2,2,5,25,1,1])
y = np.bincount(x)
ii = np.nonzero(y)[0]
然后:
zip(ii,y[ii])
# [(1, 5), (2, 3), (5, 1), (25, 1)]
或:
np.vstack((ii,y[ii])).T
# array([[ 1, 5],
[ 2, 3],
[ 5, 1],
[25, 1]])
或者您想结合计数和唯一值.
or however you want to combine the counts and the unique values.
这篇关于numpy:数组中唯一值的最有效频率计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文