首页 > 解决方案 > 为什么 python 中 scipy.optimze 中的 curve_Fit 在这段代码中表现得很奇怪?

问题描述

我用一段非常简单的代码遇到了这个奇怪的问题。我查看了无数示例,它们似乎都运行良好,但由于某种原因,我的却没有……</p>

我想要实现的是设置回归模型开始绘制的点。我希望它是 5000,而不是零。在另一端它工作正常,即如果我将回归设置为在所需的点结束,它会,但从零以外的任何其他点开始都不起作用。如果我将“start”xFit1 设置为非零值,则缩放会变得很奇怪,而绘图的开头则保持为零。可能会发生什么,有什么想法吗?

AVG_PNTSx = []
u = 0
p = 0
S_O = False

#Array creation

#Add each sample preparation result here manually:  
AVG_PNTSy = [14, 30, 64, 130, 300, 400, 1220, 2660, 4939, 10100, 10600, 10900, 10000, 22150, 45000]

#Auto creation of x-array based on y-array
for i in range(len(AVG_PNTSy)):
    u = (i*20001)+10000
    AVG_PNTSx.append(u)

#Exp Regression

def func(x,a,b):
    return a*np.exp(b*x)

xFit1 = np.arange(start,300000,1)

init_guess = [1,0.00001]

popt, pcov = curve_fit(func, AVG_PNTSx, AVG_PNTSy,init_guess)

#Plots
p3, = plt.plot((func(xFit1,*popt)))
plt.plot(AVG_PNTSx,AVG_PNTSy,"ro")  

提前致谢!

标签: pythoncurve-fitting

解决方案


当您进行绘图时,您忘记放置 X。

一个解法:

xFit2=np.arange(50000, 300000, 1)

#Plots
p3, = plt.plot(xFit2,(func(xFit2,*popt)))
plt.plot(AVG_PNTSx,AVG_PNTSy,"ro")

在此处输入图像描述


推荐阅读