-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW07_SupportVectors.R
executable file
·300 lines (227 loc) · 11.9 KB
/
W07_SupportVectors.R
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# =======================================================
# SVM : Part 1 : Support Vector Classifier
# =======================================================
# install.packages("e1071")
library(e1071)
# Pretty plotting routine for SVM models
prettyPlot <- function(svmModel, dataSet, gridSize) {
# create the grid of points to plot
x1.Pixels <- seq(from = as.numeric(min(dataSet[,1])),
to = as.numeric(max(dataSet[,1])), len = gridSize)
x2.Pixels <- seq(from = as.numeric(min(dataSet[,2])),
to = as.numeric(max(dataSet[,2])), len = gridSize)
pixelGrid <- expand.grid(X1 = x1.Pixels, X2 = x2.Pixels)
names(pixelGrid) <- names(dataSet)[1:2]
# predict the classes of the pixels
pixelData <- data.frame(pixelGrid)
classGrid <- predict(svmModel, newdata = pixelData)
# plot pixelGrid with colored class
plot(pixelGrid, pch = 20, cex = 0.2,
col = as.numeric(classGrid) + 1)
# plot the data points on top of this
points(dataSet[,1:2], pch = 19, cex = 1, col = as.numeric(dataSet[,3]) + 1)
# mark the actual support vectors now
points(dataSet[svmModel$index, 1:2], pch = 9, cex = 1.5)
}
# -------------------------------------------------------
# Load and explore the dataset
attach(iris)
help(iris)
str(iris)
# Truncate the data to a "2-dimensional 2-class" problem
irisData <- as.data.frame(iris[1:100,c(1,3,5)])
irisData$Species <- droplevels(irisData$Species)
str(irisData)
plot(irisData$Sepal.Length, irisData$Petal.Length,
col = as.numeric(irisData$Species) + 1, pch = 19,
xlab = "Sepal.Length", ylab = "Petal.Length")
# Split into Train and Validation sets
# Training Set : Validation Set = 70 : 30 (random)
train <- sample(nrow(irisData), 0.7*nrow(irisData), replace = FALSE)
irisTrain <- irisData[train,]
irisValid <- irisData[-train,]
plot(irisTrain$Sepal.Length, irisTrain$Petal.Length,
col = as.numeric(irisTrain$Species) + 1, pch = 19,
xlab = "Sepal.Length", ylab = "Petal.Length")
points(irisValid$Sepal.Length, irisValid$Petal.Length,
col = as.numeric(irisValid$Species) + 1, pch = 21)
# =======================================================
# SVM : Part 1A : Maximal Margin Classifier
# =======================================================
# Fit a Maximal Margin Classifier on the train set
# Set high cost to minimize number of support vectors
svmFit <- svm(Species ~ ., # formula for fit
data = irisTrain, # dataset for fit
kernel = "linear", # choose a kernel
cost = 1e6, # relaxation cost
scale = FALSE) # feature-scaling
summary(svmFit) # summary of the fitted model
# plot(svmFit, irisTrain) # default R plot for SVM
prettyPlot(svmFit, irisTrain, 100) # our prettyPlot for SVM
svmFit$index # index of the support vectors
svmFit$SV # the actual support vectors
# Predict the classes for the validation set
predValid <- predict(svmFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# =======================================================
# SVM : Part 1B : Support Vector Classifier
# =======================================================
# Fit a Support Vector Classifier on the train set
# Set variable cost to vary number of support vectors
svmFit <- svm(Species ~ ., # formula for fit
data = irisTrain, # dataset for fit
kernel = "linear", # choose a kernel
cost = 0, # relaxation cost
scale = FALSE) # feature-scaling
summary(svmFit) # summary of the fitted model
# plot(svmFit, irisTrain) # default R plot for SVM
prettyPlot(svmFit, irisTrain, 100) # our prettyPlot for SVM
svmFit$index # index of the support vectors
svmFit$SV # the actual support vectors
# Predict the classes for the validation set
predValid <- predict(svmFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# -------------------------------------------------------
# Fit an optimally tuned Support Vector Classifier
# by performing cross-validation with a cost range
tuneModel <- tune(svm,
Species ~ .,
data = irisTrain,
kernel = "linear",
ranges = list(cost = c(0.001, 0.01, 0.1, 1, 10, 100, 1000)),
scale = FALSE)
summary(tuneModel) # summary of tuning the cost
bestFit <- tuneModel$best.model # extract the best tuned model
summary(bestFit) # best model after tuning cost
# plot(bestFit, irisTrain) # default R plot for SVM
prettyPlot(bestFit, irisTrain, 100) # our prettyPlot for SVM
# Predict the classes for the validation set
predValid <- predict(bestFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# =======================================================
# SVM : Part 2 : Support Vector Machine
# =======================================================
# Load and explore the dataset
attach(iris)
help(iris)
str(iris)
# Truncate the data to a "2-dimensional 2-class" problem
irisData <- as.data.frame(iris[1:100,c(1,3,5)])
irisData$Species <- droplevels(irisData$Species)
str(irisData)
plot(irisData$Sepal.Length, irisData$Petal.Length,
col = as.numeric(irisData$Species) + 1, pch = 19,
xlab = "Sepal.Length", ylab = "Petal.Length")
# Split into Train and Validation sets
# Training Set : Validation Set = 70 : 30 (random)
train <- sample(nrow(irisData), 0.7*nrow(irisData), replace = FALSE)
irisTrain <- irisData[train,]
irisValid <- irisData[-train,]
plot(irisTrain$Sepal.Length, irisTrain$Petal.Length,
col = as.numeric(irisTrain$Species) + 1, pch = 19,
xlab = "Sepal.Length", ylab = "Petal.Length")
points(irisValid$Sepal.Length, irisValid$Petal.Length,
col = as.numeric(irisValid$Species) + 1, pch = 21)
# Fit a Support Vector machine to the train set using various kernels
# where the choice of the kernel depends on the nature of the dataset
# -------------------------------------------------------
# Polynomial Kernel
polFit <- svm(Species ~ ., # formula for fit
data = irisTrain, # dataset for fit
kernel = "polynomial", # ( gamma * (u.v) + coef0 ) ^ degree
gamma = 1, # gamma is required
coef0 = 1, # coef0 is required (default = 0)
degree = 5, # degree is required
cost = 10, # relaxation cost
scale = FALSE) # feature-scaling
summary(polFit) # summary of the fitted model
# plot(polFit, irisTrain) # default R plot for SVM
prettyPlot(polFit, irisTrain, 100) # our prettyPlot for SVM
polFit$index # index of the support vectors
polFit$SV # the actual support vectors
# Predict the classes for the validation set
predValid <- predict(polFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# -------------------------------------------------------
# Radial (Gaussian) Kernel
radFit <- svm(Species ~ ., # formula for fit
data = irisTrain, # dataset for fit
kernel = "radial", # exp( - gamma * (u - v) . (u - v) )
gamma = 1, # gamma is required
cost = 10, # relaxation cost
scale = FALSE) # feature-scaling
summary(radFit) # summary of the fitted model
# plot(radFit, irisTrain) # default R plot for SVM
prettyPlot(radFit, irisTrain, 100) # our prettyPlot for SVM
radFit$index # index of the support vectors
radFit$SV # the actual support vectors
# Predict the classes for the validation set
predValid <- predict(radFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# -------------------------------------------------------
# Sigmoid Kernel
sigFit <- svm(Species ~ ., # formula for fit
data = irisTrain, # dataset for fit
kernel = "sigmoid", # tanh( gamma * (u.v) + coef0 )
gamma = 0.01, # gamma is required
coef0 = 1, # coef0 is required (default = 0)
cost = 10, # relaxation cost
scale = FALSE) # feature-scaling
summary(sigFit) # summary of the fitted model
# plot(sigFit, irisTrain) # default R plot for SVM
prettyPlot(sigFit, irisTrain, 100) # our prettyPlot for SVM
sigFit$index # index of the support vectors
sigFit$SV # the actual support vectors
# Predict the classes for the validation set
predValid <- predict(radFit, newdata = irisValid) # prediction
table(predict = predValid, truth = irisValid$Species) # confusion matrix
# =======================================================
# SVM : Part 2B : Tuning the various SVM Kernels
# =======================================================
# Fit an optimally tuned Support Vector Machine on the train set
# by performing cross-validation with a range of parameter values
# -------------------------------------------------------
# Polynomial Kernel
tuneModel <- tune(svm,
Species ~ .,
data = irisTrain,
kernel = "polynomial", # ( gamma * (u.v) + coef0 ) ^ degree
ranges = list(gamma = c(0.01, 0.1, 1, 10, 100),
coef0 = c(0.01, 0.1, 1, 10, 100),
degree = c(1, 2, 3, 4, 5, 6 ,7),
cost = c(0.01, 0.1, 1, 10, 100)),
scale = FALSE)
summary(tuneModel) # summary of tuning parameters
bestFit <- tuneModel$best.model # extract the best tuned model
summary(bestFit) # best parameter-tuned model
# plot(bestFit, irisTrain) # default R plot for SVM
prettyPlot(bestFit, irisTrain, 100) # our prettyPlot for SVM
# -------------------------------------------------------
# Radial (Gaussian) Kernel
tuneModel <- tune(svm,
Species ~ .,
data = irisTrain,
kernel = "radial", # exp( - gamma * (u - v) . (u - v) )
ranges = list(gamma = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),
cost = c(0.001, 0.01, 0.1, 1, 10, 100, 1000)),
scale = FALSE)
summary(tuneModel) # summary of tuning parameters
bestFit <- tuneModel$best.model # extract the best tuned model
summary(bestFit) # best parameter-tuned model
# plot(bestFit, irisTrain) # default R plot for SVM
prettyPlot(bestFit, irisTrain, 100) # our prettyPlot for SVM
# -------------------------------------------------------
# Sigmoid Kernel
tuneModel <- tune(svm,
Species ~ .,
data = irisTrain,
kernel = "sigmoid", # tanh( gamma * (u.v) + coef0 )
ranges = list(gamma = c(0.01, 0.1, 1, 10, 100),
coef0 = c(0.01, 0.1, 1, 10, 100),
cost = c(0.001, 0.01, 0.1, 1, 10, 100, 1000)),
scale = FALSE)
summary(tuneModel) # summary of tuning parameters
bestFit <- tuneModel$best.model # extract the best tuned model
summary(bestFit) # best parameter-tuned model
# plot(bestFit, irisTrain) # default R plot for SVM
prettyPlot(bestFit, irisTrain, 100) # our prettyPlot for SVM