用 z-score 尋找離群值
常態分配在自然界中非常普遍,也是最常見的分配。這也是為什麼 z-score 方法常能快速偵測離群值。
回想影片中的經驗法則:如果某個樣本與平均數相差超過 3 個標準差,你可以把它視為極端值。
不過,也要記得使用 z-score 方法時要謹慎。只有在你有把握資料來自常態分配時,這個方法才合適。否則結果可能會產生誤導。
prices 的分配已為你載入。
本練習屬於課程
Python 中的異常偵測
練習說明
- 從對應的
scipy模組匯入zscore函式。 - 計算
prices的 z-score,並將結果存到scores。 - 建立名為
is_over_3的布林遮罩,檢查scores的絕對值是否大於 3。 - 使用此遮罩篩選
prices中的離群值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the zscores function
from scipy.____ import ____
# Find the zscores of prices
scores = ____(____)
# Check if the absolute values of scores are over 3
is_over_3 = ____
# Use the mask to subset prices
outliers = ____[____]
print(len(outliers))