【发布时间】:2018-09-24 18:48:00
【问题描述】:
我正在学习数据科学的基础知识,并从回归分析开始。所以我决定建立一个线性回归模型来检查这个dataset中的两个变量(chemical_1和chemical_2)之间的线性关系。
我将chemical_1 设为预测变量(自变量),将chemical_2 设为目标(因变量)。然后使用scipy.stats.linregress 计算回归线。
from scipy import stats
X = df['chemical_1']
Y = df['chemical_2']
slope, intercept, r_value, p_value, slope_std_error = stats.linregress(X,Y)
predict_y = slope * X + intercept
我想出了如何用 matplotlib 绘制回归线。
plt.plot(X, Y, 'o')
plt.plot(X, predict_y)
plt.show()
但是我想用 Seaborn 绘制回归图。我目前发现的唯一选择如下:
sns.set(color_codes=True)
sns.set(rc={'figure.figsize':(7, 7)})
sns.regplot(x=X, y=Y);
有没有办法为 Seaborn 提供回归线 predict_y = slope * X + intercept 以构建回归图?
UPD:当使用 RPyStats 提出的以下解决方案时,Y 轴将获得 chemical_1 名称,尽管它应该是 chemical_2。
fig, ax = plt.subplots()
sns.set(color_codes=True)
sns.set(rc={'figure.figsize':(8, 8)})
ax = sns.regplot(x=X, y=Y, line_kws={'label':'$y=%3.7s*x+%3.7s$'%(slope, intercept)});
ax.legend()
sns.regplot(x=X, y=Y, fit_reg=False, ax=ax);
sns.regplot(x=X, y=predict_y,scatter=False, ax=ax);
【问题讨论】:
标签: python plot regression linear-regression seaborn