Root Mean Square In Numpy And Complications Of Matrix And Arrays Of Numpy
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
Solution 1:
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 2:
For rms, the fastest expression I have found for small x.size
(~ 1024) and real x
is:
defrms(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:
defrms(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 3:
For the RMS, how about
norm(V)/sqrt(V.size)
Solution 4:
I don't know why it's not built in. I like
defrms(x, axis=None):
return sqrt(mean(x**2, axis=axis))
If you have nans in your data, you can do
defnanrms(x, axis=None):
return sqrt(nanmean(x**2, axis=axis))
Solution 5:
Try this:
U = np.zeros((N,N))
ind = 1
k = np.zeros(N)
k[:] = U[ind,:]
Post a Comment for "Root Mean Square In Numpy And Complications Of Matrix And Arrays Of Numpy"