[Tutor] normalize an array

John washakie at gmail.com
Sat Nov 27 19:41:41 CET 2010


Thank you both! Broadcasting is a concept I hadn't yet read about, but
knew was important for efficient python programming... thanks for the
link!


On Sat, Nov 27, 2010 at 6:44 PM, Eike Welk <eike.welk at gmx.net> wrote:
> Hello John!
>
> On Friday 26.11.2010 23:23:51 Peter Otten wrote:
>> John wrote:
>> > I know this is a simple problem, but I want to do it the most
>> > efficient way (that is vectorized...)
>> >
>> > import numpy as np
>> >
>> > a = np.array(([1,2,3,4],[1,.2,3,4],[1,22,3,4]))
>> > b = np.sum(a,axis=1)
>> >
>> > for i,elem in enumerate(a):
>> >     a[i,:] = elem/b[i]
>> >
>> > suggestions?
>>
>> I'm not a numpy expert, but:
>>
>> (a.transpose()/np.sum(a, axis=1)).transpose()
>
> The underlying feature of Peter's solution is called broadcasting. For a
> detailed explanation look at:
> http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
>
>
> Eike.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Configuration
``````````````````````````
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Python 2.6
PIL 1.1.6
Mailman 2.1.9
Postfix 2.4.5
Procmail v3.22 2001/09/10
Basemap: 1.0
Matplotlib: 1.0.0


More information about the Tutor mailing list