Reputation: 192
I am using the following code to count the number of occurrences of a particular number in a numpy array, sort the dictionary in a descending order and then return it
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
function call
lc = labels(km_0)
However, it throws up the following error
<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'
Upvotes: 0
Views: 477
Reputation: 4681
Since the typo has already been addressed, I'll go another route. If you're looking specifically for numpy.array
, then you can use MooingRawr's solution using Counter
. However, to add a bit more performance, you can use a native Numpy counter such as 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))
Upvotes: 0
Reputation: 4991
Maybe instead of building your own counter you could employ from 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)])
I still don't know what j
is for yours I'm guessing it's a typo for i
Edited:
Above works for normal Arrays but for numpy use the follow using numpy's unique()
function call :
#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)))
For more information on numpy.unique see here.
Upvotes: 1