0

I have a numpy array

How can I find which of them are the same and how many times appear in the matrix? thanks dummy example:

A=np.array([[0, 1, 0, 1],[0, 0, 0, 0],[0, 1, 1, 1],[0, 0, 0, 0]])
1
  • What have you tried so far? Commented Dec 29, 2021 at 14:41

1 Answer 1

1

You can use numpy.unique with axis=0 and return_counts=True:

np.unique(A, axis=0, return_counts=True)

Output:

(array([[0, 0, 0, 0],
        [0, 1, 0, 1],
        [0, 1, 1, 1]]),
 array([2, 1, 1]))
Sign up to request clarification or add additional context in comments.

2 Comments

As an aside, If the number of rows is substantially lager than number of columns, pandas.DataFrame.value_counts can be faster as it avoids sorting rows.
i tried it, it works!! thank you very much!!

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.