Mattijn
Mattijn

Reputation: 13930

How to get the frequency of a specific value in a numpy array

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?

Upvotes: 2

Views: 4644

Answers (3)

Koustav Ghosal
Koustav Ghosal

Reputation: 504

You can use the 'Counter' from collections module.

from collections import Counter
import numpy as np
my_array=np.asarray(10*np.random.random((10,10)),'int')
my_dict=Counter()
print '\n The Original array \n ', my_array

for i in my_array:
    my_dict=my_dict+Counter(i)

print '\n The Counts \n', my_dict

The O/P is like this

The Original array [[6 8 3 7 6 9 2 2 3 2] [7 0 1 1 8 0 8 2 6 3] [0 4 0 1 8 7 6 1 1 1] [9 2 9 2 5 9 9 6 6 7] [5 1 1 0 3 0 2 7 6 2] [6 5 9 6 4 7 5 4 8 0] [7 0 8 7 1 8 5 1 3 2] [6 7 7 0 8 3 6 5 6 6] [0 7 1 6 1 2 7 8 4 1] [0 8 6 7 1 7 3 3 8 8]]

The Counts Counter ({6: 15, 1: 14, 7: 14, 8: 12, 0: 11, 2: 10, 3: 8, 5: 6, 9: 6, 4: 4})

You can try the most_common() method which gives the most common entries If you want the occurrence of a specific element just access just like dictionary.

Example: my_dict[6] will give 15 for the above code

Upvotes: 0

Bi Rico
Bi Rico

Reputation: 25833

bincount returns an array where the frequency of x is bincount[x], It requires a flat input so you can use bincount(array.ravel()) to handle cases when array might not be flat.

If your array only has a few unique values, ie 2 and 127, it might be worth reducing the array using unique before calling bincount ie:

import numpy as np
def frequency(array):
    values, array = np.unique(array, return_inverse=True)
    return values, bincount(array.ravel())

array = np.array([[2, 2, 2],
                  [127, 127, 127],
                  [2, 2, 2]])
frequency(array)
# array([  2, 127]), array([6, 3])

Lastly you can do

np.sum(array == 12)

Notice the difference between array == 12 and np.where(array == 12):

array = np.array([12, 0, 0, 12])
array == 12
# array([ True, False, False,  True], dtype=bool)
np.where(array == 12)
#(array([0, 3]),)

Clearly summing over the second is not going to give you what you want.

Upvotes: 1

Nils Werner
Nils Werner

Reputation: 36849

You want the number of occurrences of a simple number in your data array? Try

np.bincount(data)[number]

Upvotes: 2

Related Questions