如何在 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() 函式,並將必要的資料作為輸入傳遞,以便執行 Grubbs 檢驗。
示例
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() 函式以從提供的資料集中獲取最大異常值,以獲取單側 Grubbs 檢驗。
示例
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 庫進行實現。

語法
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 檢驗將從任一側檢測異常值。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP