python - 为什么使用 Seaborn 绘制回归时截距显示不正确?

标签 python plot regression linear-regression seaborn

我构建了一个线性回归模型来检查两个变量(chemical_1chemical_2)之间的关系 dataset 。 根据结果​​,intercept = 16.83488364225717

我刚刚开始探索数据科学的数学基础知识,目前我对截距的理解是,它是回归线与 y 轴(且 x=0)相交的值。所以现在我对用 Seaborn 构建的结果图感到困惑。

为什么它显示在 10 和 12 之间穿过 y 轴 的回归线,而不是截距的实际值 (16.83488364225717) 和 x=0?我应该做什么来解决这个问题?

这是我的代码:

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)
print ("slope = " + str(slope))
print ("intercept = " + str(intercept))
print ("r_squared = " + str(r_value**2))
print ("r_value = " + str(r_value))
print ("p_value = " +str(p_value))

slope = -0.9345759557752411
intercept = 16.83488364225717
r_squared = 0.04205938806347038
r_value = -0.20508385617466426
p_value = 0.00784469031490164

predict_y = slope * X + intercept

fig, ax = plt.subplots()
sns.set(color_codes=True)
sns.set(rc={'figure.figsize':(10, 10)})
ax = sns.regplot(x=X, y=Y, line_kws={'label':'$y=%3.7s*x+%3.7s$'%(slope, intercept)});
sns.regplot(x=X, y=Y, fit_reg=False, ax=ax);
sns.regplot(x=X, y=predict_y,scatter=False, ax=ax);
ax.set_ylabel('chemical_2')
ax.legend()
plt.show()

enter image description here

UPD:当我使用西蒙提出的解决方案时 - 扩展轴的限制,截距仍然没有显示,绘图如下所示: enter image description here
当我使用 set_ylim(0,20) 时,绘图上的数据看起来被挤压。实际上,我设置的任何轴参数(默认值除外)都会导致图上的数据和置信区间看起来受到挤压。

enter image description here

最佳答案

正如评论中提到的,当X的值为0时,截距是Y的值。所以你的X轴的范围不允许实际的要显示的拦截

import numpy as np
from scipy import stats
import seaborn as sns

np.random.seed(1236)
X = np.arange(5,10) + np.random.normal(0,1,5)
Y = np.arange(5,10) + np.random.normal(0,1,5)

slope, intercept, r_value, p_value, slope_std_error = stats.linregress(X,Y)
predict_y = slope * X + intercept

print("slope = " + str(slope))
print("intercept = " + str(intercept))

sns.regplot(x=X, y=Y, fit_reg=False)
sns.regplot(x=X, y=predict_y,scatter=False)

这里我们可以看到截距是0.115:

slope = 0.9897768121234015
intercept = 0.11521162448067557

这给出了一个如下所示的seaborn图表:

enter image description here

如果您想真正看到交叉点,您要做的就是扩展轴的限制:

p = sns.regplot(x=X, y=Y, fit_reg=False)
p.axes.set_xlim(0,)
p.axes.set_ylim(0,)
sns.regplot(x=X, y=predict_y,scatter=False)

enter image description here

编辑:

如果您想在扩大轴限制时解决数据压缩问题,可以通过计算 Z 分数来标准化数据:

X = np.arange(5,10) + np.random.normal(0,1,5)
Y = np.arange(5,10) + np.random.normal(0,1,5)
X = stats.zscore(X)
Y = stats.zscore(Y)

slope, intercept, r_value, p_value, slope_std_error = stats.linregress(X,Y)
predict_y = slope * X + intercept

print("slope = " + str(slope))
print("intercept = " + str(intercept))

sns.regplot(x=X, y=Y, fit_reg=False)
sns.regplot(x=X, y=predict_y,scatter=False)

参数值:

slope = 0.667021422528575
intercept = -2.8128800822178726e-16

enter image description here

值得注意的是,在这种情况下,您的 XY 不再处于其原始指标中。因此,斜率现在的解释是“X 每增加 1 个标准差,Y 的值将增加 0.667 个标准差”。但您会看到截距现在基本上为 0(即 X=0 时 Y 的值),并且显示在图的中心

关于python - 为什么使用 Seaborn 绘制回归时截距显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49866207/

相关文章:

python - NumPy 中 'any' 的困难

machine-learning - 测试预测值相对于已知值的准确性

python - group_by 返回重复的键

python - gensim 中的 get_document_topics 和 get_term_topics

python - 如何在 Python 中将文件加载为 List 类型?

R:将文件列表中的多个绘图保存到单个文件中(png 或 pdf 或其他格式)

graphics - 如何实现函数绘图仪

r - R中的标签对数刻度显示

python - 神经网络中的线性函数正在产生巨大的输出值

r - 为什么来自mgcv的bam对于某些数据比较慢?