3 回答

TA贡献1796条经验 获得超10个赞
多维频率计数,即计数阵列。
>>> print(color_array )
array([[255, 128, 128],
[255, 128, 128],
[255, 128, 128],
...,
[255, 128, 128],
[255, 128, 128],
[255, 128, 128]], dtype=uint8)
>>> np.unique(color_array,return_counts=True,axis=0)
(array([[ 60, 151, 161],
[ 60, 155, 162],
[ 60, 159, 163],
[ 61, 143, 162],
[ 61, 147, 162],
[ 61, 162, 163],
[ 62, 166, 164],
[ 63, 137, 162],
[ 63, 169, 164],
array([ 1, 2, 2, 1, 4, 1, 1, 2,
3, 1, 1, 1, 2, 5, 2, 2,
898, 1, 1,

TA贡献1815条经验 获得超10个赞
更新:原始答案中提到的方法已弃用,我们应该使用新方法:
>>> import numpy as np>>> x = [1,1,1,2,2,2,5,25,1,1]>>> np.array(np.unique(x, return_counts=True)).T array([[ 1, 5], [ 2, 3], [ 5, 1], [25, 1]])
原始答案:
你可以使用scipy.stats.itemfreq
>>> 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.]])
添加回答
举报