開始使用免費開始

含類別變數的 LOF

lof() 函式可以接受數值型資料框或距離矩陣作為輸入來計算 LOF 分數。這個練習中,你會練習使用 Gower 距離計算距離矩陣,接著將其傳入 lof() 進行評分。

和前一題一樣,已經幫你預先載入 thyroid 資料,且其中字元欄位已轉為因子(factor),可直接使用。

本練習屬於課程

R 中的異常偵測入門

檢視課程

練習說明

  • thyroid 資料計算 Gower 距離矩陣,並將結果指定給新物件 thyroid_dist
  • 使用 thyroid_dist,在 k = 10 的情況下為每位病患產生 LOF。
  • 印出矩陣 thyroid_dist 中距離值的範圍。

動手互動練習

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

# Calculate Gower's distance matrix
thyroid_dist <- daisy(___, metric = ___)

# Generate LOF scores for thyroid data
thyroid_lof <- lof(thyroid_dist, k = 10)

# Range of values in the distance matrix
___(as.matrix(___))
編輯並執行程式碼