>RE::VISION CRM

R 데이터 분석

[R분석] Anomaly Detection과 EDA 결합 (IsolationForest) 활용

YONG_X 2018. 9. 5. 10:08






# Anomaly Detection?




## 관련행사 :: 

[인공지능컨퍼런스] AI서밋 Workshop - 머신러닝 실전: 예측과 이상패턴 탐지

## 링크 ::  http://blog.daum.net/revisioncrm/427



#--------[ for video tutorial on KBO hitting example ]--------

# 유튜브 영상 링크 :: https://www.youtube.com/watch?v=mxegXZWUjMU


# IsolationForest 패키지 인스톨 

# devtools::install_github("yanyachen/IsolationForest")

# install.packages("IsolationForest", repos="http://R-Forge.R-project.org")

# [주의] 최신버전의 R에서만 정상 작동


library(IsolationForest)

# Exploratory Anomaly Detection Practice Using KBO 2018 Hitting Data


# 오픈소스 R을 사용한 anomaly detection 예제를 통한  

# 탐색적 분석(EDA) Tutorial  전용준2018. 리비젼컨설팅

# R anomaly detection Practice EDA 전용준2018


# KBO 2018 아시안게임브레이크 직전까지의 타자 성적 통계 데이터 로딩

hit0 <- read.csv("https://t1.daumcdn.net/cfile/blog/99B515435B87998B07?download")

hit0 <- hit0[,!(names(hit0) %in% c("선수명","순위","팀명"))]


hit0 <- hit0[,(names(hit0) %in% c("HR","PA", "BB.K", "RBI","TB"))]


library(IsolationForest)

# train a model of Isolation Forest

tr<-IsolationTrees(hit0, ntree=1000, 

                   rFactor=0.5, nmin=3)

#evaluate anomaly score

as<-AnomalyScore(hit0,tr)

# show anomaly score

plot(density(as$outF ))

sort(as$outF, decreasing=T)[1:2]


# check what matters the most 

library(party)


hit0v <- hit0 

hit0v$outF <- as$outF 

hit0v1 <- hit0v[,!(names(hit0v) %in% "선수명")]


ctv1 <- ctree(outF~., data=hit0v1)

plot(ctv1)


# 랜덤포리스트를 활용한 변수별 Anomaly Score 생성 영향도 파악

# install.packages("randomForest")

library(randomForest)


rfv1 <- randomForest(outF~., data=hit0v1, 

                     nodesize=3, ntree=3000)

varImpPlot(rfv1)


hit0 <- read.csv("https://t1.daumcdn.net/cfile/blog/99B515435B87998B07?download")

hit0$outF <- as$outF 

# head(hit0[order(hit0$outF, decreasing=T),])


# UDF for standardize anomaly score

# 스코어의 스케일을 0~1 사이로 변환하기 위한 사용자정의함수

stnd <- function(x) {

  x1 <- (x-min(x)) / (max(x)-min(x))

  return(x1)

}

stndoutF<- stnd(as$outF)


# 핵심변수 두 가지를 활용한 scatterplot 생성

# 시각적 EDA 데이터 분석을 통한 Score 이해

plot(hit0$BB.K, hit0$TB/hit0$PA, 

     col=rgb(stndoutF, 0,1-stndoutF) , pch=19, cex=1.5,

     main="KBO Hitting (2018-08-16) - Anomaly Detection",

     xlab="볼넷삼진비율 BB/K",

     ylab="타수당루타 TB/PA",

     sub="red: anomaly")

text(hit0$BB.K, hit0$TB/hit0$PA, 

     labels=as.character(hit0$'선수명'),

     pos=2)

polygon(c(0.9, 1.22, 1.22),c(0.58, 0.58, 0.47), col=rgb(0,0,0.6,0.5), lty=0)

abline(v=c(quantile(hit0$BB.K, probs=c(0.9,0.8,0.7))), lty=2, col="grey")

points(hit0$BB.K, hit0$TB/hit0$PA, 

       col=rgb(stndoutF, 0,1-stndoutF) , pch=19, cex=1.5)



# ---- end of script -----------------------

# 사진을 누르면 유튜브 동영상으로 연결



#------- 바로 동영상 보기 ------------





hit20180919.csv



#---------------------------------------

# using solitude package -- a ranger-based implementation [Alternatively]


install.packages('solitude')


library(solitude)


# train a model of isolation forest from solitude


# mo <-isolation_forest(hit0, ntree=1000, rFactor=0.5, nmin=3)


mo <- isolation_forest(hit0, seed=1)


#evaluate anomaly score


as <- predict(mo, hit0)


summary(as)


# show anomaly score


plot(density(as))

sort(as, decreasing=T)[1:2]


hit20180919.csv
0.01MB
hit20180919.csv
0.01MB