Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find the counts of unique binary arrays in a list of arrays?

Tags:

python

numpy

If I had a numpy array in the form of

[[0. 1. 1. 1. 1.],
[1. 0. 0. 0. 0.], 
[1. 0. 0. 0. 0.],
[1. 0. 0. 0. 0.],
[1. 0. 0. 0. 0.],
[0. 1. 1. 1. 1.]]

is there a way to determine the frequency of these binary arrays?

Using the example listed above, the frequencies would be something like [1.0.0.0.0] - 4, [0.1.1.1.1] - 2. I've tried using np.unique, but that returns the counts of just unique numbers which isn't super helpful in this case.

like image 512
febreeze Avatar asked Jan 28 '26 01:01

febreeze


1 Answers

from collections import Counter

counts = Counter(map(tuple, arr))

map(tuple, arr) converts each row of the array to a tuple which is hashable and thus can be stored in a mapping like Counter.

like image 171
Alex Hall Avatar answered Jan 29 '26 15:01

Alex Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!