Python 如何执行Grubbs测试

Python 如何执行Grubbs测试

Grubbs测试是一种统计假设检验方法,用于检测数据集中的异常值。异常值是分散数据分布的观测值,也被称为异常值。带有异常值的数据集比具有正态/高斯分布的数据更容易过拟合。因此,在进行机器学习建模之前,有必要处理异常值。在处理之前,我们必须检测和定位数据集中的异常值。最常用的异常值检测技术是QQ图、四分位间距和Grubbs统计检验。然而,本文将只讨论Grubbs测试来检测异常值。您将学到:Grubbs测试是什么以及如何在Python中实现它。

什么是异常值

异常值是数值上与其他数据值有很大差距的数据观测值。这些值位于正态分布数据范围之外。数据集必须包含在均值的第一标准差下的67%记录,在第二标准差下的95%数据,以及在第三标准差下的99.7%点,以达到正态分布。换句话说,数据点应该位于第一和第三四分位数范围之间。我们将位于第一四分位数以下和第三四分位数以上的记录视为异常值或异常。

Grubbs统计假设检验

Grubbs检验与任何其他统计假设检验一样,也会接受或拒绝零(H0)或替代(H1)假设。Grubbs测试是一种检测数据集中异常值的测试。

我们可以以两种方式进行Grubbs测试:单边测试和双边测试,对于一个具有至少七个变量的单变量数据集,或接近正态分布的样本。该测试也被称为极端学生化离差测试或最大标准化残差测试。

Grubbs测试使用以下假设 –

  • 零假设(H0):数据集没有异常值。
  • 备择假设(H1):数据集只有一个异常值。

Python中的Grubbs测试

Python凭借其庞大的库集合可以解决任何编程挑战。这些库提供内置方法,可直接用于执行任何操作、统计检验等。同样,Python拥有一个库,其中包含用于执行Grubbs测试以检测异常值的方法。但是,我们将探讨在Python中实现Grubbs测试的两种方式:使用库中的内置函数和从头开始实现公式。

Outliers库和Smirnov_grubbs

让我们首先使用以下命令安装outlier_utils库。

!pip install outlier_utils

现在让我们创建一个包含异常值的数据集,并进行Grubbs检验。

双侧Grubbs检验

语法

grubbs.test(data, alpha=.05)

参数

data - 数值向量数据。

alpha - 检验的显著性水平。

说明

在这种方法中,用户必须使用outliers软件包中的smirnov_grubbs.test()函数,并将必要的数据作为输入,以运行Grubb’s检验。

示例

import numpy as np
from outliers import smirnov_grubbs as grubbs

#define data
data = np.array([ 5, 14, 15, 15, 14, 19, 17, 16, 20, 22, 8, 21, 28, 11, 9, 29, 40])

#perform Grubbs' test
grubbs.test(data, alpha=.05)

输出

array([ 5, 14, 15, 15, 14, 19, 17, 16, 20, 22,  8, 21, 28, 11,  9, 29])

以上代码简单地从加载库和数据开始,最后使用“test”方法对此数据执行Grubbs测试。这个测试可以检测出离群值,即左侧和右侧的值,或第一四分位数以下和第三四分位数以上的值。数据只有一个离群值,即40,使用Grubbs测试被移除。

单边 Grubbs 测试

语法

grubbs.max_test(data, alpha=.05)

解释

在这种方法中,用户必须调用 grubbs.min_test() 函数来从提供的数据集中获取最小异常值,或者调用 grubbs.max_test() 函数来从提供的数据集中获取最大异常值,以获得单侧Grubb’s检验。

示例

import numpy as np
from outliers import smirnov_grubbs as grubbs

#define data
data = np.array([5, 14, 15, 15, 14, 19, 17, 16, 20, 22, 8, 21, 28, 11, 9, 29, 40])

#perform Grubbs' test for minimum value is an outlier
print(grubbs.min_test(data, alpha=.05)) 

#perform Grubbs' test for minimum value is an outlier
grubbs.max_test(data, alpha=.05)

输出

[ 5 14 15 15 14 19 17 16 20 22  8 21 28 11  9 29 40]
array([ 5, 14, 15, 15, 14, 19, 17, 16, 20, 22,  8, 21, 28, 11,  9, 29])

单侧Grubbs检验可以检测出位于第一四分位数以下或第三四分位数以上的异常值。我们可以看到min_test方法从数据的最小一侧移除了异常值,max_test方法从数据的顶部一侧移除了异常值。

公式实现

在这里,我们将在Python中实现以下Grubbs测试公式。我们将使用Numpy和Scipy库进行实现。

Python 如何执行Grubbs测试

语法

g_calculated = numerator/sd_x
g_critical = ((n - 1) * np.sqrt(np.square(t_value_1))) / (np.sqrt(n) * np.sqrt(n - 2 + np.square(t_value_1)))

步骤

实现的步骤如下:

  • 计算数据集的平均值。

  • 计算数据集的标准差。

  • 为实现Grubbs测试公式,通过从数据集中的每个值减去其平均值来计算分子。

  • 将分子的值除以标准差以得到计算得分。

  • 计算相同值的临界得分。

  • 如果临界值大于计算值,则数据集中没有异常值,否则存在异常值。

示例

import numpy as np
import scipy.stats as stats
## define data
x = np.array([12,13,14,19,21,23])
y = np.array([12,13,14,19,21,23,45])

## implement Grubbs test
def grubbs_test(x):
   n = len(x)
   mean_x = np.mean(x)
   sd_x = np.std(x)
   numerator = max(abs(x-mean_x))
   g_calculated = numerator/sd_x
   print("Grubbs Calculated Value:",g_calculated)
   t_value_1 = stats.t.ppf(1 - 0.05 / (2 * n), n - 2)
   g_critical = ((n - 1) * np.sqrt(np.square(t_value_1))) / (np.sqrt(n) * np.sqrt(n - 2 + np.square(t_value_1)))
   print("Grubbs Critical Value:",g_critical)
   if g_critical > g_calculated:
      print("We can see from the Grubbs test that the calculated value is less than the crucial value. Recognize the null hypothesis and draw the conclusion that there are no outliers\n")
   else:
      print("We see from the Grubbs test that the estimated value exceeds the critical value. Reject the null theory and draw the conclusion that there are outliers\n")
grubbs_test(x)
grubbs_test(y)

输出

Grubbs Calculated Value: 1.4274928542926593
Grubbs Critical Value: 1.887145117792422
We can see from the Grubbs test that the calculated value is less than the crucial value. Recognize the null hypothesis and draw the conclusion that there are no outliers

Grubbs Calculated Value: 2.2765147221587774
Grubbs Critical Value: 2.019968507680656
We see from the Grubbs test that the estimated value exceeds the critical value. Reject the null theory and draw the conclusion that there are outliers

根据Grubbs测试的结果,数组x没有任何离群值,但y有1个离群值。

结论

在本文中,我们了解了Python中的离群值和Grubbs测试。让我们总结一下本文的几个要点。

  • 离群值是超出四分位距范围的记录。

  • 离群值位于数据集的正常分布之外。

  • 我们可以使用Grubbs假设的统计测试来检测离群值。

  • 我们可以使用outlier_utils库中提供的内置方法执行Grubbs测试。

  • 双侧Grubbs测试可以检测和删除左右两侧的离群值。

  • 然而,单侧Grubbs测试将从任一侧检测离群值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程