forked from mhahsler/Introduction_to_Data_Mining_R_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap4_decisionboundary.R
370 lines (278 loc) · 12.4 KB
/
chap4_decisionboundary.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#' ---
#' title: "Additional R Code for Chapter 4 of Introduction to Data Mining: Classification: Comparing Decision Boundaries of Different Classifiers"
#' author: "Michael Hahsler"
#' output:
#' html_document:
#' toc: true
#' ---
#' This code covers chapter 4 of _"Introduction to Data Mining"_
#' by Pang-Ning Tan, Michael Steinbach and Vipin Kumar.
#'
#' ![CC](https://i.creativecommons.org/l/by/4.0/88x31.png)
#' This work is licensed under the
#' [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/). For questions please contact
#' [Michael Hahsler](http://michael.hahsler.net).
#'
#' # Decision Boundaries
#'
#' Classifiers create decision boundaries to discriminate between classes.
#' Different classifiers are able to create different shapes of decision
#' boundaries (e.g., some are strictly linear) and thus some classifiers
#' may perform better for certain datasets. This page visualizes the decision
#' boundaries found by several popular classification methods.
#'
#' The following plot adds the decision boundary by evaluating the classifier
#' at evenly spaced grid points. Note that low resolution
#' (to make evaluation faster) will make
#' the decision boundary look like it has small steps even if it is a
#' (straight) line.
library(scales)
library(tidyverse)
library(ggplot2)
library(caret)
decisionplot <- function(model, x, cl = NULL, predict_type = "class",
resolution = 100) {
if(!is.null(cl)) {
x_data <- x %>% dplyr::select(-all_of(cl))
cl <- x %>% pull(cl)
} else cl <- 1
k <- length(unique(cl))
# resubstitution accuracy
prediction <- predict(model, x_data, type = predict_type)
if(is.list(prediction)) prediction <- prediction$class
if(is.numeric(prediction))
prediction <- factor(prediction, labels = levels(cl))
else
prediction <- factor(prediction, levels = levels(cl))
cm <- confusionMatrix(data = prediction, reference = cl)
acc <- cm$overall["Accuracy"]
# evaluate model on a grid
r <- sapply(x[, 1:2], range, na.rm = TRUE)
xs <- seq(r[1,1], r[2,1], length.out = resolution)
ys <- seq(r[1,2], r[2,2], length.out = resolution)
g <- cbind(rep(xs, each = resolution), rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as_tibble(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
prediction <- predict(model, g, type = predict_type)
if(is.list(prediction)) prediction <- prediction$class
if(is.numeric(prediction))
prediction <- factor(prediction, labels = levels(cl))
else
prediction <- factor(prediction, levels = levels(cl))
g <- g %>% add_column(prediction)
ggplot(g, mapping = aes_string(
x = colnames(g)[1],
y = colnames(g)[2])) +
geom_tile(mapping = aes(fill = prediction)) +
geom_point(data = x, mapping = aes_string(
x = colnames(x)[1],
y = colnames(x)[2],
shape = colnames(x)[3]), alpha = .5) +
labs(subtitle = paste("Training accuracy:", round(acc, 2)))
}
#' # Iris Dataset
#'
#' For easier visualization, we use on two dimensions of the Iris dataset.
set.seed(1000)
data(iris)
iris <- as_tibble(iris)
# Three classes (MASS also has a select function)
x <- iris %>% dplyr::select(Sepal.Length, Sepal.Width, Species)
x
ggplot(x, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
#' _Note:_ There is some overplotting and you could use `geom_jitter()` instead of `geom_point()`.
#'
#' ## K-Nearest Neighbors Classifier
library(caret)
model <- x %>% knn3(Species ~ ., data = ., k = 1)
decisionplot(model, x, cl = "Species") + labs(title = "kNN (1 neighbor)")
model <- x %>% knn3(Species ~ ., data = ., k = 10)
decisionplot(model, x, cl = "Species") + labs(title = "kNN (10 neighbor)")
#' ## Naive Bayes Classifier
library(e1071)
model <- x %>% naiveBayes(Species ~ ., data = .)
decisionplot(model, x, cl = "Species") + labs(title = "Naive Bayes")
#' ## Linear Discriminant Analysis
library(MASS)
model <- x %>% lda(Species ~ ., data = .)
decisionplot(model, x, cl = "Species") + labs(title = "LDA")
#' ## Multinomial Logistic Regression (implemented in nnet)
#'
#' Multinomial logistic regression is an extension of logistic regression to problems with more than two classes.
#'
library(nnet)
model <- x %>% multinom(Species ~., data = .)
decisionplot(model, x, cl = "Species") + labs(titel = "Multinomial Logistic Regression")
#' ## Decision Trees
library("rpart")
model <- x %>% rpart(Species ~ ., data = .)
decisionplot(model, x, cl = "Species") + labs(title = "CART")
model <- x %>% rpart(Species ~ ., data = .,
control = rpart.control(cp = 0.001, minsplit = 1))
decisionplot(model, x, cl = "Species") + labs(title = "CART (overfitting)")
library(C50)
model <- x %>% C5.0(Species ~ ., data = .)
decisionplot(model, x, cl = "Species") + labs(title = "C5.0")
library(randomForest)
model <- x %>% randomForest(Species ~ ., data = .)
decisionplot(model, x, cl = "Species") + labs(title = "Random Forest")
#' ## SVM
library(e1071)
model <- x %>% svm(Species ~ ., data = ., kernel = "linear")
decisionplot(model, x, cl = "Species") + labs(title = "SVM (linear kernel)")
model <- x %>% svm(Species ~ ., data = ., kernel = "radial")
decisionplot(model, x, cl = "Species") + labs(title = "SVM (radial kernel)")
model <- x %>% svm(Species ~ ., data = ., kernel = "polynomial")
decisionplot(model, x, cl = "Species") + labs(title = "SVM (polynomial kernel)")
model <- x %>% svm(Species ~ ., data = ., kernel = "sigmoid")
decisionplot(model, x, cl = "Species") + labs(title = "SVM (sigmoid kernel)")
#' ## Single Layer Feed-forward Neural Networks
library(nnet)
model <-x %>% nnet(Species ~ ., data = ., size = 1, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "Species") + labs(title = "NN (1 neuron)")
model <-x %>% nnet(Species ~ ., data = ., size = 2, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "Species") + labs(title = "NN (2 neurons)")
model <-x %>% nnet(Species ~ ., data = ., size = 4, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "Species") + labs(title = "NN (4 neurons)")
model <-x %>% nnet(Species ~ ., data = ., size = 10, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "Species") + labs(title = "NN (10 neurons)")
#' ## Deep Learning with keras
library(keras)
#' define predict so it works with decision plot
predict.keras.engine.training.Model <- function(object, newdata, ...)
predict_classes(object, as.matrix(newdata))
#' Choices are the activation function, number of layers, number of units per layer and the optimizer.
#' A L2 regularizer is used for the dense layer weights to reduce overfitting. The output is a
#' categorical class value, therefore the output layer uses the softmax activation function,
#' the loss is categorical crossentropy, and the metric is accuracy.
model <- keras_model_sequential() %>%
layer_dense(units = 10, activation = 'relu', input_shape = c(2),
kernel_regularizer=regularizer_l2(l=0.01)) %>%
layer_dense(units = 4, activation = 'softmax') %>%
compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = 'accuracy')
history <- model %>% fit(
as.matrix(x[,1:2]),
x %>% pull(3) %>% as.integer %>% to_categorical(),
epochs = 100,
batch_size = 10
)
history
decisionplot(model, x, cl = "Species") + labs(title = "keras (relu activation)")
model <- keras_model_sequential() %>%
layer_dense(units = 10, activation = 'tanh', input_shape = c(2),
kernel_regularizer = regularizer_l2(l = 0.01)) %>%
layer_dense(units = 4, activation = 'softmax') %>%
compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = 'accuracy')
history <- model %>% fit(
as.matrix(x[,1:2]),
x %>% pull(3) %>% as.integer %>% to_categorical(),
epochs = 100,
batch_size = 10
)
history
decisionplot(model, x, cl = "Species") + labs(title = "keras (tanh activation)")
#' # Circle Dataset
#'
#' This set is not linearly separable!
set.seed(1000)
library(mlbench)
x <- mlbench.circle(500)
#x <- mlbench.cassini(500)
#x <- mlbench.spirals(500, sd = .1)
#x <- mlbench.smiley(500)
x <- cbind(as.data.frame(x$x), factor(x$classes))
colnames(x) <- c("x", "y", "class")
x <- as_tibble(x)
x
ggplot(x, aes(x = x, y = y, color = class)) + geom_point()
#' ## K-Nearest Neighbors Classifier
library(caret)
model <- x %>% knn3(class ~ ., data = ., k = 1)
decisionplot(model, x, cl = "class") + labs(title = "kNN (1 neighbor)")
model <- x %>% knn3(class ~ ., data = ., k = 10)
decisionplot(model, x, cl = "class") + labs(title = "kNN (10 neighbor)")
#' ## Naive Bayes Classifier
library(e1071)
model <- x %>% naiveBayes(class ~ ., data = .)
decisionplot(model, x, cl = "class") + labs(title = "naive Bayes")
#' ## Linear Discriminant Analysis
library(MASS)
model <- x %>% lda(class ~ ., data = .)
decisionplot(model, x, cl = "class") + labs(title = "LDA")
#' ## Multinomial Logistic Regression (implemented in nnet)
#'
#' Multinomial logistic regression is an extension of logistic regression to problems with more than two classes.
#'
library(nnet)
model <- x %>% multinom(class ~., data = .)
decisionplot(model, x, cl = "class") + labs(titel = "Multinomial Logistic Regression")
#' ## Decision Trees
library("rpart")
model <- x %>% rpart(class ~ ., data = .)
decisionplot(model, x, cl = "class") + labs(title = "CART")
model <- x %>% rpart(class ~ ., data = .,
control = rpart.control(cp = 0.001, minsplit = 1))
decisionplot(model, x, cl = "class") + labs(title = "CART (overfitting)")
library(C50)
model <- x %>% C5.0(class ~ ., data = .)
decisionplot(model, x, cl = "class") + labs(title = "C5.0")
library(randomForest)
model <- x %>% randomForest(class ~ ., data = .)
decisionplot(model, x, cl = "class") + labs(title = "Random Forest")
#' ## SVM
library(e1071)
model <- x %>% svm(class ~ ., data = ., kernel = "linear")
decisionplot(model, x, cl = "class") + labs(title = "SVM (linear kernel)")
model <- x %>% svm(class ~ ., data = ., kernel = "radial")
decisionplot(model, x, cl = "class") + labs(title = "SVM (radial kernel)")
model <- x %>% svm(class ~ ., data = ., kernel = "polynomial")
decisionplot(model, x, cl = "class") + labs(title = "SVM (polynomial kernel)")
model <- x %>% svm(class ~ ., data = ., kernel = "sigmoid")
decisionplot(model, x, cl = "class") + labs(title = "SVM (sigmoid kernel)")
#' ## Single Layer Feed-forward Neural Networks
library(nnet)
model <-x %>% nnet(class ~ ., data = ., size = 1, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "class") + labs(title = "NN (1 neuron)")
model <-x %>% nnet(class ~ ., data = ., size = 2, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "class") + labs(title = "NN (2 neurons)")
model <-x %>% nnet(class ~ ., data = ., size = 4, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "class") + labs(title = "NN (4 neurons)")
model <-x %>% nnet(class ~ ., data = ., size = 10, maxit = 1000, trace = FALSE)
decisionplot(model, x, cl = "class") + labs(title = "NN (10 neurons)")
#' ## Deep Learning with keras
library(keras)
#' redefine predict so it works with decision plot
predict.keras.engine.training.Model <- function(object, newdata, ...)
predict_classes(object, as.matrix(newdata))
#' Choices are the activation function, number of layers, number of units per layer and the optimizer.
#' A L2 regularizer is used for the dense layer weights to reduce overfitting. The output is a
#' categorical class value, therefore the output layer uses the softmax activation function,
#' the loss is categorical crossentropy, and the metric is accuracy.
model <- keras_model_sequential() %>%
layer_dense(units = 10, activation = 'relu', input_shape = c(2),
kernel_regularizer=regularizer_l2(l = 0.0001)) %>%
layer_dense(units = 3, activation = 'softmax') %>%
compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = 'accuracy')
history <- model %>% fit(
as.matrix(x[,1:2]),
x %>% pull(3) %>% as.integer %>% to_categorical(),
epochs = 100,
batch_size = 10
)
history
decisionplot(model, x, cl = "class") + labs(title = "keras (relu activation)")
model <- keras_model_sequential() %>%
layer_dense(units = 10, activation = 'tanh', input_shape = c(2),
kernel_regularizer = regularizer_l2(l = 0.0001)) %>%
layer_dense(units = 3, activation = 'softmax') %>%
compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = 'accuracy')
history <- model %>% fit(
as.matrix(x[,1:2]),
x %>% pull(3) %>% as.integer %>% to_categorical(),
epochs = 100,
batch_size = 10
)
history
decisionplot(model, x, cl = "class") + labs(title = "keras (tanh activation)")