1

Given an numpy array a, I can use np.unique(a) to generate the unique values included in a. How can I get the count of each unique values included in a?

1
  • 1
    If you want the count of each unique element, you can use from collections import Counter then print(Counter(a)) Commented May 13, 2018 at 2:12

1 Answer 1

3

Since you mentioned np.unique, it accepts a second argument called return_counts:

u, c = np.unique(a, return_counts=True)

c[i] is the corresponding count for u[i].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.