Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 10 - Model Selection And Boosting/k-Fold Cross Validation/k_fold_cross_validation.R
1341 views
1
# k-Fold Cross Validation
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
# Visualising the Training set results
55
library(ElemStatLearn)
56
set = training_set
57
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
58
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
59
grid_set = expand.grid(X1, X2)
60
colnames(grid_set) = c('Age', 'EstimatedSalary')
61
y_grid = predict(classifier, newdata = grid_set)
62
plot(set[, -3],
63
main = 'Kernel SVM (Training set)',
64
xlab = 'Age', ylab = 'Estimated Salary',
65
xlim = range(X1), ylim = range(X2))
66
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
67
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
68
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
69
70
# Visualising the Test set results
71
library(ElemStatLearn)
72
set = test_set
73
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
74
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
75
grid_set = expand.grid(X1, X2)
76
colnames(grid_set) = c('Age', 'EstimatedSalary')
77
y_grid = predict(classifier, newdata = grid_set)
78
plot(set[, -3], main = 'Kernel SVM (Test set)',
79
xlab = 'Age', ylab = 'Estimated Salary',
80
xlim = range(X1), ylim = range(X2))
81
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
82
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
83
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
84