gpt4 book ai didi

python - 如何从 scipy.optimize.leastsq 模块获取 RMSE

转载 作者:行者123 更新时间:2023-11-28 22:05:53 25 4
gpt4 key购买 nike

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

最佳答案

这是一个使用 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

关于python - 如何从 scipy.optimize.leastsq 模块获取 RMSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4520785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com