# [Numpy-discussion] what is the best way to do a statistical mode operation?

josef.pktd@gmai... josef.pktd@gmai...
Sun Oct 3 17:30:11 CDT 2010

```On Sun, Oct 3, 2010 at 5:56 PM, Gordon Wrigley <gordon@tolomea.com> wrote:
>> How many point do you have in np.unique(array) ?
> 43 currently
>
> On Sun, Oct 3, 2010 at 11:55 PM, <josef.pktd@gmail.com> wrote:
>>
>> On Sun, Oct 3, 2010 at 8:41 AM, Gordon Wrigley <gordon@tolomea.com> wrote:
>> > I have an array of uint8's that has a shape of X*Y*Z*8, I would like to
>> > calculate modes along the 8 axis so that I end up with an array that has
>> > the
>> > shape X*Y*Z and is full of modes.
>> > I'm having problems finding a good way of doing this. My attempts at
>> > solving
>> > this using bincount or histogram produce an intermediate array that is
>> > 32x
>> > the size of my input data and somewhat larger than I have the memory to
>> > deal
>> > with.
>> > Can anyone suggest a good way to produce modes over sets of 8 bytes?
>> > Also in the instance where there are multiple modes for a particular set
>> > I'm
>> > happy for it to pick any one arbitrarily.
>>
>> What's the range of integers? How many point do you have in
>> np.unique(array) ?
>>
>> bincount, for example, uses range(arr.max()+1) as set of points.
>>
>> looping over 8 values would be fast, but the intermediate arrays would
>> depend on the number of unique values.
>> I would try to use a dictionary.

My try, but no time for real testing

>>> import numpy as np
>>> a = np.random.randint(10,size=(5,4,8))
>>> auni = np.unique(a)
>>> mode = np.zeros(a.shape[:-1],int)
>>> idx = -1
>>> for k in np.unique(a):
idx += 1
count = (a==k).sum(-1)
mode[count>mode] = idx

>>> auni[mode]
array([[2, 2, 6, 8],
[4, 2, 4, 2],
[2, 2, 2, 4],
[7, 4, 4, 2],
[4, 1, 3, 2]])

Josef

>>
>> Josef
>>
>>
>> > G
>> >
>> >
>> > _______________________________________________
>> > NumPy-Discussion mailing list
>> > NumPy-Discussion@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> >
>> >
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
```