Root mean square of a function in python

后端 未结 1 579
小鲜肉
小鲜肉 2021-02-07 18:47

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.

I tried Numpy and Scipy Docs and couldn\

1条回答
  •  甜味超标
    2021-02-07 19:26

    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.

    In numpy, you can simply square y, take its mean and then its square root as follows:

    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.

    0 讨论(0)
提交回复
热议问题