處理類別型特徵

R 中的異常偵測入門

Alastair Rushworth

Data Scientist

檢查欄位類別

單一欄位的類別

class(sat$V1)
"numeric"

所有欄位的類別

sapply(X = sat, FUN = class)
      label          V1          V2          V3         V4          V5          V6    high_low
  "numeric"   "numeric"   "numeric"   "numeric"  "numeric"   "numeric"   "numeric" "character"
R 中的異常偵測入門

Isolation forest

將類別特徵編碼為 factor

sat$high_low <- as.factor(sat$high_low)

class(sat$high_low)
"factor"

訓練 isolation forest

sat_for <- iForest(sat[, -1], nt = 100)
R 中的異常偵測入門

含 factor 的 LOF

Gower 距離 可衡量同時含類別與數值特徵的點間距離

 

library(cluster)
sat_dist <- daisy(sat[, -1], metric = "gower")

sat_dist 傳給 lof

sat_lof <- lof(sat_dist, k = 10)

R 中的異常偵測入門

探索 Gower 距離矩陣

  • 物件轉換為矩陣
sat_distmat <- as.matrix(sat_dist)

 

  • 尋找點間距離的最大與最小值
range(sat_distmat)
0.0000000 0.8680774
R 中的異常偵測入門

一起來練習吧!

R 中的異常偵測入門

Preparing Video For Download...