我使用以下代码计算numpy数组中特定数字的出现次数,按降序对字典进行排序然后返回
km_0 = [indian,chinese,italian,mexican,indian,indian,chinese,italian] #numpy array
#The ord_dict should be like this {indian:3, chinese:2, italian:2, mexican:1}
def labels(cluster):
label_count ={}
for i in cluster[0]:
if i in label_count:
label_count[i] += 1
else:
label_count[i] =1
ord_dict = OrderedDict(sorted(label_count.items(), key=lambda kv:kv[1], reverse=True))
return ord_dict
函数调用
lc = labels(km_0)
但是,它会引发以下错误
<ipython-input-8-72f0a128bdd4> in labels(cluster)
9 label_count ={}
10 for i in cluster[0]:
---> 11 if i in label_count:
12 label_count[i] += 1
13 else:
TypeError: unhashable type: 'list'
答案 0 :(得分:1)
也许您可以使用collection
Counter
from collections import Counter, OrderedDict
x = "hello world"
print(OrderedDict(sorted(Counter(x).items(), key=lambda t: t[1], reverse=True)))
#prints OrderedDict([('l', 3), ('o', 2), (' ', 1), ('e', 1), ('d', 1), ('h', 1), ('r', 1), ('w', 1)])
我仍然不知道j
对你来说是什么我猜它是i
编辑:
以上适用于普通数组,但对于numpy使用numpy的unique()
函数调用:
#replace array_name with like your `i`
unique, counts = numpy.unique(array_name, return_counts=True)
#Then zip them together to make a dictionary
counted = dict(zip(unique, counts))
#then toss it into OrderedDict
print(OrderedDict(sorted(counted.items(), key=lambda t: t[1], reverse=True)))
有关numpy.unique see here.
的更多信息答案 1 :(得分:0)
由于错字已经得到解决,我将走另一条路。如果您正在寻找numpy.array
,那么您可以使用Counter
来使用MooingRawr的解决方案。但是,要添加更多性能,可以使用本地Numpy计数器,例如count_nonzero
。
import numpy as np
def cnt(arr):
counts = {i: np.count_nonzero(arr == i) for i in range(arr.min(), arr.max() + 1)}
return OrderedDict(sorted(counts.items(), key=lambda x: x[1], reverse=True))
x = np.random.random_integers(50, size=100)
y = np.random.random_integers(50, size=(10, 10))