1a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
2unique, counts = numpy.unique(a, return_counts=True)
3dict(zip(unique, counts))
4
5# {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
6
1>>> import numpy as np
2>>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
3
4>>> np.count_nonzero(y == 1)
51
6>>> np.count_nonzero(y == 2)
77
8>>> np.count_nonzero(y == 3)
93
10
1# credit to Stack Overflow user in source link
2# a is a numpy array
3# Note: the following syntax allows you to count the number of elements x
4# of the array such that 25 < x < 100
5((a > 25) & (a < 100)).sum()