为了账号安全,请及时绑定邮箱和手机立即绑定

拟合曲线时出现类型错误

拟合曲线时出现类型错误

烙印99 2021-11-09 11:01:15
我正在尝试将曲线拟合到我拥有的一些数据,但由于某种原因,我只是收到错误“'numpy.float64' 对象不能解释为整数”,我不明白为什么或如何修复它。将不胜感激一些帮助,代码如下:import numpy as npimport matplotlib.pyplot as pltfrom scipy import optimizemud=[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 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))m = np.linspace(max(mud),min(mud), 9)ax = plot(mud,F,"b^")ax3 = plot(m,fitfunc(p2,m),"g-")
查看完整描述

2 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

您的问题是您的参数mud和F是列表,而不是数组,这意味着您不能将它们与数字相乘。因此错误。如果您将这些参数定义为np.ndarrays,它将起作用:


import numpy as np

import matplotlib.pyplot as plt

from scipy import optimize


mud=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 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))


print(p1, success)


[ 0.46006301 76.7920086   1.        ] 2


查看完整回答
反对 回复 2021-11-09
?
慕后森

TA贡献1802条经验 获得超5个赞

这是使用 Van Deemter 色谱方程的图形拟合器,它非常适合您的数据。


//img1.sycdn.imooc.com//6189e49c0001d1fa07260608.jpg

import numpy, scipy, matplotlib

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit


# mud

xData=numpy.array([0.0014700734999999996,

0.0011840320799999997,

0.0014232304799999995,

0.0008501509799999997,

0.0007235751599999999,

0.0005770661399999999,

0.0005581295999999999,

0.00028703807999999994,

0.00014850233999999998])


# F

yData=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 defaults

initialParameters = numpy.array([1.0, 1.0, 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 (mud)') # X axis data label

    axes.set_ylabel('Y Data (F)') # Y axis data label


    plt.show()

    plt.close('all') # clean up after using pyplot


graphWidth = 800

graphHeight = 600

ModelAndScatterPlot(graphWidth, graphHeight)


查看完整回答
反对 回复 2021-11-09
  • 2 回答
  • 0 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信