k-nearest neighbors-afstandsscore

Introductie tot anomaliedetectie in R

Alastair Rushworth

Data Scientist

Meubelmaten

plot(Width ~ Height, data = furniture)

Introductie tot anomaliedetectie in R

k-nearest neighbors (kNN)-afstand

Afwijkingen liggen vaak ver van hun buren

Introductie tot anomaliedetectie in R

Inputs voor afstandsmatrixberekening

library(FNN)
furniture_knn <- get.knn(data = furniture, k = 5)

 

Argumenten

  • data: matrix met data
  • k: aantal buren
Introductie tot anomaliedetectie in R

Uitvoer afstandsmatrix

get.knn() geeft twee matrices terug

names(furniture_knn)
"nn.index" "nn.dist" 

Afstandsmatrix

head(furniture_knn$nn.dist, 3)
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 5.128300 5.367791 5.390801 5.740713 8.477025
[2,] 4.300093 5.367791 6.159139 7.091966 7.428176
[3,] 3.047502 3.545978 4.426266 5.006570 5.654202
Introductie tot anomaliedetectie in R

kNN-afstandsscore

Gemiddelde afstand tot naaste buren

furniture_score <- rowMeans(furniture_knn$nn.dist)

 

Grootste score?

which.max(furniture_score)
29
Introductie tot anomaliedetectie in R

Laten we oefenen!

Introductie tot anomaliedetectie in R

Preparing Video For Download...