使用 IQR 計算離群值界線
將離群值視覺化通常只是偵測離群值的第一步。若要更進一步,你需要撰寫程式碼,將離群值從分佈中篩選出來。
在這個練習中,你會實作箱形圖在背後進行的第一個步驟。也就是說,你會手動計算離群值的下界與上界。
分佈資料已存於 prices。
本練習屬於課程
Python 中的異常偵測
練習說明
- 計算 prices 的第一與第三四分位數,分別存為
q1與q3。 - 將 IQR 分數計算後存入
IQR。 - 將離群值下界計算後存入
lower_limit。 - 將離群值上界計算後存入
upper_limit。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate the 25th and 75th percentiles
q1 = prices.____(____)
q3 = prices.____(____)
# Find the IQR
IQR = ____
factor = 2.5
# Calculate the lower limit
lower_limit = ____ - ____
# Calculate the upper limit
upper_limit = ____ + ____