カテゴリ変数の扱い

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で学ぶ異常検知入門

因子を用いたLOF

Gower距離 はカテゴリと数値の特徴が混在する点間距離を測定します

 

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

sat_distlof に渡す

sat_lof <- lof(sat_dist, k = 10)

Rで学ぶ異常検知入門

Gower距離行列の探索

  • オブジェクトを行列に変換
sat_distmat <- as.matrix(sat_dist)

 

  • 点間距離の最大・最小を確認
range(sat_distmat)
0.0000000 0.8680774
Rで学ぶ異常検知入門

Let's practice!

Rで学ぶ異常検知入門

Preparing Video For Download...