仅带一个参数的opt.curve_fit

问题描述 投票:1回答:1

我在使用scipy.opt.curve_fit仅用一个参数拟合曲线时遇到麻烦:

import scipy.optimize as opt
import numpy as np

def func(T):
    return 76.881324*np.exp((-L)/(8.314*T))

best_params, cov_matrix = opt.curve_fit(func, xdata = x, ydata = y, p0=[])

我有值数组,x(下面的等式中的T)和y(P),我正尝试拟合方程enter image description here

但是似乎它希望func()具有多个参数。我该如何解决?

python scipy curve-fitting
1个回答
0
投票

这里是使用您的方程式和一些测试数据的图形化Python拟合器。用您自己的示例数据替换,您应该完成。

plot

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

xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7])
yData = numpy.array([1.1, 20.2, 30.3, 60.4, 50.0, 60.6, 70.7])


def func(T, L):
    return 76.881324*numpy.exp((-L)/(8.314*T))


# all "1.0" is the same as the scipy defaults
initialParameters = numpy.array([1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

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('Parameters:', fittedParameters)
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)
© www.soinside.com 2019 - 2024. All rights reserved.