-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM_Hyperparametertuning.Rmd
More file actions
executable file
·228 lines (159 loc) · 4.81 KB
/
SVM_Hyperparametertuning.Rmd
File metadata and controls
executable file
·228 lines (159 loc) · 4.81 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: "Support Vector Machines"
author: "Sushama_Rangarajan"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
if(!require("pacman")) install.packages("pacman")
pacman::p_load(e1071, ggplot2, caret, rmarkdown, corrplot)
theme_set(theme_classic())
```
Reading the data and partitioning it with 80% train and 20% test
```{r dataloading}
j1 <- read.csv("juice.csv")
j1$Purchase <- as.factor(j1$Purchase)
```
```{r Partitioning}
set.seed(123)
part <- createDataPartition(j1$Purchase, p=0.8, list = FALSE)
j1.train <- j1[part,]
j1.test <- j1[-part,]
```
# Linear Kernel with cost = 0.01
```{r Linear SVM}
svm1 <- svm(Purchase~., data=j1.train,kernel = "linear",cost=0.01,type = "C-classification")
summary(svm1)
```
Predicting the test and train dataset with linear SVM
```{r Predicting with linear SVM}
#prediction of test data
pr1 <- predict(svm1,j1.test)
conf.test <- table(Predicted = pr1, Actual = j1.test$Purchase)
conf.test
sensitivity(conf.test)
specificity(conf.test)
#Test Error rate
(1- (sum(diag(conf.test)) / sum(conf.test)))
#prediction of train data
pr11 <- predict(svm1,j1.train)
conf.train<- table(Predicted = pr11, Actual = j1.train$Purchase)
conf.train
#Train Error rate
(1- (sum(diag(conf.train)) / sum(conf.train)))
```
Hyperparameter Tuning:
```{r Tuning Linear model}
set.seed(123)
#sequence was modified after multiple iterations
tune.linear <- tune(svm, Purchase ~.,data = j1.train, kernel = "linear", ranges = list(cost = seq(0.01,10.1,by=0.3)))
summary(tune.linear)
plot(tune.linear)
best1 <- tune.linear$best.model
summary(best1)
```
#Prediction:
```{r Predicting with best linear model}
#Prediction using test data
bestpred1 <- predict(best1,j1.test)
conf.test1 <- table(predicted = bestpred1, actual = j1.test$Purchase)
conf.test1
#Test Error rate
(1- (sum(diag(conf.test1)) / sum(conf.test1)))
#Prediction using train data
bestpred2 <- predict(best1,j1.train)
conf.train1 <- table(predicted = bestpred2, actual = j1.train$Purchase)
conf.train1
#Train Error rate
(1- (sum(diag(conf.train1)) / sum(conf.train1)))
```
```{r Radial SVM}
set.seed(123)
svm2<- svm(Purchase~., data=j1.train, cost = 0.01)
summary(svm2)
```
Prediction :
```{r Predicting with Radial SVM}
#prediction using test data
pr2 <- predict(svm2,j1.test)
conf.test2 <- table(Predicted = pr2, Actual = j1.test$Purchase)
conf.test2
#Test Error rate
(1- (sum(diag(conf.test2)) / sum(conf.test2)))
#prediction using train data
pr21 <- predict(svm2,j1.train)
conf.train2<- table(Predicted = pr21, Actual = j1.train$Purchase)
conf.train2
#Train Error rate
(1- (sum(diag(conf.train2)) / sum(conf.train2)))
```
Hyperparameter Tuning
```{r Radial - Tuning}
set.seed(123)
#sequence was modified after multiple iterations
tune.radial <- tune(svm, Purchase ~.,data = j1.train, kernel = "radial", ranges = list(cost = seq(0.01,10.1,by=1)))
summary(tune.radial)
plot(tune.radial)
best2 <- tune.radial$best.model
summary(best2)
```
Prediction
```{r Predicting with Radial best model}
#Prediction using test
bestpred2 <- predict(best2,j1.test)
conf.test3 <- table(predicted = bestpred2, actual = j1.test$Purchase)
conf.test3
#Test Error rate
(1- (sum(diag(conf.test3)) / sum(conf.test3)))
#Prediction using train
bestpred3 <- predict(best2,j1.train)
conf.train3 <- table(predicted = bestpred3, actual = j1.train$Purchase)
conf.train3
#Train Error rate
(1- (sum(diag(conf.train3)) / sum(conf.train3)))
```
# Polynomial Kernel with degree =2 and cost = 0.01
```{r Polynomial SVM}
svm3<- svm(Purchase~., data=j1.train,kernel = "polynomial",degree = 2, cost = 0.01)
summary(svm3)
```
```{r Predicting with Polynomial SVM}
#Prediction using test
pr3 <- predict(svm3,j1.test)
conf.test4 <- table(Predicted = pr3, Actual = j1.test$Purchase)
conf.test4
#Test Error rate
(1- (sum(diag(conf.test4)) / sum(conf.test4)))
#prediction of train data
pr31 <- predict(svm3,j1.train)
conf.train4<- table(Predicted = pr31, Actual = j1.train$Purchase)
conf.train4
#Train Error rate
(1- (sum(diag(conf.train4)) / sum(conf.train4)))
```
Hyperparameter Tuning
```{r Polynomial - Tuning}
#Hyper parameter Tuning
set.seed(123)
#sequence was modified after multiple iterations
tune.poly <- tune(svm, Purchase ~.,data = j1.train, kernel = "polynomial", degree = 2, ranges = list(cost = seq(0.01,10.1,by=0.5)))
summary(tune.poly)
plot(tune.poly)
best3 <- tune.poly$best.model
summary(best3)
```
```{r Predicting with best polynomial model}
#Prediction using test
bestpred4 <- predict(best3,j1.test)
conf.test5 <- table(predicted = bestpred4, actual = j1.test$Purchase)
conf.test5
#Test Error rate
(1- (sum(diag(conf.test5)) / sum(conf.test5)))
#Prediction using train
bestpred5 <- predict(best3,j1.train)
conf.train5 <- table(predicted = bestpred5, actual = j1.train$Purchase)
conf.train5
#Train Error rate
(1- (sum(diag(conf.train5)) / sum(conf.train5)))
```