Skip to content

Instantly share code, notes, and snippets.

@yuchen-xue
Last active May 20, 2018 15:32
Show Gist options
  • Save yuchen-xue/55e350d5d31d59bbb8412b938ba66dd5 to your computer and use it in GitHub Desktop.
Save yuchen-xue/55e350d5d31d59bbb8412b938ba66dd5 to your computer and use it in GitHub Desktop.
Count the occurrence of certain item in an ndarray in Python. Ref: https://stackoverflow.com/a/28663910/8809592
import numpy as np
a = np.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
print(dict(zip(unique, counts)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment