python - scipy.optimize.curvefit 中除以零误差

标签 python scipy curve-fitting chemistry

我正在尝试用两个速率常数 k1 和 k2 拟合三个时间演化曲线。我试图求解的方程组是:

A_t = A_0 * exp(-k1*t)
B_t = [A_0 * k1/(k2-k1)]* exp(-k1*t) - [A_0*(k1/(k2-k1)-B_0] * exp(-k2*t)
C_t = [A_0 * -k2/(k2-k1) ]* exp(-k1*t) + [A_0*(k1/(k2-k1)-B_0] * exp(-k2*t) + A_0 + B_0

我想要将 k1k2 的最佳值拟合到 A、B 和 C 的数据值,其中 A_t 等是A 在时间 tA_0=0.4B_0=0.6 时的当前人口。

为了解决这个问题,我使用 scipy.optimize.curve_fit函数,我将方程设置为矩阵 uw。在下面,我手动将 A_0=0.4B_0=0.6 输入到函数中(这与底部问题的第 2 部分相关):

def func(t,k1,k2):

    u = np.array([[0.4,0,0],
                  [0.4*k1/(k2-k1),-0.4*(k1/(k2-k1))+0.6,0],
                  [0.4*(-k2/(k2-k1)),0.4*k1/(k2-k1)-0.6,1]])


    w = np.array([np.exp(-t*k1),
                  np.exp(-t*k2),
                  np.ones_like(t)])

    return np.dot(u,w).flatten()

为了解决一些测试数据,我创建了一个测试集,其中设置了k1=0.03k2=0.003:

t=np.arange(1000)*0.5
test=func(t,0.03,0.004).reshape((3,1000))
test+=np.random.normal(size=test.shape)*0.01

生成以下图:

Plot of test

当我尝试拟合 k1k2 的值时,出现以下错误:

popt,popc=optimize.curve_fit(func,t,test.flatten(),method='lm')

/usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:4: RuntimeWarning: divide by zero encountered in double_scalars after removing the cwd from sys.path. /usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:5: RuntimeWarning: divide by zero encountered in double_scalars """ /usr/local/lib/python3.6/site-packages/scipy/optimize/minpack.py:785: OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning)

我知道这里某处存在除以零的错误,但我不确定它在哪里或如何解决它。所以,我的问题是:

  1. 如何解决 curve_fit 函数中的此错误?
  2. 有没有办法将 A_0B_0 传递到 optimize.curve_fit 中,而不是像我上面那样手动输入浓度?我的理解是只有自变量t和未知数k1k2可以传递给函数?

感谢您提供的任何帮助

最佳答案

根据评论,这里是使用 scipy 的 Differential_evolution 遗传算法模块来估计初始参数的示例。该模块使用拉丁超立方算法来确保对参数空间的彻底搜索,并且该算法需要在搜索范围内进行。在此示例中,这些界限取自数据最小值和最大值。拟合通过调用 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

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(t, n_0, L, offset): #exponential curve fitting function
    return n_0*numpy.exp(-L*t) + 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)

    parameterBounds = []
    parameterBounds.append([minX, maxX]) # seach bounds for n_0
    parameterBounds.append([minX, maxX]) # seach bounds for L
    parameterBounds.append([0.0, maxY]) # seach 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('Fitted parameters:', 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)

关于python - scipy.optimize.curvefit 中除以零误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52781274/

相关文章:

r - R中的曲线拟合

python - scipy.optimize.curve_fit 用于对变量参数具有复杂依赖性的函数

python - 将相同字典键的值放在python中的一组中

python - 按字典顺序对 2d numpy 数组进行排序

python - 在 SymPy 中明智地使用常量

python - Scipy.optimize COBYLA 约束违规

python - 根据另一个矩阵中的值移动一个矩阵中的值的有效方法

python-3.x - 使用整数输入的曲线拟合 Python 3.3

python - pypi,看哪个包需要特定的包

Python:使用线程求解多个线性系统