Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 10 - Model Selection And Boosting/Grid Search/grid_search.R
1336 views
1
# Grid Search
2
3
# Importing the dataset
4
dataset = read.csv('Social_Network_Ads.csv')
5
dataset = dataset[3:5]
6
7
# Encoding the target feature as factor
8
dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))
9
10
# Splitting the dataset into the Training set and Test set
11
# install.packages('caTools')
12
library(caTools)
13
set.seed(123)
14
split = sample.split(dataset$Purchased, SplitRatio = 0.75)
15
training_set = subset(dataset, split == TRUE)
16
test_set = subset(dataset, split == FALSE)
17
18
# Feature Scaling
19
training_set[-3] = scale(training_set[-3])
20
test_set[-3] = scale(test_set[-3])
21
22
# Fitting Kernel SVM to the Training set
23
# install.packages('e1071')
24
library(e1071)
25
classifier = svm(formula = Purchased ~ .,
26
data = training_set,
27
type = 'C-classification',
28
kernel = 'radial')
29
30
# Predicting the Test set results
31
y_pred = predict(classifier, newdata = test_set[-3])
32
33
# Making the Confusion Matrix
34
cm = table(test_set[, 3], y_pred)
35
36
# Applying k-Fold Cross Validation
37
# install.packages('caret')
38
library(caret)
39
folds = createFolds(training_set$Purchased, k = 10)
40
cv = lapply(folds, function(x) {
41
training_fold = training_set[-x, ]
42
test_fold = training_set[x, ]
43
classifier = svm(formula = Purchased ~ .,
44
data = training_fold,
45
type = 'C-classification',
46
kernel = 'radial')
47
y_pred = predict(classifier, newdata = test_fold[-3])
48
cm = table(test_fold[, 3], y_pred)
49
accuracy = (cm[1,1] + cm[2,2]) / (cm[1,1] + cm[2,2] + cm[1,2] + cm[2,1])
50
return(accuracy)
51
})
52
accuracy = mean(as.numeric(cv))
53
54
# Applying Grid Search to find the best parameters
55
# install.packages('caret')
56
library(caret)
57
classifier = train(form = Purchased ~ ., data = training_set, method = 'svmRadial')
58
classifier
59
classifier$bestTune
60
61
# Visualising the Training set results
62
library(ElemStatLearn)
63
set = training_set
64
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
65
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
66
grid_set = expand.grid(X1, X2)
67
colnames(grid_set) = c('Age', 'EstimatedSalary')
68
y_grid = predict(classifier, newdata = grid_set)
69
plot(set[, -3],
70
main = 'Kernel SVM (Training set)',
71
xlab = 'Age', ylab = 'Estimated Salary',
72
xlim = range(X1), ylim = range(X2))
73
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
74
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
75
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
76
77
# Visualising the Test set results
78
library(ElemStatLearn)
79
set = test_set
80
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
81
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
82
grid_set = expand.grid(X1, X2)
83
colnames(grid_set) = c('Age', 'EstimatedSalary')
84
y_grid = predict(classifier, newdata = grid_set)
85
plot(set[, -3], main = 'Kernel SVM (Test set)',
86
xlab = 'Age', ylab = 'Estimated Salary',
87
xlim = range(X1), ylim = range(X2))
88
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
89
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
90
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
91