【问题标题】:scipy curve_fit with constraint and fixing points带有约束和固定点的 scipy curve_fit
【发布时间】:2021-07-13 16:26:47
【问题描述】:

我正在尝试使用 SciPy 的 optimize.curve_fit 将函数拟合到一些分散数据,但我需要拟合曲线下的面积与基于分散数据计算的面积相同,并且曲线通过数据的起点和终点。为了做到这一点,我在this answer 中使用由分散数据定义的区域(积分),同时使用sigma 建议的参数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()

【问题讨论】:

  • 我会将其表述为一般受限的 NLP 问题。这意味着您可以直接向问题添加约束,而不是使用惩罚。 Scipy 有一些允许约束的求解器。

标签: python optimization scipy curve-fitting


【解决方案1】:

非常感谢 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()

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2020-11-09
    • 2021-02-16
    • 2018-02-03
    • 2016-05-06
    • 2010-11-23
    相关资源
    最近更新 更多