【发布时间】:2017-09-19 07:53:22
【问题描述】:
我正在尝试使用scipy.optimize.curvefit 将一组数据与一个函数(参见下面的示例)相匹配,
但是当我使用边界(documentation)时,拟合失败了,我只是得到
初始猜测参数作为输出。
一旦我将-np.inf 广告np.inf 替换为第二个参数的界限
(函数中的dt),适合的作品。
我做错了什么?
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt
#Generate data
crc=np.array([-1.4e-14, 7.3e-14, 1.9e-13, 3.9e-13, 6.e-13, 8.0e-13, 9.2e-13, 9.9e-13,
1.e-12, 1.e-12, 1.e-12, 1.0e-12, 1.1e-12, 1.1e-12, 1.1e-12, 1.0e-12, 1.1e-12])
time=np.array([0., 368., 648., 960., 1520.,1864., 2248., 2655., 3031.,
3384., 3688., 4048., 4680., 5343., 6055., 6928., 8120.])
#Define the function for the fit
def testcurve(x, Dp, dt):
k = -Dp*(x+dt)*2e11
curve = 1e-12 * (1+2*(-np.exp(k) + np.exp(4*k) - np.exp(9*k) + np.exp(16*k)))
curve[0]= 0
return curve
#Set fit bounds
dtmax=time[2]
param_bounds = ((-np.inf, -dtmax),(np.inf, dtmax))
#Perform fit
(par, par_cov) = opt.curve_fit(testcurve, time, crc, p0 = (5e-15, 0), bounds = param_bounds)
#Print and plot output
print(par)
plt.plot(time, crc, 'o')
plt.plot(time, testcurve(time, par[0], par[1]), 'r-')
plt.show()
【问题讨论】:
-
没有inf时你的界限是什么?
-
他们就是例子中的那些,+/-
time[2],即-648.和648.但是我也试过手动输入不同的值,还是不行。跨度> -
我也有同样的问题,没有界限,我所有的拟合都失败了(使用curve_fit、least_squares,甚至最小化和直接定义的损失函数),但是有界限,它们都失败了。跨度>