scipy curve_fit 有约束和固定点
scipy curve_fit with constraint and fixing points
我正在尝试使用 SciPy 的 optimize.curve_fit
将函数拟合到一些分散的数据,但我需要拟合曲线下的面积与基于分散的数据,并且曲线通过数据的起点和终点。为此,我在 this answer, while weighing the fit with the parameter sigma
as proposed here.
中使用由分散数据定义的区域(积分)作为惩罚公式
不幸的是,当包含积分约束时,我无法让我的拟合通过初始点和终点。如果我忽略积分约束,则拟合工作正常并通过该点。是不是不能同时满足积分约束和点约束?我在 Windows 10.
上使用 Python 3.7.10
import scipy
import numpy as np
import matplotlib.pyplot as plt
x = scipy.linspace(0, scipy.pi, 100)
y = scipy.sin(x) + (0. + scipy.rand(len(x))*0.4)
def Func(x,a,b,c):
return a*x**2 + b*x + c
# modified function definition with penalization
def FuncPen(x,a,b,c):
integral = scipy.integrate.quad(Func, x[0], x[-1], args=(a,b,c))[0]
penalization = abs(np.trapz(y,x)-integral)*10000
return a*x**2 + b*x + c + penalization
sigma = np.ones(len(x))
sigma[[0, -1]] = 0.0001 # first and last points
popt1, _ = scipy.optimize.curve_fit(Func, x, y, sigma=sigma)
popt2, _ = scipy.optimize.curve_fit(FuncPen, x, y, sigma=sigma)
y_fit1 = Func(x, *popt1)
y_fit2 = Func(x, *popt2)
fig, ax = plt.subplots(1)
ax.scatter(x,y)
ax.plot(x,y_fit1, color='g', alpha=0.75, label='curve_fit')
ax.plot(x,y_fit2, color='b', alpha=0.75, label='constrained')
plt.legend()
非常感谢 Erwin Kalvelagen 对这个问题的开阔的评论。我在这里发布我的解决方案:
import scipy
import numpy as np
import matplotlib.pyplot as plt
x = scipy.linspace(0, scipy.pi, 100)
y = scipy.sin(x) + (0. + scipy.rand(len(x))*0.4)
def Func(x,a,b,c):
return a*x**2 + b*x + c
# modified function definition with penalization
def FuncPen(x,a,b,c):
integral = scipy.integrate.quad(Func, x[0], x[-1], args=(a,b,c))[0]
penalization = abs(np.trapz(y,x)-integral)*10000
return a*x**2 + b*x + c + penalization
# Writing as a general constraint problem
def FuncNew(x,params):
return params[2]*x**2 + params[1]*x + params[0]
def ConstraintIntegral(params):
integral = integr.quad(FuncNew, x[0], x[-1], args=(params,))[0]
return integral- np.trapz(y,x)
def ConstraintBegin(params):
return y[0] - FuncNew(x[0],params)
def ConstraintEnd(params):
return y[-1] - FuncNew(x[-1],params)
def Objective(params,x,y):
y_pred = FuncNew(x,params)
return np.sum((y_pred - y) ** 2) # least squares
cons = [{'type':'eq', 'fun': ConstraintIntegral},
{'type':'eq', 'fun': ConstraintBegin},
{'type':'eq', 'fun': ConstraintEnd}]
new = scipy.optimize.minimize(Objective, x0=popt1, args=(x,y), constraints=cons)
popt3 = new.x
y_fit3 = FuncNew(x,popt3)
#####
sigma = np.ones(len(x))
sigma[[0, -1]] = 0.0001 # first and last points
popt1, _ = scipy.optimize.curve_fit(Func, x, y, sigma=sigma)
popt2, _ = scipy.optimize.curve_fit(FuncPen, x, y, sigma=sigma)
y_fit1 = Func(x, *popt1)
y_fit2 = Func(x, *popt2)
fig, ax = plt.subplots(1)
ax.scatter(x,y)
ax.plot(x,y_fit1, color='g', alpha=0.75, label='curve_fit')
ax.plot(x,y_fit2, color='b', alpha=0.75, label='constrained')
ax.plot(x,y_fit3, color='r', alpha=0.75, label='generally constrained')
plt.legend()
我正在尝试使用 SciPy 的 optimize.curve_fit
将函数拟合到一些分散的数据,但我需要拟合曲线下的面积与基于分散的数据,并且曲线通过数据的起点和终点。为此,我在 this answer, while weighing the fit with the parameter sigma
as proposed here.
不幸的是,当包含积分约束时,我无法让我的拟合通过初始点和终点。如果我忽略积分约束,则拟合工作正常并通过该点。是不是不能同时满足积分约束和点约束?我在 Windows 10.
上使用 Python 3.7.10import scipy
import numpy as np
import matplotlib.pyplot as plt
x = scipy.linspace(0, scipy.pi, 100)
y = scipy.sin(x) + (0. + scipy.rand(len(x))*0.4)
def Func(x,a,b,c):
return a*x**2 + b*x + c
# modified function definition with penalization
def FuncPen(x,a,b,c):
integral = scipy.integrate.quad(Func, x[0], x[-1], args=(a,b,c))[0]
penalization = abs(np.trapz(y,x)-integral)*10000
return a*x**2 + b*x + c + penalization
sigma = np.ones(len(x))
sigma[[0, -1]] = 0.0001 # first and last points
popt1, _ = scipy.optimize.curve_fit(Func, x, y, sigma=sigma)
popt2, _ = scipy.optimize.curve_fit(FuncPen, x, y, sigma=sigma)
y_fit1 = Func(x, *popt1)
y_fit2 = Func(x, *popt2)
fig, ax = plt.subplots(1)
ax.scatter(x,y)
ax.plot(x,y_fit1, color='g', alpha=0.75, label='curve_fit')
ax.plot(x,y_fit2, color='b', alpha=0.75, label='constrained')
plt.legend()
非常感谢 Erwin Kalvelagen 对这个问题的开阔的评论。我在这里发布我的解决方案:
import scipy
import numpy as np
import matplotlib.pyplot as plt
x = scipy.linspace(0, scipy.pi, 100)
y = scipy.sin(x) + (0. + scipy.rand(len(x))*0.4)
def Func(x,a,b,c):
return a*x**2 + b*x + c
# modified function definition with penalization
def FuncPen(x,a,b,c):
integral = scipy.integrate.quad(Func, x[0], x[-1], args=(a,b,c))[0]
penalization = abs(np.trapz(y,x)-integral)*10000
return a*x**2 + b*x + c + penalization
# Writing as a general constraint problem
def FuncNew(x,params):
return params[2]*x**2 + params[1]*x + params[0]
def ConstraintIntegral(params):
integral = integr.quad(FuncNew, x[0], x[-1], args=(params,))[0]
return integral- np.trapz(y,x)
def ConstraintBegin(params):
return y[0] - FuncNew(x[0],params)
def ConstraintEnd(params):
return y[-1] - FuncNew(x[-1],params)
def Objective(params,x,y):
y_pred = FuncNew(x,params)
return np.sum((y_pred - y) ** 2) # least squares
cons = [{'type':'eq', 'fun': ConstraintIntegral},
{'type':'eq', 'fun': ConstraintBegin},
{'type':'eq', 'fun': ConstraintEnd}]
new = scipy.optimize.minimize(Objective, x0=popt1, args=(x,y), constraints=cons)
popt3 = new.x
y_fit3 = FuncNew(x,popt3)
#####
sigma = np.ones(len(x))
sigma[[0, -1]] = 0.0001 # first and last points
popt1, _ = scipy.optimize.curve_fit(Func, x, y, sigma=sigma)
popt2, _ = scipy.optimize.curve_fit(FuncPen, x, y, sigma=sigma)
y_fit1 = Func(x, *popt1)
y_fit2 = Func(x, *popt2)
fig, ax = plt.subplots(1)
ax.scatter(x,y)
ax.plot(x,y_fit1, color='g', alpha=0.75, label='curve_fit')
ax.plot(x,y_fit2, color='b', alpha=0.75, label='constrained')
ax.plot(x,y_fit3, color='r', alpha=0.75, label='generally constrained')
plt.legend()