首頁  >  文章  >  後端開發  >  如何在Python中執行Grubbs檢驗

如何在Python中執行Grubbs檢驗

WBOY
WBOY轉載
2023-08-28 15:49:07798瀏覽

簡介

格拉布斯檢定是一種統計假設檢定方法,用於偵測資料集中的異常值。異常值是分配資料分佈的觀察結果,也稱為異常。具有異常值的資料集往往比具有常態/高斯分佈的資料更容易過度擬合。因此,在機器學習建模之前有必要解決異常值。在處理之前,我們必須檢測並定位資料集中的異常值。最受歡迎的異常值檢測技術是 QQPlot、四分位數間距和 Grubbs 統計檢定。然而,本文將僅討論用於檢測異常值的格魯布斯檢定。您將學習:什麼是 Grubbs 測試以及如何在 Python 中實現它。

什麼是異常值?

異常值是與其他資料值在數值上相距較遠的資料觀測值。這些值超出了常態分佈資料的範圍。資料集必須包含第一個標準差下的 67% 的記錄、第二個標準差下的 95% 的資料以及第三個標準差下的 99.7% 的點,才能達到常態分佈。換句話說,數據點應位於第一和第三四分位數範圍之間。我們將第一四分位數以下和第三四分位數以上的記錄視為異常值或異常值。

格拉布斯統計假設檢定

與任何其他統計假設檢定一樣,格拉布斯檢定也可以批准或拒絕原假設 (H0) 或替代假設 (H1)。 Grubbs 測試是偵測資料集中異常值的測試。

我們可以透過兩種方式執行格拉布斯檢定:單面檢定雙面檢定,用於單變數資料集或幾乎常態樣本至少有七個變數的分佈。此檢定也稱為極端學生化偏差檢定或最大歸一化殘差檢定。

格拉布斯檢定使用以下假設 -

  • Null (H0):資料集沒有異常值。

  • 替代 (H1):資料集只有一個異常值。

Python 中的格拉布斯測試

Python 憑藉其龐大的庫集合可以應對任何程式設計挑戰。這些庫提供了內建方法,可直接用於執行任何操作、統計測試等。同樣,Python 有一個庫,其中包含執行 Grubbs 測試以檢測異常值的方法。不過,我們將探索在 Python 中實作 Grubbs 測試的兩種方法:庫中的內建函數和從頭開始實作公式。

異常值庫和 Smirnov_grubbs

讓我們先使用以下指令安裝 outlier_utils 函式庫。

!pip install outlier_utils

現在讓我們製作一個包含異常值的資料集並執行 Grubbs 測試。

雙面格拉布檢定

文法

grubbs.test(data, alpha=.05)

參數

data - 資料值的數值向量。

alpha - 測試的顯著水準。

說明

在此方法中,使用者必須使用異常值套件中的 smirnov_grubbs.test() 函數,並將必要的資料作為輸入傳遞,以便執行 Grubb 的測試。

範例

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 測試。此測試可偵測兩側(左側和右側)的異常值,或低於第一四分位數和高於第三四分位數的值。資料只有 1 個異常值(40),已使用 Grubbs 檢定刪除。

單邊格拉布斯檢定

Synatx

grubbs.max_test(data, alpha=.05)

說明

在此方法中,使用者必須呼叫grubbs.min_test() 函數從提供的資料集中取得最小離群值,或是呼叫grubbs.max_test()函數從提供的資料集中取得最大離群值,以獲得單側格拉布檢定。

範例

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])

單側格拉布斯檢定檢測第一四分位數以下或第三四分位數以上的異常值。我們可以看到,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)))

演算法

實作步驟如下 -

  • 計算資料集值的平均值。

  • 計算資料集值的標準差。

  • 要實現格拉布斯檢定公式,請透過從資料集中的每個值的平均值中減去其值來計算分子。

  • 將分子值除以標準差即可得到計算的分數。

  • 計算相同值的臨界分數。

  • 如果臨界值大於計算值,則資料集中不存在異常值,否則存在異常值。

範例

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

Grubb 測試的結果顯示陣列 x 沒有任何異常值,但 y 有 1 個異常值。

結論

我們在本文中了解了 Python 中的離群值和 Grubbs 測試。讓我們用一些重點來總結這篇文章。

  • 異常值是指超出四分位數範圍的記錄。

  • 異常值不符合資料集的常態分佈。

  • 我們可以使用格拉布斯假設統計檢定來偵測異常值。

  • 我們可以使用 outlier_utils 函式庫中提供的內建方法來執行 Grubbs 測試。

  • 雙面格拉布斯檢定可偵測並刪除左側和右側的異常值。

  • 然而,單側格拉布斯檢定將偵測任一側的異常值。

以上是如何在Python中執行Grubbs檢驗的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除