Skip to content

Instantly share code, notes, and snippets.

@SeanSyue
Last active May 20, 2018 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanSyue/55e350d5d31d59bbb8412b938ba66dd5 to your computer and use it in GitHub Desktop.
Save SeanSyue/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