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

后端 未结 6 734
猫巷女王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: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.

提交回复
热议问题