-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hyperparameter_Tuning_SVM.Rmd
251 lines (155 loc) · 7.41 KB
/
Hyperparameter_Tuning_SVM.Rmd
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
---
title: Multinomial Regression using Hyperparameter Optimization with SVM
author: "Niharika Sharma"
date: "September 19, 2019"
output:
html_document: default
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\Users\\NIHA\\AppData\\Local\\Programs\\MikTex 2.9\\miktex\\bin", sep=.Platform$path.sep))
knitr::opts_chunk$set(echo = FALSE)
#install.packages('tinytex')
#tinytex::install_tinytex()
```
```{r loadpackages, warning=FALSE}
pacman::p_load(e1071, tidyverse, caret, rmarkdown, corrplot, readxl, ModelMetrics)
theme_set(theme_classic())
```
## Defining data
Certain columns have been deleted due to redundancy issues.
The List of the deleted columns:
PriceCH
PriceMM
Store7
ListPriceDiff
STORE
More crucial attribute than the prices of both these brands is the price difference between both these brands.
We already have the Store ID in the dataset so STORE and Store7 need not be included in the dataset.
List Price difference is a redundant attribute as we already have the Sales Price Difference in the data.
Removing such redundant variables in the dataset and keeping more relevant attributes will help us avoid the overfitting of the model.
Creating a Partition for the train and test datasets.
```{r dataload , echo=TRUE}
juice <- read_csv("juice.csv")
drops <- c("PriceCH", "PriceMM" ,"Store7", "ListPriceDiff", "STORE")
juice <- select(juice,-drops)
juice$Purchase <- as.factor(juice$Purchase)
set.seed(123)
train.index <- createDataPartition(juice$Purchase , p=0.8, list=FALSE)
juice_train <- juice[train.index,]
juice_test <- juice[-train.index,]
```
## Linear SVM Model
```{r LinearSVM, echo=TRUE}
set.seed(123)
svm_lnr <- svm(Purchase~., data=juice_train, kernel="linear", cost=0.01)
summary(svm_lnr)
pred_lnr1 <- predict( svm_lnr, juice_train)
pred_lnr2 <- predict( svm_lnr, juice_test)
confmatrix_lnr1 <- table(Actual = juice_train$Purchase, Predicted = pred_lnr1)
confmatrix_lnr1
confmatrix_lnr2 <- table(Actual = juice_test$Purchase, Predicted = pred_lnr2)
confmatrix_lnr2
```
Train Accuracy Score - `r sum(diag(confmatrix_lnr1))/sum(confmatrix_lnr1) `
Test Accuracy Score - `r sum(diag(confmatrix_lnr2))/sum(confmatrix_lnr2) `
Train Error Rate - `r 1-sum(diag(confmatrix_lnr1))/sum(confmatrix_lnr1) `
Test Error Rate - `r 1-sum(diag(confmatrix_lnr2))/sum(confmatrix_lnr2) `
## Grid Search for Linear SVM using tune function
```{r Lnr_SVM_Tune , echo=TRUE}
cost_arr=seq(0.01, 10, by=0.1)
set.seed(123)
tune_lnr <- tune(svm, Purchase~., data = juice_train, kernel="linear",
ranges = list(cost=cost_arr))
summary(tune_lnr)
plot(tune_lnr)
pred_lnr1 <- predict( tune_lnr$best.model, juice_train)
pred_lnr2 <- predict( tune_lnr$best.model, juice_test)
confmatrix_lnr1 <- table(Actual = juice_train$Purchase, Predicted = pred_lnr1)
confmatrix_lnr1
confmatrix_lnr2 <- table(Actual = juice_test$Purchase, Predicted = pred_lnr2)
confmatrix_lnr2
```
Train Accuracy Score - `r sum(diag(confmatrix_lnr1))/sum(confmatrix_lnr1) `
Test Accuracy Score - `r sum(diag(confmatrix_lnr2))/sum(confmatrix_lnr2) `
Train Error Rate - `r 1-sum(diag(confmatrix_lnr1))/sum(confmatrix_lnr1) `
Test Error Rate - `r 1-sum(diag(confmatrix_lnr2))/sum(confmatrix_lnr2) `
##############################################################################
## RBF Model
```{r RBF , echo=TRUE}
set.seed(123)
svm_rbf <- svm(Purchase~., data=juice_train, cost=0.01)
summary(svm_rbf)
pred_rbf1 <- predict( svm_rbf, juice_train)
pred_rbf2 <- predict( svm_rbf, juice_test)
confmatrix_rbf1 <- table(Actual = juice_train$Purchase, Predicted = pred_rbf1)
confmatrix_rbf1
confmatrix_rbf2 <- table(Actual = juice_test$Purchase, Predicted = pred_rbf2)
confmatrix_rbf2
```
Train Accuracy Score - `r sum(diag(confmatrix_rbf1))/sum(confmatrix_rbf1) `
Test Accuracy Score - `r sum(diag(confmatrix_rbf2))/sum(confmatrix_rbf2) `
Train Error Rate - `r 1-sum(diag(confmatrix_rbf1))/sum(confmatrix_rbf1) `
Test Error Rate - `r 1-sum(diag(confmatrix_rbf2))/sum(confmatrix_rbf2) `
## Tuned RBF Model
```{r RBF_SVM_Tune , echo=TRUE}
set.seed(123)
tune_rbf <- tune(svm, Purchase~., data = juice_train,
ranges = list(cost=cost_arr))
summary(tune_rbf)
plot(tune_rbf)
pred_rbf1 <- predict( tune_rbf$best.model, juice_train)
pred_rbf2 <- predict( tune_rbf$best.model, juice_test)
confmatrix_rbf1 <- table(Actual = juice_train$Purchase, Predicted = pred_rbf1)
confmatrix_rbf1
confmatrix_rbf2 <- table(Actual = juice_test$Purchase, Predicted = pred_rbf2)
confmatrix_rbf2
```
Train Accuracy Score - `r sum(diag(confmatrix_rbf1))/sum(confmatrix_rbf1) `
Test Accuracy Score - `r sum(diag(confmatrix_rbf2))/sum(confmatrix_rbf2) `
Train Error Rate - `r 1-sum(diag(confmatrix_rbf1))/sum(confmatrix_rbf1) `
Test Error Rate - `r 1-sum(diag(confmatrix_rbf2))/sum(confmatrix_rbf2) `
####################################################################
## Polynomial Model
```{r Poly , echo=TRUE}
set.seed(123)
svm_poly <- svm(Purchase~., data=juice_train, kernel ="poly", cost=0.01, degree=2)
summary(svm_poly)
pred_poly1 <- predict(svm_poly, juice_train)
pred_poly2 <- predict(svm_poly, juice_test)
confmatrix_poly1 <- table(Actual = juice_train$Purchase, Predicted = pred_poly1)
confmatrix_poly1
confmatrix_poly2 <- table(Actual = juice_test$Purchase, Predicted = pred_poly2)
confmatrix_poly2
```
Train Accuracy Score - `r sum(diag(confmatrix_poly1))/sum(confmatrix_poly1) `
Test Accuracy Score - `r sum(diag(confmatrix_poly2))/sum(confmatrix_poly2) `
Train Error Rate - `r 1-sum(diag(confmatrix_poly1))/sum(confmatrix_poly1) `
Test Error Rate - `r 1-sum(diag(confmatrix_poly2))/sum(confmatrix_poly2) `
```{r Poly_SVM_Tune , echo=TRUE}
set.seed(123)
tune_poly <- tune(svm, Purchase~., data = juice_train, kernel="poly", degree=2,
ranges = list(cost=cost_arr))
summary(tune_poly)
plot(tune_poly)
pred_poly1 <- predict( tune_poly$best.model, juice_train)
pred_poly2 <- predict( tune_poly$best.model, juice_test)
confmatrix_poly1 <- table(Actual = juice_train$Purchase, Predicted = pred_poly1)
confmatrix_poly1
confmatrix_poly2 <- table(Actual = juice_test$Purchase, Predicted = pred_poly2)
confmatrix_poly2
```
Train Accuracy Score - `r sum(diag(confmatrix_poly1))/sum(confmatrix_poly1) `
Test Accuracy Score - `r sum(diag(confmatrix_poly2))/sum(confmatrix_poly2) `
Train Error Rate - `r 1-sum(diag(confmatrix_poly1))/sum(confmatrix_poly1) `
Test Error Rate - `r 1-sum(diag(confmatrix_poly2))/sum(confmatrix_poly2) `
## Conclusion
After comparing the Train and Test Scores for all the models:
<u>Basic Models</u>
SVM with Linear Kernel is the best in case of Basic Models with the least Error scores for both Train and Test datasets.
<u>Tuned Models</u>
For both RBF and Linear Kernels, the cost parameter for the best model is 0.31 and the scores are almost equal.
For Polynomial, the cost parameter is 9.61 but the model isn't as good.
Taking the Accuracy rate and Error Rate into Consideration, both tuned models - SVM with Kernel RBF and Kernel Linear are good but RBF is slighlty better as there are lower chances of Overfitting.
#####################################################################################