【问题标题】:How to get RMSE from scipy.optimize.leastsq module如何从 scipy.optimize.leastsq 模块获取 RMSE
【发布时间】:2010-12-23 16:53:27
【问题描述】:

我可以从 scipy.optimize.leastsq 模块中获取 RMSE 的价值吗?

【问题讨论】:

    标签: python numpy scipy


    【解决方案1】:

    这是一个使用leastsq的小例子:

    import numpy as np
    import scipy.optimize as optimize
    import collections
    
    x = np.array([821,576,473,377,326,300])
    y = np.array([255,235,208,166,157,140])
    
    def sigmoid(p,x):
        x0,y0,c,k=p
        y = c / (1 + np.exp(-k*(x-x0))) + y0
        return y
    
    def residuals(p,x,y):
        return y - sigmoid(p,x)
    
    Param=collections.namedtuple('Param','x0 y0 c k')
    p_guess=Param(x0=600,y0=200,c=100,k=0.01)
    p,cov,infodict,mesg,ier = optimize.leastsq(
        residuals,p_guess,args=(x,y),full_output=1,warning=True)
    p=Param(*p)
    xp = np.linspace(100, 1600, 1500)
    print('''\
    x0 = {p.x0}
    y0 = {p.y0}
    c = {p.c}
    k = {p.k}
    '''.format(p=p))
    

    你可以这样计算残差:

    resid=residuals(p,x,y)
    print(resid)
    # [ 0.76205302 -2.010142    2.60265297 -3.02849144  1.6739274 ]
    

    但您不必计算 resid -- infodict['fvec'] 已经包含信息。

    print(infodict['fvec'])
    # [ 0.76205302 -2.010142    2.60265297 -3.02849144  1.6739274 ]
    
    chisq=(infodict['fvec']**2).sum()
    # dof is degrees of freedom
    dof=len(x)-len(p)
    rmse=np.sqrt(chisq/dof)
    print(rmse)
    # 5.40092057562
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2020-10-04
      • 2016-09-30
      • 1970-01-01
      • 2021-07-24
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多