Python中的R abline()等效项 [英] R abline() equivalent in Python
本文介绍了Python中的R abline()等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Python中将线性回归绘制到散点图上.
I am trying to plot a Linear Regression onto a scatterplot in Python.
在R中,我将简单地执行以下操作:
In R I would simply do the following:
fit_1 <- lm(medv ~ lstat)
plot(medv ~ lstat)
abline(fit_1, col = "red")
我一直在用Python寻找不同的解决方案,但是我似乎无法真正使它工作.
I have been looking at different solutions in Python, but I can't seem to be able to actually get it to work.
我的脚本是:
Boston.plot(kind='scatter', x='medv', y='lstat', color = "black")
plt.show()
运行线性回归
fit_1 = sm.ols(formula='medv ~ lstat', data= Boston).fit()
显示摘要
fit_1.summary()
图回归线
在此处插入代码
Plot Regression Line
Insert code here推荐答案
尝试一下:
plt.plot(Boston.lstat, fit_1.fittedvalues, 'r')
这篇关于Python中的R abline()等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文