【发布时间】:2017-09-22 15:10:10
【问题描述】:
假设我有一个由t 参数化的模型f。我想要t 的最佳值,以使∑ₓ (f(x, t) - y(x))² 最小化。这就是最小二乘优化的目的。
在下面的例子中
from numpy import *
from scipy.optimize import curve_fit
x = arange(100)
t_true = 30
y = 1. / (1 + exp(-(x - t_true) / 5.))
f = lambda x, t: [0. if xi < t else 1. for xi in x]
t_opt, t_cor = curve_fit(f, x, y, p0=(20.))
plot(x, y)
plot(x, f(x, t_opt))
print(t_cor)
为什么我得到t_opt=20 而不是接近t_opt=30?
另外,为什么是t_cor=inf?我得到的结果是:
其中蓝色是数据,绿色是拟合模型,但我希望看起来像这样:
我确实预料到了这一点,因为第二张图像的残差平方和肯定小于第一张图像的平方和,并且显然没有局部最小值,优化可能会卡住。那么为什么这不起作用呢?
【问题讨论】:
-
你想找到x的最佳值(比如xopt),它给出了距离(y(x)-xopt)的最小误差?
-
@SaulloCastro 不,我的模型是函数
f,它由t参数化。我想要t的最佳值,使∑ₓ (f(x, t) - y(x))²最小化
标签: python scipy least-squares