>>> from scipy.stats import itemfreq
>>> x = [1,1,1,2,2,2,5,25,1,1]
>>> itemfreq(x)
/usr/local/bin/python:1: DeprecationWarning: `itemfreq` is deprecated! `itemfreq` is deprecated and will be removed in a future version. Use instead `np.unique(..., return_counts=True)`
array([[ 1., 5.],
[ 2., 3.],
[ 5., 1.],
[ 25., 1.]])
import numpy as np
x = np.array([1,1,1,2,2,2,5,25,1,1])
unique, counts = np.unique(x, return_counts=True)
print np.asarray((unique, counts)).T
它给出:
[[ 1 5]
[ 2 3]
[ 5 1]
[25 1]]
与scipy.stats.itemfreq的快速比较:
In [4]: x = np.random.random_integers(0,100,1e6)
In [5]: %timeit unique, counts = np.unique(x, return_counts=True)
10 loops, best of 3: 31.5 ms per loop
In [6]: %timeit scipy.stats.itemfreq(x)
10 loops, best of 3: 170 ms per loop
看看
np.bincount
:http://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html
然后:
或:
或者您希望将计数和唯一值组合起来。
更新:原答案中提到的方法已弃用,我们应改用新方法:
原始答案:
你可以使用scipy.stats.itemfreq
从Numpy 1.9开始,最简单、最快的方法是简单地使用^{} ,它现在有一个
return_counts
关键字参数:它给出:
与
scipy.stats.itemfreq
的快速比较:相关问题
PyPI热门下载资源包