python中函数的均方根 [英] Root mean square of a function in python
本文介绍了python中函数的均方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算Python中函数的均方根.我的函数采用y = f(x)的简单形式. x和y是数组.
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.
我尝试了 Numpy和Scipy文档,但找不到任何东西.
I tried Numpy and Scipy Docs and couldn't find anything.
推荐答案
我将假定您要计算以下伪代码给出的表达式:
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)
即y
元素的平方值的平均值的平方根.
i.e. the square root of the mean of the squared values of elements of y
.
在numpy中,您可以简单地将y
平方,取其均值,然后平方根如下:
In numpy, you can simply square y
, take its mean and then its square root as follows:
rms = np.sqrt(np.mean(y**2))
例如,
>>> 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.
这篇关于python中函数的均方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文