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?

cs95
  • 379,657
  • 97
  • 704
  • 746
user297850
  • 7,705
  • 17
  • 54
  • 76
  • 1
    If you want the *count* of each unique element, you can use `from collections import Counter` then `print(Counter(a))` – rafaelc May 13 '18 at 02:12

1 Answers1

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].

cs95
  • 379,657
  • 97
  • 704
  • 746