【问题标题】:How can I graph a regression model?如何绘制回归模型?
【发布时间】:2018-08-17 17:50:15
【问题描述】:

我使用混合线性回归拟合 (mixedlm) 将数据拟合到多项式方程:

y = a + bx + cx2

模型给了我a、b和c,我想快速创建多项式曲线的图形。

有没有一种无需手动输入公式中所有参数的快速方法?

我读过 coefplot2 但我无法在 python 上安装它,欢迎任何建议谢谢

【问题讨论】:

  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 On topichow to ask... the perfect question 在此处申请。 StackOverflow 不是设计、编码、研究或教程服务。
  • 有很多可用的绘图包——而寻求建议对于 Stack Overflow 来说是题外话。生成值是基本的编程,一个简单的循环。你在哪里卡住了?
  • 你能给出一些期望的输出示例吗?

标签: python plot graph model


【解决方案1】:

根据您的描述,不清楚您想要什么究竟。根据我的理解,如果您想始终拟合 2 次多项式,您可以使用 NumPy 的 polyfitpoly1d 来拟合并使用它进一步绘制图表。下面是一个示例,您不必手动输入系数abc 来创建图表:

x = np.arange(10)
y = x**2 + np.random.normal(-2,2,10) # Adding random noise to y = x**2

xmesh = np.linspace(x[0], x[-1], 100) # Fine x-mesh for plotting
fit = np.poly1d(np.polyfit(x, y, 2)) # 2nd order polynomial fit

plt.plot(x, y, 'ko', label = 'Original data') # Plotting original data 
plt.plot(xmesh, fit(xmesh), label = 'Fit') # Plotting polynomial fit  
plt.legend(fontsize=16)

结果图

【讨论】:

    【解决方案2】:

    不确定您所说的“手动”是什么意思。一个可行的简单方法是(使用 numpy 和 matplotlib)

     x = np.linspace(-10, 10, 1000) #put your real domain here
     y = a + b*x + c*x**2 #you have these values from the model
     plt.plot(x, y)
     plt.show()
    

    【讨论】:

    • 我有几个数据集要运行,我不想为每个人手动输入系数。我想知道是否有办法从模型结果中自动创建曲线
    猜你喜欢
    • 2020-07-13
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2019-04-09
    • 2021-12-26
    相关资源
    最近更新 更多