python - opt.curve_fit 只有一个参数

标签 python scipy curve-fitting

我在使用 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 拟合器,使用您的方程和一些测试数据。将示例数据替换为您自己的数据,您就应该完成了。

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)

关于python - opt.curve_fit 只有一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833030/

相关文章:

Python 参数解析 : command-line argument that can be either named or positional

python - 双曲 Curve_Fit 的预测区间 - SciPy

python - 如何从 Django 中的复选框中获取多个值

python - 使用 pandas 过滤和排序数据

python - 列切片和行切片之间哪个是哪个?

python - numpy、scipy、matplotlib 和 pylab 之间的混淆

python - 在Python中插值网格时忽略NaN

matlab - 区分居中和缩放的 Polyfit 拟合

python - 如何将多条曲线拟合到单个数据散点图?

python - 尝试使用Selenium通过xpath选择元素,但出现错误“无法找到元素”