SciPy 的最小平方误差的相对平方和
我对模型拟合和 SciPy 比较陌生;对于任何无知提前表示歉意。
我正在尝试使用 scipy.optimize east_squares 拟合非线性模型。
这是函数:
def growthfunction(theta, t):
return (theta[0]*np.exp(-np.exp(-theta[1]*(t-theta[2]))))
和一些数据
t = [1, 2, 3, 4] observed = [3, 10, 14, 17]
我首先定义模型
def fun(theta):
return (myfunction(theta, ts) - observed)
选择一些随机起始参数来优化如下:
theta0 = [1, 1, 1]
然后我利用 leas_squares 来优化
res1 = least_squares(fun, theta0)
这非常有效,除了以下事实:最小二乘在这里优化绝对误差。我的数据随时间变化,这意味着时间点 1 处的 5 误差按比例大于时间点 100 处的 5 误差。我想更改此设置,以便优化相对误差。
我尝试手动执行此操作,但如果我像这样除以 fun(theta)
中的预测值:
def fun(theta):
return (myfunction(theta, ts) - observed)/myfunction(theta, ts)
least_squares
显示参数太多且无法优化的错误
I am relatively new to model fitting and SciPy; apologies in advance for any ignorance.
I am trying to fit a non-linear model using scipy.optimize least_squares.
Here's the function:
def growthfunction(theta, t):
return (theta[0]*np.exp(-np.exp(-theta[1]*(t-theta[2]))))
and some data
t = [1, 2, 3, 4]
observed = [3, 10, 14, 17]
I first define the model
def fun(theta):
return (myfunction(theta, ts) - observed)
Select some random starting parameters to be optimized below:
theta0 = [1, 1, 1]
Then I utilize leas_squares to optimize
res1 = least_squares(fun, theta0)
This works great, except for the fact that least_squares is here optimizing the absolute error. My data changes with time, meaning an error of 5 at time point 1 is proportionally larger than an error of 5 at time point 100. I would like to change this so that instead the relative error is optimized.
I tried doing it manually, but if I divide by the predicted values in fun(theta)
like so:
def fun(theta):
return (myfunction(theta, ts) - observed)/myfunction(theta, ts)
least_squares
displays an error that there are too many parameters and cannot optimize
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是通过获取相对误差来实现的:
输出:
This is working by taking the relative error:
Output:
如果没有 最小可重现示例,很难为您提供帮助,但您可以尝试相对最少的更传统版本正方形
,或者也许是为了防止小值/零值,
Without a minimal reproducible example it is very hard to help you, but you can try a more traditional version of relative least squares which is
or, perhaps, to guard against small/zero values,