python - opt.curve_fit 只有一个参数
问题描述
我无法使用只有一个参数来拟合曲线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
(下面的 eq 中的 T)和y
(P)我试图拟合方程
但它似乎想要func()
有不止一个论点。我该如何解决?
解决方案
这是一个图形化的 Python 拟合器,它使用您的方程和一些测试数据。用您自己的数据替换示例数据,您应该完成了。
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)
推荐阅读
- javascript - 如果服务器上不存在文件扩展名,则为文件类型扩展名获取 null、空或未定义
- python - 如何将 python 脚本的结果传递给 adoc (asciidoc)
- javascript - 导入变量后的javascript在初始化之前无法访问
- mongodb - 从 mongo db 文档中的数据字段中获取月份
- kotlin - 如何限制 Kotlin 中枚举值的使用?
- r - 创建用 R 中的累积计数总结分组的变量
- django - Pip 包安装在全局和 virtualenv 中
- objective-c - 如何在 Obj-C 中调用一些具有超时的阻塞方法?
- java - getContextPath() 在不同的环境中表现不同
- uwp - 如何更改禁用的 UWP ComboBox 的前景色?