Python:如何处理回归 Q-Q 图中的异常值?

标签 python machine-learning regression data-science

我画了 qq 图多元回归,得到了下面的图。谁能告诉我为什么红线下面有两个点?这些点对我的模型有影响吗?

 graph

我使用下面的代码来绘制图表。

from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg = reg.fit(x_train,y_train)

pred_reg_GS = reg.predict(x_test)
diff= y_test-pred_reg_GS

import statsmodels.api as sm
sm.qqplot(diff,fit=True,line='45')
plt.show()

最佳答案

看看Understanding Q-Q Plots了解什么是 QQ 图的简明描述。就您而言,这个特定部分很重要:

If both sets of quantiles came from the same distribution, we should see the points forming a line that’s roughly straight.

这种理论上的一对一关系在您的图中使用红线明确地说明了。

关于你的问题...

that points effect for my model?

...远离红线的一个或两个点可能被认为是异常值。这意味着您尝试在此处构建的任何模型都无法捕获这些观察结果的属性。如果我们在这里看到的是回归模型残差的 QQ 图,您应该仔细查看这两个观察结果。这两个因素是什么让他们从其他样本中脱颖而出? “捕获”这些异常值的一种方法通常是用一两个虚拟变量来表示它们。

<小时/>

编辑 1:异常值和虚拟变量的基本方法

<小时/>

由于您没有明确标记您的问题sklearn,我冒昧地使用statsmodels来说明这一点。我将只使用内置的 iris 数据集来代替您的数据示例,其中我们将使用的最后一部分如下所示:

enter image description here

1。 sepal_width 对 sepal_length 的线性回归

图 1:

enter image description here

看起来不错!这里没什么问题。但是,让我们通过向数据集中添加一些极值来将其混合一下。您将在最后找到完整的代码片段。

2。引入异常值

现在,让我们在数据框中添加一行“sepal_width = 8而不是3”。 这将为您提供以下带有非常清晰的异常值的 qqplot:

enter image description here

这是模型摘要的一部分:

===============================================================================
                  coef    std err          t      P>|t|      [0.025      0.975]
-------------------------------------------------------------------------------
sepal_width     1.8690      0.033     57.246      0.000       1.804       1.934
==============================================================================
Omnibus:                       18.144   Durbin-Watson:                   0.427
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                7.909
Skew:                          -0.338   Prob(JB):                       0.0192
Kurtosis:                       2.101   Cond. No.                         1.00
==============================================================================

那么为什么这是一个异常值?因为我们搞乱了数据集。我无法确定数据集中出现异常值的原因。在我们虚构的示例中,山鸢尾的萼片宽度为 8 的原因可能很多。也许科学家把它贴错了?也许它根本就不是山毛榉?或者也许它已经被基因改造了?现在,不只是从样本中丢弃此观察结果,将其保留在原处通常会提供更多信息,接受此观察结果有一些特殊之处,并通过包含一个 11 的虚拟变量来准确说明这一点code> 表示该观察结果,0 表示所有其他观察结果。现在数据框的最后一部分应如下所示:

enter image description here

3。使用虚拟变量识别异常值

现在,你的 qqplot 将如下所示:

enter image description here

这是您的模型摘要:

=================================================================================
                    coef    std err          t      P>|t|      [0.025      0.975]
---------------------------------------------------------------------------------
sepal_width       1.4512      0.015     94.613      0.000       1.420       1.482
outlier_dummy    -6.6097      0.394    -16.791      0.000      -7.401      -5.819
==============================================================================
Omnibus:                        1.917   Durbin-Watson:                   2.188
Prob(Omnibus):                  0.383   Jarque-Bera (JB):                1.066
Skew:                           0.218   Prob(JB):                        0.587
Kurtosis:                       3.558   Cond. No.                         27.0
==============================================================================

请注意,虚拟变量的包含会更改 sepal_widht 的系数估计值,以及 SkewnessKurtosis 的值。这就是异常值对您的模型产生的影响的简短版本。

完整代码:

import numpy as np
import pandas as pd
import statsmodels.api as sm
from matplotlib import pyplot as plt
import seaborn as sns

# sample data
df = pd.DataFrame(sns.load_dataset('iris'))

# subset of sample data
df=df[df['species']=='setosa']

# add column for dummy variable
df['outlier_dummy']=0

# append line with extreme value for sepal width
# as well as a dummy variable = 1 for that row.
df.loc[len(df)] = [5,8,1.4, 0.3, 'setosa', 1]

# define independent variables
x=['sepal_width', 'outlier_dummy']

# run regression
mod_fit = sm.OLS(df['sepal_length'], df[x]).fit()
res = mod_fit.resid

fig = sm.qqplot(res)
plt.show()
mod_fit.summary()

关于Python:如何处理回归 Q-Q 图中的异常值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59574109/

相关文章:

python - 是否有与 PHP-FPM 等效的 Python?

python - 值错误 : Unknown type <class 'redis.client.StrictPipeline' >

python - 创建朝着特定点增加的随机数据

python - 凯拉斯/ tensorflow : Weird dropout behaviour

machine-learning - 异构特征空间上的SVM学习

python - 如何转换时间范围(19 :00-20:00) to a TimeStamp/Date Time object?

r - 向前逐步回归

python - 从 python 中的 mako Template.render() 对象中获取变量?

用于桌面应用程序回归测试的自动化测试工具

r - 获取每个分类变量的 lm 估计值