5

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

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Very complete answer ! Thank you very much !
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.