開始使用免費開始

在 PyOD 中使用修正 z 分數

現在該用 pyod 來找離群值了。我們將使用 pyod 的 MAD 估計器來計算修正 z 分數。這個估計器在底層已經使用 median_abs_deviation 函式,因此不需要重複前面的步驟。

MAD 估計器已從 pyod.models.mad 載入,資料已提供為 prices。

本練習屬於課程

Python 中的異常偵測

檢視課程

練習說明

  • 以 threshold 設為 3.5 來初始化 MAD()。
  • 重新塑形 prices,使其成為 2D。
  • 同時擬合並預測,對 prices 產生內點/離群點標籤,使用 mad 的方法。
  • 篩選出離群點的 labels,離群點以 1 表示。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Initialize with a threshold of 3.5
mad = ____(____=____)

# Reshape prices to make it 2D
prices_reshaped = ____.____(-1, 1)

# Fit and predict outlier labels on prices_reshaped
labels = ____

# Filter for outliers
outliers = ____[____ == ____]

print(len(outliers))
編輯並執行程式碼