我正在拟合一条有三个点的非常简单的曲线。使用leastsq方法,遵循所有规则。但我还是犯了个错误。我不明白。有人能帮忙吗。非常感谢
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x = np.array([2.0,30.2,15.0])
y = np.array([45.0,56.2,30.0])
print(x)
print(y)
# model
def t(x,a,b,c):
return a*x**2 + b*x + c
#residual fucntion
def residual_t(x,y,a,b,c):
return y-t(x,a,b,c)
#initial parameters
g0 = np.array([0.0,0.0,0.0])
#leastsq method
coeffs, cov = leastsq(residual_t, g0, args=(x,y))
plt.plot(x,t(x,*coeffs),'r')
plt.plot(x,y,'b')
plt.show()
#finding out Rsquared and Radj squared value
absError = residual_t(y,x,*coeffs)
se = np.square(absError) # squared errors
Rsquared = 1.0 - (np.var(absError) / np.var(y))
n = len(x)
k = len(coeffs)
Radj_sq = (1-((1-Rsquared)/(n-1)))/(n-k-1)
print (f'Rsquared value: {Rsquared} adjusted R saquared value: {Radj_sq}')
TypeError:剩余的\u t()缺少2个必需的位置参数:“b”和“c”
为什么?? coeffs已经是包含a、b、c的最佳it值的数组。系数也显示未定义,剩余系数也显示问题。你能帮我理解一下吗
通过复制粘贴代码(包括
*coeffs
更改),我可以也就是说,在
leastsq
调用中使用residual_t
是错误的如果我加上
在
g0
定义之后,我得到了相同的错误:所以您需要定义
residual_t
来处理这样的调用。我不想猜测你到底想要什么,所以我把问题留给你解决请记住,
residual_t
将用x0
调用,并用args
元组拼接。这是scipy.optimize
函数的典型用法。必要时检查文件编辑
将函数定义为:
运行无错误
相关问题 更多 >
编程相关推荐