【问题标题】:Not expected result received with scipy's curve_fitscipy的curve_fit未收到预期结果
【发布时间】:2016-09-30 00:50:02
【问题描述】:

我试图用 scipy curve_fit 对我的数据进行多元对数回归,结果期望得到一条线,但得到一条曲线。 这是我使用的代码:

Quercetin=[23,195,6,262,272,158,79,65,136,198]
Naringenin=[11,4,8,6,6,7,6,9,7,9]
Rutin=[178,165,93,239,202,3325,4427,7607,3499,1762]
TEAC=[23,189,37,265,290,267,362,388,364,321]

import matplotlib.pyplot as plt
import scipy
from scipy.optimize import curve_fit
import numpy as np
def func(x, a, b, c, d,e):
    m=np.log(a*x[0]+b*x[1]+c*x[2])
    return d*(m)+e
x=scipy.array([Quercetin, Naringenin,Rutin])
y=scipy.array(TEAC)
popt, pcov = curve_fit(func, x ,y)
print (popt)

plt.plot(func(x,*popt),y,'ro-')
plt.show()

我得到了这个结果:

虽然我想得到这样的东西:

谁能告诉我我做错了什么? 如果这很重要,我会在 Windows 10 上使用来自 Anaconda 的 Python 3.5。

【问题讨论】:

    标签: python scipy curve-fitting


    【解决方案1】:

    (我假设问题实际上是关于绘图)。 您要求 matplotlib 绘制由直线 (-) 连接的红点 ('ro')。 Matplotlib 按照给定的任何顺序强制和连接它们。

    如果要绘制一条线,只需单独绘制即可:

    In [58]: yres = func(x, *popt)
    
    In [59] plt.plot(yres, y, 'ro')
    Out[59]: [<matplotlib.lines.Line2D at 0x7f5c0796b828>]
    
    In [60]: plt.plot([0, 400], [0, 400], '-')
    Out[60]: [<matplotlib.lines.Line2D at 0x7f5c07a9c860>]
    

    【讨论】:

    • 谢谢。我真的忽略了我以前绘制的方式将绘制由线连接的点!但是在这里,正如我所看到的,将绘制一条简单的线 y=x ,而我想绘制曲线拟合的线。
    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2020-06-27
    • 2018-12-22
    相关资源
    最近更新 更多