SVM 서포트 벡터 머신(Support vector machin) 이란, 기계 학흡의 분야 중 하나로 패턴 인식, 자료 분석을 위한 지도 학습 모델이다.
쉽게 말해 데이터 상에 있는 각 점들의 거리를 분석해 가장 먼 거리에 있는 점을 기준으로 Support vector를 형성하여 두개의 Support vector 중간에 초평면을 만들어 분류를 하는 방법이다.
library(kernlab)
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:kernlab':
##
## alpha
모델을 평가 하는데 필요 train/test sampling
df <- iris
training_sampling <- sort(sample(1:nrow(df),nrow(df)*0.7))
test_sampling <- setdiff(1:nrow(df), training_sampling)
training_set <- df[training_sampling,]
test_set <- df[test_sampling,]
ksvm() 함수 ksvm(종속변수 ~ 독립변수, data = df)
svm_m <- ksvm(Species ~ Petal.Length + Petal.Width, data = training_set)
svm_p <- predict(svm_m, newdata = test_set)
confusionMatrix(svm_p, test_set$Species)
## Confusion Matrix and Statistics
##
## Reference
## Prediction setosa versicolor virginica
## setosa 17 0 0
## versicolor 0 12 0
## virginica 0 2 14
##
## Overall Statistics
##
## Accuracy : 0.9556
## 95% CI : (0.8485, 0.9946)
## No Information Rate : 0.3778
## P-Value [Acc > NIR] : 2.61e-16
##
## Kappa : 0.933
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: setosa Class: versicolor Class: virginica
## Sensitivity 1.0000 0.8571 1.0000
## Specificity 1.0000 1.0000 0.9355
## Pos Pred Value 1.0000 1.0000 0.8750
## Neg Pred Value 1.0000 0.9394 1.0000
## Prevalence 0.3778 0.3111 0.3111
## Detection Rate 0.3778 0.2667 0.3111
## Detection Prevalence 0.3778 0.2667 0.3556
## Balanced Accuracy 1.0000 0.9286 0.9677
결과
Versicolor를 virginica로 인식한 것이 2개가 발생하였고, 종합적으로 약 95%의 정확도를 얻을 수 있었다.