Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   [numarray] mean vector of 2d matrix (http://www.velocityreviews.com/forums/t330753-numarray-mean-vector-of-2d-matrix.html)

Curzio Basso 05-10-2004 03:25 PM

[numarray] mean vector of 2d matrix
 
Hi all,

I was wondering what is the best way to compute the mean vector of a
matrix. Currently I'm doing this:

>>> data.shape

(117027, 8)
>>> mean = (numarray.add.reduce(data, axis=1))/(data.shape[1])


but maybe there is a faster way?

cheers

beliavsky@aol.com 05-11-2004 01:53 PM

Re: [numarray] mean vector of 2d matrix
 
Curzio Basso <curzio.basso@unibas.ch> wrote in message news:<409f9edd$1@maser.urz.unibas.ch>...
> Hi all,
>
> I was wondering what is the best way to compute the mean vector of a
> matrix. Currently I'm doing this:


With Numeric, one can use the 'average' function to compute the average along
an axis, or the average of all elements when the axis argument is set to None.
See p317 of Martelli's "Python in a Nutshell" book. For example, the code

from Numeric import zeros,Float,average
nr = 3
nc = 2
xx = zeros([nr,nc],Float)
for i in range(nr):
for j in range(nc):
xx[i,j] = i + 10.0*j
print xx
for i in [None,0,1]: print "\n",i,average(xx,axis=i)

produces output

[[ 0. 10.]
[ 1. 11.]
[ 2. 12.]]

None 6.0

0 [ 1. 11.]

1 [ 5. 6. 7.]

Curzio Basso 05-12-2004 07:52 AM

Re: [numarray] mean vector of 2d matrix
 
beliavsky@aol.com wrote:

> With Numeric, one can use the 'average' function to compute the average along
> an axis, or the average of all elements when the axis argument is set to None.
> See p317 of Martelli's "Python in a Nutshell" book. For example, the code


However, it looks like in numarray no average() is provided. Now I found
out there is an average() function provided by the MA (Masked Arrays)
module, but I do not really understand why there is an average()
function for a masked array and not for a generic array...

Anyway, thanks for the tip!


All times are GMT. The time now is 09:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57