I've got a numpy array with shape 1001, 2663. Array contains values of 12 and 127, now I would like to count the number of a specific value, in this case 12. So I try using bincount, but that's doing strange. See what I get:
>>> x.shape
(1001, 2663)
>>> np.bincount(x)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: object too deep for desired array
>>> y = np.reshape(x, 2665663)
>>> y.shape
(2665663,)
>>> np.bincount(y)
array([ 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 529750, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 2135913])
>>> np.nonzero(np.bincount(y))
(array([ 12, 127]),)
The value 529750 is probably the frequency of the values 12 and 2135913 is probably the frequency of value 127, but it won't tell me this. Also the shape of the matrix is strange.
If I try sum with where also wont give me right value:
>>> np.sum(np.where(x==12))
907804649
I'm out of options: dear prestigious uses of SO, how to get the frequency of a specific value in a numpy matrix?
EDIT
Smaller example. But still get results that I don't really understand. Why the zero?
>>> m = np.array([[1,1,2],[2,1,1],[2,1,2]])
>>> np.bincount(m)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: object too deep for desired array
>>> n = np.reshape(m, 9)
>>> n
array([1, 1, 2, 2, 1, 1, 2, 1, 2])
>>> np.bincount(n)
array([0, 5, 4])
I think I get it. The zero in [0,5,4] means there are no 0 values in matrix. So in the my real situation, the 529750 is the 12th value in the matrix, matrix value 0-11 are all '0', than get's lots of 0 values (values 13-126) and then value 127 gives frequency of 2135913. But how to get the frequency as single value of a specific number in a numpy array?