【发布时间】:2022-09-22 23:55:27
【问题描述】:
为了确定一段时间内的趋势,我使用scipy curve_fit 和来自time.time() 的X 值,例如1663847528.7147126(16 亿)。
进行线性插值有时会产生错误的结果,并且提供近似的初始 p0 值无济于事。我发现 X 的大小是这个错误的关键因素,我想知道为什么?
这是一个简单的 sn-p 显示工作和非工作 X 偏移量:
import scipy.optimize
def fit_func(x, a, b):
return a + b * x
y = list(range(5))
x = [1e8 + a for a in range(5)]
print(scipy.optimize.curve_fit(fit_func, x, y, p0=[-x[0], 0]))
# Result is correct:
# (array([-1.e+08, 1.e+00]), array([[ 0., -0.],
# [-0., 0.]]))
x = [1e9 + a for a in range(5)]
print(scipy.optimize.curve_fit(fit_func, x, y, p0=[-x[0], 0.0]))
# Result is not correct:
# OptimizeWarning: Covariance of the parameters could not be estimated
# warnings.warn(\'Covariance of the parameters could not be estimated\',
# (array([-4.53788811e+08, 4.53788812e-01]), array([[inf, inf],
# [inf, inf]]))
Almost perfect p0 for b removes the warning but still curve_fit doesn\'t work
print(scipy.optimize.curve_fit(fit_func, x, y, p0=[-x[0], 0.99]))
# Result is not correct:
# (array([-7.60846335e+10, 7.60846334e+01]), array([[-1.97051972e+19, 1.97051970e+10],
# [ 1.97051970e+10, -1.97051968e+01]]))
# ...but perfect p0 works
print(scipy.optimize.curve_fit(fit_func, x, y, p0=[-x[0], 1.0]))
#(array([-1.e+09, 1.e+00]), array([[inf, inf],
# [inf, inf]]))
作为一个附带问题,也许有一种更有效的线性拟合方法?不过,有时我想找到二阶多项式拟合。
在 Windows 10 下使用 Python 3.9.6 和 SciPy 1.7.1 进行测试。
-
拟合过程对比例敏感。规范化可能是您需要的。
标签: python scipy scipy-optimize