[SciPy-user] Array selection help

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Feb 11 15:49:29 EST 2009


On Wed, Feb 11, 2009 at 3:08 PM, Stéfan van der Walt <stefan at sun.ac.za> wrote:
> 2009/2/11 Stéfan van der Walt <stefan at 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:
>>> ndimage.mean(5.0-np.arange(5), labels=np.arange(1,10,2), index=np.arange(10))
[0.0, 5.0, 0.0, 4.0, 0.0, 3.0, 0.0, 2.0, 0.0, 1.0]

So your translation table should work if you replace labels_unique by
range(max(labels)) in ndimage.mean(...).
I tried some basic example but I didn't really test it. This would
work then as long as labels are positive integers.

Josef

example:
>>> tt = ndimage.mean(5.0-np.arange(5), labels=np.arange(1,10,2), index=np.arange(10))
>>> tt
[0.0, 5.0, 0.0, 4.0, 0.0, 3.0, 0.0, 2.0, 0.0, 1.0]
>>> np.array(tt)[np.arange(1,10,2)]
array([ 5.,  4.,  3.,  2.,  1.])
>>>



More information about the SciPy-User mailing list