[SciPy-user] Array selection help

josef.pktd@gmai... josef.pktd@gmai...
Wed Feb 11 12:23:42 CST 2009


Putting the array back together with means also seems to be pretty
fast this way. The test for correctness takes several times longer
than the array creation. I think labelmeanfilter below does what you
want.

Josef

timing again for large case:
>>> arr1.size
100000
>>> labelsunique.size
10000


mean observation per label 10.0
labelcoord2 0.391759008477
labelcoord3 0.253704311581
labelmeanfilter 0.446776056733

def labelmeanfilter(arr1, arr2):
    R = {}
    [R.setdefault(row[0],[]).append(index) for index, row in
                     enumerate(zip(arr1,arr2))]

    labelsunique = R.keys() #np.unique(arr1)
    labelmeans = ndimage.mean(arr2, labels=arr1, index=labelsunique)
    arr3 = np.zeros(arr1.shape)
    for k,v in zip(labelsunique,labelmeans):
        arr3[R[k]] = v
    return arr3

def test_labelmeanfilter(arr1, arr2):
    arr3b = labelmeanfilter(arr1, arr2)
    labmeandict = dict(zip(labelsunique,labelmeans))
    for orig,means in zip(arr1,arr3b):
        assert_array_equal(means, labmeandict[orig], repr(orig))



On 2/11/09, Jose Gomez-Dans <jgomezdans@gmail.com> wrote:
> Josef,
>
> Thanks for that
>
> 2009/2/11 <josef.pktd@gmail.com>
>
>> list comprehension is still a bit faster. That's about 90 times faster
>> than your version for building the dict of indices for this case.
>
>
> I still think ndimage is more obvious to use if you have images, and in my
> case it is fast (I didn't time it, but less than making a cup of horrible
> instant coffee ;p). My problem is that it takes a long time to go from a
> list/dictionary of class mean values (output from either Stéfan's ndimage
> solution or your dictionary solution) back into the original 2D array.
>
> In fact, I'm thinking about using weave to achieve this, unless someone
> comes up with a better idea.
>
> Many thanks for your help, and for the code!
> Jose
>
> --
> Remote Sensing Unit           | Env. Monitoring and Modelling Group
> Dept. of Geography            | Dept. of Geography
> University College London     | King's College London
> Gower St, London WC1E 6BT UK  | Strand Campus, Strand, London WC2R 2LS UK
>


More information about the SciPy-user mailing list