含離群機率的 KNN
因為使用 contamination 時無法完全信任輸出結果,我們改用離群機率再做一次確認。離群機率通常更可靠。
資料集已載入為 females,且已匯入 KNN 估計器。
本練習屬於課程
Python 中的異常偵測
練習說明
- 以 20 個鄰居實例化
KNN。 - 計算離群機率。
- 建立布林遮罩,當離群機率超過 55% 時回傳 true。
- 使用
is_outlier從females中篩選離群點。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Instantiate a KNN with 20 neighbors and fit to `females`
knn = ____
knn.____
# Calculate probabilities
probs = ____
# Create a boolean mask
is_outlier = ____
# Use the boolean mask to filter the outliers
outliers = ____
print(len(outliers))