[SciPy-User] How to average different pieces or an array?

Joseph Smidt josephsmidt@gmail....
Fri Aug 7 13:06:38 CDT 2009


Thank you all.  Your suggestions were very helpful.

On Fri, Aug 7, 2009 at 10:59 AM, <josef.pktd@gmail.com> wrote:
> On Fri, Aug 7, 2009 at 1:38 PM, Borís BURLE<boris.burle@univ-provence.fr> wrote:
>> Hi,
>>
>> Or another version, close to Gilles' one:
>>
>> x = array([1,3,2,6,7,4,5,4,9,4])
>> y = [0,2,4,10]
>> z = x
>>
>>
>> for i in arange(len(y)-1):
>>    z[y[i]:y[i+1]] = mean(x[y[i]:y[i+1]])
>>
>>
>> hope this helps!
>>
>>     B.
>>
>> gilles Rochefort a écrit :
>>
>> Hi,
>>
>> Not sure to answer well to the question (indeed there is loops)  but
>> have you tried
>>  something like this :
>>
>> for s in [ slice(a,b) for a,b in zip(y[:-1],y[1:]) ]:
>>     x[s] = mean(x[s])
>>
>> Regards,
>> Gilles Rochefort.
>>
>>
>>       I'm sure this is easy I just can't think of how to do it without
>> a bunch of for loops which I would like to avoid since they take up so
>> much computational time.
>>
>> Say I have an array x, with 100 entries.   Lets say I have another
>> array y that looks like this:
>>
>> y = [0,5,10,20,40,100]  which specified which ranges of x I would like
>> to average.
>>
>> In other words, I want elements 0 - 4  in x to be replaced by their
>> average value. 5-9 replaced with their average value. 10-19 replaced
>> by their averaged value, etc...
>>
>>     Effectively this will give me a new array with one 100 entries
>> with different sized binning along the array.
>>
>>     If anyone knows how to do this without using for loops the whole
>> way I would appreciate it.
>>
>>      Example:  I will make it simpler in case my above description was
>> confusing.  Say x = [1,3,2,6,7,4,5,4,9,4] and I passed in y =
>> [0,2,4,10].  The above description would return a new X:
>>
>> X = [2,2,4,4,5.5,5.5,5.5,5.5,5.5,5.5]
>>
>>     So that the apprpriate bins are averaged.
>>
>>                                        Joseph Smidt
>>
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>>
>> --
>> Borís BURLE
>> Laboratoire de Neurobiologie de la Cognition
>> CNRS et Université de Provence
>> tel: (+33) 4 88 57 68 79
>> fax: (+33) 4 88 57 68 72
>> web page:  http://www.up.univ-mrs.fr/lnc/ACT/act-fr.html
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>
> Using the loop of Boris to build the reusable label/indicator array
> and bincount. labels may be slower because they don't assume that the
> array is sorted by label.
>
>>>> x = np.array([1,3,2,6,7,4,5,4,9,4])
>>>> y = np.array([0,2,4,10])
>>>> ind = np.empty(x.shape, int)
>>>> for i in np.arange(len(y)-1): ind[y[i]:y[i+1]] = i
>
>>>> means = np.bincount(ind,x)/np.bincount(ind)
>>>> meanarr = means[ind]
>>>> meanarr
> array([ 2. ,  2. ,  4. ,  4. ,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5])
>
> Josef
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



-- 
------------------------------------------------------------------------
Joseph Smidt <josephsmidt@gmail.com>

Physics and Astronomy
4129 Frederick Reines Hall
Irvine, CA 92697-4575
Office: 949-824-3269


More information about the SciPy-User mailing list