numpy的:在阵列中的唯一值的最有效的频数 [英] numpy: most efficient frequency counts for unique values in an array

查看:1034
本文介绍了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用户在那里,我基本上找表()功能)

推荐答案

看看 np.bincount

<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html\">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屋!

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