【问题标题】:Root mean square error in python two functions [duplicate]python两个函数中的均方根误差[重复]
【发布时间】:2017-10-13 07:54:01
【问题描述】:

我有两个功能:

f(x) = sin(x)

g(x) = x - 1/6x^3

我有range(0, 2*PI),我想计算这两个函数的RMSE。我该怎么做?

【问题讨论】:

    标签: python


    【解决方案1】:

    有关 RMSE 的说明可在此处找到:

    Root mean square error in python

    在那里,它向您展示了如何从两个列表(或 numpy 数组)计算 RMSE。您需要指定您想要的目标值和预测值。

    下面是计算两个列表的建议代码,每个列表填充两个函数的结果,用于 0 到 2*PI 之间的值,增量为 0.1(注意纯 Python 范围函数不支持浮点类型)。

    import numpy as np
    
    def func1(x):
    
       return np.sin(x)
    
    def func2(x):
    
       return x - (1/6)*(x**3)
    
    l1 = []
    l2 = []
    
    for i in np.arange(0,2*np.pi,0.1):
    
       l1.append(func1(i))
       l2.append(func2(i))
    

    假设您在下面指定了一个新的预测列表 (l3),它以 0.1 为增量取 0 到 6.2 的值,分别比较 l3 和 l1(l3 到 l2)的 RMSE 值是:

    # Create new list of equal length for your predictions
    l3 = np.arange(0,2*np.pi,0.1)
    
    def rmse(predictions, targets):
        return np.sqrt(((predictions - targets) ** 2).mean())
    
    print(rmse(l3,l1))
    
    print(rmse(l3,l2))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-29
      • 2017-04-19
      • 1970-01-01
      • 2016-12-28
      • 2015-03-03
      • 2011-03-07
      • 2017-05-11
      • 2018-04-25
      相关资源
      最近更新 更多