Python中的R abline()等价物

Python中的R abline()等价物,python,r,plot,linear-regression,Python,R,Plot,Linear Regression,我试图用Python将线性回归绘制到散点图上 在R中,我只需执行以下操作: 运行OLS线性再研磨 运行线性回归 显示摘要 绘图回归线 在此处插入代码尝试以下操作: plt.plot(Boston.lstat, fit_1.fittedvalues, 'r') 试试这个: plt.plot(Boston.lstat, fit_1.fittedvalues, 'r') 这可以很简单地做到。在下面的代码中,我使用sklearn来拟合模型并预测值 import pandas as pd from s

我试图用Python将线性回归绘制到散点图上

在R中,我只需执行以下操作:

运行OLS线性再研磨 运行线性回归 显示摘要 绘图回归线 在此处插入代码尝试以下操作:

plt.plot(Boston.lstat, fit_1.fittedvalues, 'r')
试试这个:

plt.plot(Boston.lstat, fit_1.fittedvalues, 'r')

这可以很简单地做到。在下面的代码中,我使用
sklearn
来拟合模型并预测值

import pandas as pd
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt

model = LinearRegression()
model.fit(X,y)
predictions = model.predict(X)

plt.plot(X,y,'o')
# change here
plt.plot(X, predictions, '-')
plt.show()

这件事做起来很简单。在下面的代码中,我使用
sklearn
来拟合模型并预测值

import pandas as pd
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt

model = LinearRegression()
model.fit(X,y)
predictions = model.predict(X)

plt.plot(X,y,'o')
# change here
plt.plot(X, predictions, '-')
plt.show()

非常感谢helpOnly在X排序时起作用。非常感谢helpOnly在X排序时起作用。你的答案也起作用,但我给了@p W检查,因为他先回答了你的答案,但我给了@p W检查,因为他先回答了
plt.plot(Boston.lstat, fit_1.fittedvalues, 'r')
import pandas as pd
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt

model = LinearRegression()
model.fit(X,y)
predictions = model.predict(X)

plt.plot(X,y,'o')
# change here
plt.plot(X, predictions, '-')
plt.show()