# [SciPy-user] Array selection help

josef.pktd@gmai... josef.pktd@gmai...
Wed Feb 11 15:31:31 CST 2009

```On Wed, Feb 11, 2009 at 3:49 PM,  <josef.pktd@gmail.com> wrote:
> On Wed, Feb 11, 2009 at 3:08 PM, Stéfan van der Walt <stefan@sun.ac.za> wrote:
>> 2009/2/11 Stéfan van der Walt <stefan@sun.ac.za>:
>>> In [55]: means[[1,1,0,1,0,0]]
>>> Out[55]: array([ 0.2,  0.2,  0.1,  0.2,  0.1,  0.1])
>>>
>>> I implemented a solution using such a "translation table" (see attached).
>>
>> Note that, for this approach to work, the labels must progress in
>> increments of one from 0 to N.  So labels 0, 1, 2, 3 are fine, but 0,
>> 5, 10 are not.
>
> I just checked that ndimage can handle non-existing labels:

translation table version, another 10 times faster
makes some missing labels
>>> np.unique(arr1).shape
(9998,)
>>> np.unique(arr1)[:10]
array([ 0,  1,  2,  3,  4,  7,  8,  9, 10, 11])

labelmeanfilter 0.383765171272
labelmeanfilter1 0.0916504471937
labelmeanfilter2 0.377427047292

labelmeanfilter3 0.00886087477598    # version with translation table
with missing labels

>>> np.all(arr3_3 == arr3_0)
True

def labelmeanfilter3(arr1, arr2):
# requires integer labels
labelsunique = np.arange(np.max(arr1)+1)
labelmeans = np.array(ndimage.mean(arr2, labels=arr1, index=labelsunique))
arr3 = labelmeans[arr1]
return arr3

I think we started out with more than 20 seconds.

Josef
```