拟合曲线时出现类型错误

我正在尝试将曲线拟合到我拥有的一些数据,但由于某种原因,我只是收到错误“'numpy.float64' 对象不能解释为整数”,我不明白为什么或如何修复它。将不胜感激一些帮助,代码如下:


import numpy as np

import matplotlib.pyplot as plt

from scipy import optimize



mud=[0.0014700734999999996,

 0.0011840320799999997,

 0.0014232304799999995,

 0.0008501509799999997,

 0.0007235751599999999,

 0.0005770661399999999,

 0.0005581295999999999,

 0.00028703807999999994,

 0.00014850233999999998]

F=[0.5750972123893806,

 0.5512177433628319,

 0.5638906194690266,

 0.5240915044247788,

 0.5217873451327435,

 0.5066008407079646,

 0.5027256637168142,

 0.4847113274336283,

 0.46502123893805314]



fitfunc = lambda p, x: p[0]+p[1]*x # Target function

errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function

p0 = [0.46,80,1] # Initial guess for the parameters

p1, success = optimize.leastsq(errfunc, p0[:], args=(mud, F))


m = np.linspace(max(mud),min(mud), 9)

ax = plot(mud,F,"b^")

ax3 = plot(m,fitfunc(p2,m),"g-")


烙印99
浏览 170回答 2
2回答

精慕HU

您的问题是您的参数mud和F是列表,而不是数组,这意味着您不能将它们与数字相乘。因此错误。如果您将这些参数定义为np.ndarrays,它将起作用:import numpy as npimport matplotlib.pyplot as pltfrom scipy import optimizemud=np.array([0.0014700734999999996, 0.0011840320799999997, 0.0014232304799999995, 0.0008501509799999997, 0.0007235751599999999, 0.0005770661399999999, 0.0005581295999999999, 0.00028703807999999994, 0.00014850233999999998])F=np.array([0.5750972123893806, 0.5512177433628319, 0.5638906194690266, 0.5240915044247788, 0.5217873451327435, 0.5066008407079646, 0.5027256637168142, 0.4847113274336283, 0.46502123893805314])fitfunc = lambda p, x: p[0]+p[1]*x # Target functionerrfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target functionp0 = [0.46,80,1] # Initial guess for the parametersp1, success = optimize.leastsq(errfunc, p0[:], args=(mud, F))print(p1, success)给[ 0.46006301 76.7920086   1.        ] 2

慕后森

这是使用 Van Deemter 色谱方程的图形拟合器,它非常适合您的数据。import numpy, scipy, matplotlibimport matplotlib.pyplot as pltfrom scipy.optimize import curve_fit# mudxData=numpy.array([0.0014700734999999996,0.0011840320799999997,0.0014232304799999995,0.0008501509799999997,0.0007235751599999999,0.0005770661399999999,0.0005581295999999999,0.00028703807999999994,0.00014850233999999998])# FyData=numpy.array([0.5750972123893806,0.5512177433628319,0.5638906194690266,0.5240915044247788,0.5217873451327435,0.5066008407079646,0.5027256637168142,0.4847113274336283,0.46502123893805314])def func(x, a, b, c): # Van Deemter chromatography equation    return a + b/x + c*x# these are the same as the scipy defaultsinitialParameters = numpy.array([1.0, 1.0, 1.0])# curve fit the test datafittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)modelPredictions = func(xData, *fittedParameters) absError = modelPredictions - yDataSE = numpy.square(absError) # squared errorsMSE = numpy.mean(SE) # mean squared errorsRMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSERsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))print('Parameters:', fittedParameters)print('RMSE:', RMSE)print('R-squared:', Rsquared)print()########################################################### graphics output sectiondef 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 (mud)') # X axis data label    axes.set_ylabel('Y Data (F)') # Y axis data label    plt.show()    plt.close('all') # clean up after using pyplotgraphWidth = 800graphHeight = 600ModelAndScatterPlot(graphWidth, graphHeight)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python