python中函数的均方根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40963659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Root mean square of a function in python
提问by Praveen
I want to calculate root mean square of a function in Python. My function is in a simple form like y = f(x). x and y are arrays.
我想在 Python 中计算函数的均方根。我的函数是一种简单的形式,如 y = f(x)。x 和 y 是数组。
I tried Numpy and Scipy Docsand couldn't find anything.
我尝试了Numpy 和 Scipy Docs,但找不到任何东西。
回答by Praveen
I'm going to assume that you want to compute the expression given by the following pseudocode:
我将假设您要计算以下伪代码给出的表达式:
ms = 0
for i = 1 ... N
ms = ms + y[i]^2
ms = ms / N
rms = sqrt(ms)
i.e. the square root of the mean of the squared values of elements of y
.
即元素的平方值的均值的平方根y
。
In numpy, you can simply square y
, take its meanand then its square rootas follows:
在 numpy 中,您可以简单地平方y
,取其均值,然后取其平方根,如下所示:
rms = np.sqrt(np.mean(y**2))
So, for example:
因此,例如:
>>> y = np.array([0, 0, 1, 1, 0, 1, 0, 1, 1, 1]) # Six 1's
>>> y.size
10
>>> np.mean(y**2)
0.59999999999999998
>>> np.sqrt(np.mean(y**2))
0.7745966692414834
Do clarify your question if you mean to ask something else.
如果您想问其他问题,请澄清您的问题。