有什么方法可以为Scipy的优化曲线拟合提供限制吗?
我的例子:
def optimized_formula(x, m_1, m_2, y_1, y_2, ratio_2):
return (log(x[0]) * m_1 + m_2)*((1 - x[1]/max_age)*(1-ratio_2)) + ((log(x[1]) * y_1 + y_2)*(x[1]/max_age)*ratio_2)
popt, pcov = optimize.curve_fit(optimized_formula, usage_and_age, prices)
x [0]是年龄,max_age是常数。考虑到这一点,当x [0]接近最大值时,x [1] / max_age接近1.
是否可以提供约束/限制,其中x [1] / max_age> 0.3和x [1] / max_age< 0.7和其他约束,例如m_1< 0,m_2> 0,依此类推。
答案 0 :(得分:8)
正如另一个答案中所建议的那样,您可以使用lmfit来解决这些问题。因此,我添加了一个如何使用它的示例,以防有人对此主题感兴趣。
假设您有一个数据集如下:
xdata = np.array([177.,180.,183.,187.,189.,190.,196.,197.,201.,202.,203.,204.,206.,218.,225.,231.,234.,
252.,262.,266.,267.,268.,277.,286.,303.])
ydata = np.array([0.81,0.74,0.78,0.75,0.77,0.81,0.73,0.76,0.71,0.74,0.81,0.71,0.74,0.71,
0.72,0.69,0.75,0.59,0.61,0.63,0.64,0.63,0.35,0.27,0.26])
并且您希望将模型拟合到如下所示的数据:
model = n1 + (n2 * x + n3) * 1./ (1. + np.exp(n4 * (n5 - x)))
具有
的约束0.2 < n1 < 0.8
-0.3 < n2 < 0
使用lmfit
(版本0.8.3),然后获得以下输出:
n1: 0.26564921 +/- 0.024765 (9.32%) (init= 0.2)
n2: -0.00195398 +/- 0.000311 (15.93%) (init=-0.005)
n3: 0.87261892 +/- 0.068601 (7.86%) (init= 1.0766)
n4: -1.43507072 +/- 1.223086 (85.23%) (init=-0.36379)
n5: 277.684530 +/- 3.768676 (1.36%) (init= 274)
如您所见,拟合非常好地再现数据,参数在请求的范围内。
以下是使用一些附加注释重现该图的完整代码:
from lmfit import minimize, Parameters, Parameter, report_fit
import numpy as np
xdata = np.array([177.,180.,183.,187.,189.,190.,196.,197.,201.,202.,203.,204.,206.,218.,225.,231.,234.,
252.,262.,266.,267.,268.,277.,286.,303.])
ydata = np.array([0.81,0.74,0.78,0.75,0.77,0.81,0.73,0.76,0.71,0.74,0.81,0.71,0.74,0.71,
0.72,0.69,0.75,0.59,0.61,0.63,0.64,0.63,0.35,0.27,0.26])
def fit_fc(params, x, data):
n1 = params['n1'].value
n2 = params['n2'].value
n3 = params['n3'].value
n4 = params['n4'].value
n5 = params['n5'].value
model = n1 + (n2 * x + n3) * 1./ (1. + np.exp(n4 * (n5 - x)))
return model - data #that's what you want to minimize
# create a set of Parameters
# 'value' is the initial condition
# 'min' and 'max' define your boundaries
params = Parameters()
params.add('n1', value= 0.2, min=0.2, max=0.8)
params.add('n2', value= -0.005, min=-0.3, max=10**(-10))
params.add('n3', value= 1.0766, min=-1000., max=1000.)
params.add('n4', value= -0.36379, min=-1000., max=1000.)
params.add('n5', value= 274.0, min=0., max=1000.)
# do fit, here with leastsq model
result = minimize(fit_fc, params, args=(xdata, ydata))
# write error report
report_fit(params)
xplot = np.linspace(min(xdata), max(xdata), 1000)
yplot = result.values['n1'] + (result.values['n2'] * xplot + result.values['n3']) * \
1./ (1. + np.exp(result.values['n4'] * (result.values['n5'] - xplot)))
#plot results
try:
import pylab
pylab.plot(xdata, ydata, 'k+')
pylab.plot(xplot, yplot, 'r')
pylab.show()
except:
pass
编辑:
如果您使用版本0.9.x,则需要相应地调整代码;检查here已从0.8.3更改为0.9.x。
答案 1 :(得分:5)
注意:SciPy版本0.17中新增
假设您希望将模型拟合到如下所示的数据:
y=a*t**alpha+b
并使用alpha
约束0<alpha<2
而其他参数a和b保持空闲。然后我们应该使用optimize.curve_fit的bounds选项:
import numpy as np
from scipy.optimize import curve_fit
def func(t, a,alpha,b):
return a*t**alpha+b
param_bounds=([-np.inf,0,-np.inf],[np.inf,2,np.inf])
popt, pcov = optimize.curve_fit(func, xdata,ydata,bounds=param_bounds)
来源为here
答案 2 :(得分:3)
尝试使用lmfit模块(http://lmfit.github.io/lmfit-py/)。它为scipy.optimize中的许多优化例程添加了一种修复或设置参数边界的方法,包括最小二乘法,并提供了许多工具来使拟合更容易。
答案 3 :(得分:2)
由于curve_fit()
使用最小二乘法,您可能需要查看scipy.optimize.fmin_slsqp()
,这允许执行约束优化。检查this tutorial如何使用它。