处理分类特征

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...