root mean square in numpy and complications of matrix and arrays of numpy

86,558

Solution 1

Try this:

U = np.zeros((N,N))
ind = 1
k = np.zeros(N)
k[:] = U[ind,:]

Solution 2

For the RMS, I think this is the clearest:

from numpy import mean, sqrt, square, arange
a = arange(10) # For example
rms = sqrt(mean(square(a)))

The code reads like you say it: "root-mean-square".

Solution 3

For rms, the fastest expression I have found for small x.size (~ 1024) and real x is:

def rms(x):
    return np.sqrt(x.dot(x)/x.size)

This seems to be around twice as fast as the linalg.norm version (ipython %timeit on a really old laptop).

If you want complex arrays handled more appropriately then this also would work:

def rms(x):
    return np.sqrt(np.vdot(x, x)/x.size)

However, this version is nearly as slow as the norm version and only works for flat arrays.

Solution 4

I don't know why it's not built in. I like

def rms(x, axis=None):
    return sqrt(mean(x**2, axis=axis))

If you have nans in your data, you can do

def nanrms(x, axis=None):
    return sqrt(nanmean(x**2, axis=axis))

Solution 5

For the RMS, how about

norm(V)/sqrt(V.size)
Share:
86,558

Related videos on Youtube

fedvasu
Author by

fedvasu

Updated on February 09, 2020

Comments

  • fedvasu
    fedvasu about 4 years

    Can anyone direct me to the section of numpy manual where i can get functions to accomplish root mean square calculations ... (i know this can be accomplished using np.mean and np.abs .. isn't there a built in ..if no why?? .. just curious ..no offense)

    can anyone explain the complications of matrix and arrays (just in the following case):

    U is a matrix(T-by-N,or u say T cross N) , Ue is another matrix(T-by-N) I define k as a numpy array

    U[ind,:] is still matrix

    in the following fashion k = np.array(U[ind,:])

    when I print k or type k in ipython

    it displays following

    K = array ([[2,.3 .....
                  ......
                    9]])
    

    You see the double square brackets (which makes it multi-dim i guess) which gives it the shape = (1,N)

    but I can't assign it to array defined in this way

    l = np.zeros(N)
    shape = (,N) or perhaps (N,) something like that
    
    l[:] = k[:]
    error:
    matrix dimensions incompatible
    

    Is there a way to accomplish the vector assignment which I intend to do ... Please don't tell me do this l = k (that defeats the purpose ... I get different errors in program .. I know the reasons ..If you need I may attach the piece of code)

    writing a loop is the dumb way .. which I'm using for the time being ...

    I hope I was able to explain .. the problems I'm facing ..

    regards ...

    • JoshAdel
      JoshAdel about 13 years
      In the future, please do not combine two questions together in the same post. It will make it easier for people to respond and for future users of the sight to find things.
    • JoshAdel
      JoshAdel about 13 years
      If you inspect the shape attribute of various arrays (e.g. K.shape or l[:].shape) you will see whether the dimensions of the arrays are incompatible, and it will give you clues about how you might correct the issue.
    • eat
      eat about 13 years
      Quite a verbose question, indeed. As pointed already, if you have two questions, ask two questions then. Anyway, just show your actual code and there's good change that you'll get constructive suggestion. Just, your current way to ask the (simple) questions makes them actually quite cumbersome. Thanks
    • JoshAdel
      JoshAdel about 13 years
      Could you please clarify exactly what type of RMS calculation you want to do (either by citing an equation or linking to the definition that you are using)?
  • JoshAdel
    JoshAdel about 13 years
    Small correction: Should be U = np.zeros((N,N)), otherwise you'll get an error.
  • fedvasu
    fedvasu about 13 years
    thanx highBandWidth and JoshAdel ...this should do ... can you suggest a refernce where i can see this kind of information .. (i'm looking into numpy for matlab users .. is that good enough?)
  • highBandWidth
    highBandWidth about 13 years
    I got this mainly through trying to use scipy/numpy.
  • dashesy
    dashesy over 9 years
    only if instead of **2 it was square it would be as good as it can get
  • dashesy
    dashesy over 9 years
    upvote because it is concise but norm is from np.linalg instead of directly from np and it does not have an optional axis argument, useful to make the rms function more general
  • deprecated
    deprecated over 9 years
    Excellent point @dashesy - thank you. I have edited accordingly.
  • Eric C.
    Eric C. over 9 years
    I often work with complex data, in that case square isn't enough. You need something like abs(a)**2
  • Eric C.
    Eric C. over 9 years
    I often work with complex data, in that case square isn't enough. You need something like abs(x)**2 instead of just x**2
  • deprecated
    deprecated over 9 years
    Indeed. Or you could do a*conj(a), which should be more efficient, although I haven't benchmarked it.
  • highBandWidth
    highBandWidth over 8 years
    @chwi, N is the dimension of the matrix you want. It is a number, like 5 if you want a 5x5 matrix.
  • Walter Nissen
    Walter Nissen almost 7 years
    That may be the most elegant formulation of a non-trivial algorithm I've ever seen. And it's useful, too! Bravo.
  • a_guest
    a_guest over 3 years
    @dashesy np.linalg.norm does have an axis parameters since version 1.8.0.
  • Rainb
    Rainb about 3 years
    doesn't np.std(a) do the same thing?
  • deprecated
    deprecated about 3 years
    @Rainb: No, it is not the same unless np.mean(a) == 0.0
  • Mauricio Arboleda
    Mauricio Arboleda almost 2 years
    Personally, I prefer avoiding importing each function from the numpy library. Consider using, import numpy as np, and then use np.mean, np.sqrt instead.

Related