root mean square in numpy and complications of matrix and arrays of numpy
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)
Related videos on Youtube
fedvasu
Updated on February 09, 2020Comments
-
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 definek
as a numpy arrayU[ind,:]
is still matrixin the following fashion
k = np.array(U[ind,:])
when I print
k
or typek
in ipythonit 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 about 13 yearsIn 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 about 13 yearsIf you inspect the shape attribute of various arrays (e.g.
K.shape
orl[:].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 about 13 yearsQuite 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 about 13 yearsCould 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 about 13 yearsSmall correction: Should be
U = np.zeros((N,N))
, otherwise you'll get an error. -
fedvasu about 13 yearsthanx 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 about 13 yearsI got this mainly through trying to use scipy/numpy.
-
dashesy over 9 yearsonly if instead of **2 it was
square
it would be as good as it can get -
dashesy over 9 yearsupvote because it is concise but norm is from
np.linalg
instead of directly fromnp
and it does not have an optionalaxis
argument, useful to make the rms function more general -
deprecated over 9 yearsExcellent point @dashesy - thank you. I have edited accordingly.
-
Eric C. over 9 yearsI often work with complex data, in that case square isn't enough. You need something like
abs(a)**2
-
Eric C. over 9 yearsI often work with complex data, in that case square isn't enough. You need something like
abs(x)**2
instead of justx**2
-
deprecated over 9 yearsIndeed. Or you could do
a*conj(a)
, which should be more efficient, although I haven't benchmarked it. -
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 almost 7 yearsThat may be the most elegant formulation of a non-trivial algorithm I've ever seen. And it's useful, too! Bravo.
-
a_guest over 3 years@dashesy
np.linalg.norm
does have anaxis
parameters since version 1.8.0. -
Rainb about 3 yearsdoesn't
np.std(a)
do the same thing? -
deprecated about 3 years@Rainb: No, it is not the same unless
np.mean(a) == 0.0
-
Mauricio Arboleda almost 2 yearsPersonally, I prefer avoiding importing each function from the numpy library. Consider using, import numpy as np, and then use np.mean, np.sqrt instead.