我可以使用带有 pandas 数据框的散点图绘制回归线并显示参数吗?

Can I draw a regression line and show parameters using scatterplot with a pandas dataframe?

我想使用以下代码从 Pandas 数据帧生成散点图:

df.plot.scatter(x='one', y='two, title='Scatterplot') 

是否有我可以随语句发送的参数,以便绘制回归线并显示拟合参数?

类似于:

df.plot.scatter(x='one', y='two', title='Scatterplot', Regression_line)

我认为 DataFrame.plot() 没有这样的参数。但是,您可以使用 Seaborn 轻松实现此目的。 只需将 pandas 数据框传递给 lmplot(假设您已安装 seaborn):

import seaborn as sns
sns.lmplot(x='one',y='two',data=df,fit_reg=True) 

您可以使用sk-learn得到回归线结合散点图。

from sklearn.linear_model import LinearRegression
X = df.iloc[:, 1].values.reshape(-1, 1)  # iloc[:, 1] is the column of X
Y = df.iloc[:, 4].values.reshape(-1, 1)  # df.iloc[:, 4] is the column of Y
linear_regressor = LinearRegression()
linear_regressor.fit(X, Y)
Y_pred = linear_regressor.predict(X)

plt.scatter(X, Y)
plt.plot(X, Y_pred, color='red')
plt.show()