Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Kaggle
- pandas
- level 1
- matplotlib
- oracle
- Python
- 머신러닝
- 카카오
- SQL
- Oracel
- 알고리즘
- R
- 실습
- 파이썬
- sklearn
- 실기
- 튜닝
- 오라클
- seaborn
- level 2
- 코딩테스트
- 프로그래머스
- python3
- 빅데이터 분석 기사
- Numpy
- 데이터 분석
- 빅분기
Archives
- Today
- Total
라일락 꽃이 피는 날
[R] Support Vector Machine (SVM) 본문
728x90
데이터 불러오기
rawdata <- read.csv("wine.csv", header = TRUE)
rawdata$Class <- as.factor(rawdata$Class)
str(rawdata)

트레이닝-테스트 셋 분리 (7:3)
analdata <- rawdata
set.seed(2020)
datatotal <- sort(sample(nrow(analdata), nrow(analdata)*.7))
train <- rawdata[datatotal,]
test <- rawdata[-datatotal,]
train_x <- train[,1:13]
train_y <- train[,14]
test_x <- test[,1:13]
test_y <- test[,14]
선형 서포트 벡터 머신
ctrl <- trainControl(method = "repeatedcv", repeats = 5)
svm_linear_fit <- train(Class~.,
data = train,
method = "svmLinear",
trControl = ctrl,
preProcess = c("center", "scale"),
metric = "Accuracy")
svm_linear_fit

→ Accuracy : 0.9708541, Kappa : 0.9559268
예측
pred_test <- predict(svm_linear_fit, newdata = test)
confusionMatrix(pred_test, test$Class)

→ Accuracy : 0.9444, Kappa : 0.9117
변수중요도
importance_linear <- varImp(svm_linear_fit, scale = FALSE)
importance_linear

plot(importance_linear)

비선형 서포트 벡터 머신
ctrl <- trainControl(method = "repeatedcv", repeats = 5)
svm_poly_fit <- train(Class~.,
data = train,
method = "svmPoly",
trControl = ctrl,
preProcess = c("center", "scale"),
metric = "Accuracy")
svm_poly_fit

→ degree = 1, scale = 0.01, C = 0.5일 때, 정확도가 가장 높다.
plot(svm_poly_fit)

예측
pred_test <- predict(svm_poly_fit, newdata = test)
confusionMatrix(pred_test, test$Class)

→ Accuracy : 0.9259, Kappa : 0.8848
변수중요도
importance_poly <- varImp(svm_poly_fit, scale = FALSE)
importance_poly

plot(importance_poly)

728x90
'데이터 분석 > R' 카테고리의 다른 글
[R] PCA (주성분 분석) 2 (0) | 2021.07.08 |
---|---|
[R] PCA (주성분 분석) 1 (0) | 2021.07.08 |
[R] Decision Tree & Random Forest (0) | 2021.07.02 |
[R] Naive Bayes Classification (나이브 베이즈 분류) (0) | 2021.07.02 |
[R] Logistic Regression (로지스틱 회귀) (0) | 2021.06.30 |