Dirty_Fox
Dirty_Fox

Reputation: 1751

Equivalent of count list function in numpy array

I have a matrix listScore with the shape (100000,2): I would like to count all the identical rows like. For instance, if listScore was a list of list I would simple do:

listScore.count([2,0])

to look for all the list equal to [2,0]. I could obviously transform the type of my listScore so that it would be a list but I want to keep to effectiveness of numpy. Is there any function I could use to do the same thing ?

Thanks in advance

Upvotes: 5

Views: 447

Answers (2)

Divakar
Divakar

Reputation: 221504

If listScore is a NumPy array, you could do -

count = np.all(listScore == np.array([2,0]),axis=1).sum()

If the array is always a 2 columns array, then you can compare the two columns separately with 2 and 0 respectively for performance and get the count like so -

count = ((listScore[:,0] ==2) & (listScore[:,1] ==0)).sum()

If you are a fan of np.einsum, you might like to try this twisted one -

count = (~np.einsum('ij->i',listScore != [2,0])).sum()

Another performance-oriented solution could be with cdist from scipy -

from scipy.spatial.distance import cdist
count = (cdist(listScore,np.atleast_2d([2,0]))==0).sum()

Upvotes: 4

tmdavison
tmdavison

Reputation: 69056

For a numpy.matrix, you can use:

(listScore==listScore[ind]).all(1).sum()

to find the number of rows matching the row with index ind.

or

(listScore==[2,0]).all(1).sum()

to match a specific pattern

Upvotes: 0

Related Questions