【问题标题】:Fitting with constraints on derivative Python拟合衍生 Python 的约束
【发布时间】:2019-03-19 03:48:37
【问题描述】:

在尝试创建优化算法时,我不得不对我的集合的曲线拟合施加约束。

这是我的问题,我有一个数组:

Z = [10.3, 10, 10.2, ...]
L = [0, 20, 40, ...]

我需要找到一个适合Z 的函数,其斜率是我正在寻找的函数的导数。

假设 f 是我的函数,f 应该适合 Z 并且对 f 的导数有条件,它不应该超过特殊值。

python中是否有任何库可以帮助我完成这项任务?

【问题讨论】:

  • 您在 stackoverflow 上提出的问题应该包括您为解决问题而编写的代码。我很感激在这种情况下这是不可能的。您应该尝试在谷歌上搜索可能的 Python 库的 Python 约束优化,或咨询另一个论坛,例如 quora.com。在您编写了一些计算机代码之后,非常欢迎您带着问题回到这里。但是,您应该阅读帮助文件。

标签: python numpy curve-fitting data-fitting function-fitting


【解决方案1】:

COBYLA minimzer 可以处理此类问题。在下面的例子中,一个 3 次多项式拟合了导数处处为正的约束。

from matplotlib import pylab as plt

import numpy as np
from scipy.optimize import minimize

def func(x, pars):
    a,b,c,d=pars
    return a*x**3+b*x**2+c*x+d

x = np.linspace(-4,9,60)
y = func(x, (.3,-1.8,1,2))
y += np.random.normal(size=60, scale=4.0)

def resid(pars):
    return ((y-func(x,pars))**2).sum()

def constr(pars):
    return np.gradient(func(x,pars))

con1 = {'type': 'ineq', 'fun': constr}
res = minimize(resid, [.3,-1,1,1], method='cobyla', options={'maxiter':50000}, constraints=con1)
print res

f=plt.figure(figsize=(10,4))
ax1 = f.add_subplot(121)
ax2 = f.add_subplot(122)

ax1.plot(x,y,'ro',label='data')
ax1.plot(x,func(x,res.x),label='fit')
ax1.legend(loc=0) 
ax2.plot(x,constr(res.x),label='slope')
ax2.legend(loc=0)
plt.show()

【讨论】:

  • 当您有时间时,您能否更改此示例以允许为每个问题指定“不能超过”数字限制?它几乎是完美的,但对于极限值。
  • 我真的很喜欢这个答案,但是:NonLinearConstrain 不是必需的,func 应该在使用前声明,der() 根本没有声明。如果您的意思是constr,它应该读作ax2.plot(x[1:], constr(x, res.x), label='slope'),但在非等距点的情况下,最好使用numpy.gradient 和相应的增量而不是numpy.diff,那么“斜率”实际上是真正的斜率.最后一个plt.show() 也很好。
  • @mikuszefski 感谢您指出示例未运行。我马上把它修好。关于NonLinearConstraint,似乎在较新版本的 scipy 中定义约束的语法发生了变化。我使用的是 0.19.1,而 NonLinearConstraint 根本没有定义。
【解决方案2】:

这是一个拟合直线的示例,该直线具有导数限制。这在要拟合的函数中实现为简单的“砖墙”,如果超过导数的最大值,则函数返回一个非常大的值,因此返回一个非常大的误差。该示例使用 scipy 的差分进化遗传算法模块来估计曲线拟合的初始参数,并且由于该模块使用拉丁超立方算法来确保对参数空间的彻底搜索,因此该示例需要在其中搜索的参数范围 - 在本示例中是那些范围是从数据的最大值和最小值得出的。该示例通过对 curve_fit() 的最终调用完成拟合,而不传递参数范围,以防实际的最佳参数超出遗传算法使用的范围。

请注意,最终拟合的参数表明斜率参数处于导数极限,这样做是为了表明这种情况可能发生。我不会认为这个条件是最优的。

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.optimize import differential_evolution
import warnings

derivativeLimit = 0.0025

xData = numpy.array([19.1647, 18.0189, 16.9550, 15.7683, 14.7044, 13.6269, 12.6040, 11.4309, 10.2987, 9.23465, 8.18440, 7.89789, 7.62498, 7.36571, 7.01106, 6.71094, 6.46548, 6.27436, 6.16543, 6.05569, 5.91904, 5.78247, 5.53661, 4.85425, 4.29468, 3.74888, 3.16206, 2.58882, 1.93371, 1.52426, 1.14211, 0.719035, 0.377708, 0.0226971, -0.223181, -0.537231, -0.878491, -1.27484, -1.45266, -1.57583, -1.61717])
yData = numpy.array([0.644557, 0.641059, 0.637555, 0.634059, 0.634135, 0.631825, 0.631899, 0.627209, 0.622516, 0.617818, 0.616103, 0.613736, 0.610175, 0.606613, 0.605445, 0.603676, 0.604887, 0.600127, 0.604909, 0.588207, 0.581056, 0.576292, 0.566761, 0.555472, 0.545367, 0.538842, 0.529336, 0.518635, 0.506747, 0.499018, 0.491885, 0.484754, 0.475230, 0.464514, 0.454387, 0.444861, 0.437128, 0.415076, 0.401363, 0.390034, 0.378698])


def func(x, slope, offset): # simple straight line function
    derivative = slope # in this case, derivative = slope
    if derivative > derivativeLimit:
        return 1.0E50 # large value gives large error
    return x * slope + offset


# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    val = func(xData, *parameterTuple)
    return numpy.sum((yData - val) ** 2.0)


def generate_Initial_Parameters():
    # min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)

    slopeBound = (maxY - minY) / (maxX - minX)

    parameterBounds = []
    parameterBounds.append([-slopeBound, slopeBound]) # search bounds for slope
    parameterBounds.append([minY, maxY]) # search bounds for offset

    # "seed" the numpy random number generator for repeatable results
    result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
    return result.x

# by default, differential_evolution completes by calling curve_fit() using parameter bounds
geneticParameters = generate_Initial_Parameters()

# now call curve_fit without passing bounds from the genetic algorithm,
# just in case the best fit parameters are aoutside those bounds
fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)
print(fittedParameters)
print()

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print()
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

【讨论】:

  • 非常感谢詹姆斯,感谢您的回答,我会更多地考虑此事,并尝试在我的代码中实现此解决方案并使其适应我的情况。
猜你喜欢
  • 2015-02-18
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多