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

后端 未结 6 736
猫巷女王i
猫巷女王i 2021-02-07 05:38

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

相关标签:
6条回答
  • 2021-02-07 05:43

    I use this for RMS, all using NumPy, and let it also have an optional axis similar to other NumPy functions:

    import numpy as np   
    rms = lambda V, axis=None: np.sqrt(np.mean(np.square(V), axis))
    
    0 讨论(0)
  • 2021-02-07 05:44

    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.

    0 讨论(0)
  • 2021-02-07 05:46

    Try this:

    U = np.zeros((N,N))
    ind = 1
    k = np.zeros(N)
    k[:] = U[ind,:]
    
    0 讨论(0)
  • 2021-02-07 05:50

    For the RMS, how about

    norm(V)/sqrt(V.size)
    
    0 讨论(0)
  • 2021-02-07 06:00

    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))
    
    0 讨论(0)
  • 2021-02-07 06:09

    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".

    0 讨论(0)
提交回复
热议问题