[numarray] mean vector of 2d matrix

Discussion in 'Python' started by Curzio Basso, May 10, 2004.

  1. Curzio Basso

    Curzio Basso Guest

    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
     
    Curzio Basso, May 10, 2004
    #1
    1. Advertising

  2. Curzio Basso

    Guest

    Curzio Basso <> wrote in message news:<409f9edd$>...
    > 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.]
     
    , May 11, 2004
    #2
    1. Advertising

  3. Curzio Basso

    Curzio Basso Guest

    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!
     
    Curzio Basso, May 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    24,125
  2. Christian Seberino
    Replies:
    4
    Views:
    661
    Christian Seberino
    Apr 27, 2004
  3. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
  4. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    436
    Holgerson
    Oct 26, 2007
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,284
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page