Issue
I wrote the following code but when the rows are large it is slow
import numpy as np
array = np.array([
[1,2],[1,2],[2,3],
[1,2],[2,3],[5,2]])
d={}
for l in array:
t = tuple(l)
if t in d:
d[t]+=1
else:
d[t]=1
print(d)
result:
`{(1, 2): 3, (2, 3): 2, (5, 2): 1}`
Is there a faster way to do this?
Solution
Use np.unique
elements, counts = np.unique(array, axis=0, return_counts=True)
In your case, elements
will be
[[1, 2],
[2, 3],
[5, 2]]
and counts
will be
[3, 2, 1]
Answered By - Alex P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.