-
Notifications
You must be signed in to change notification settings - Fork 40
/
xgboost.R
61 lines (46 loc) · 1.86 KB
/
xgboost.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
# XGBoost
# Setting the Working Directory
setwd('./Documents/Machine Learning A-Z/Part 10 - Model Selection/Section 53 - Combining Several Models')
# Importing the Dataset
dataset = read.csv('Churn_Modelling.csv')
dataset = dataset[4:14]
# Encoding the categorical Variables as Factors
dataset$Geography = as.numeric(factor(dataset$Geography,
levels = c('France', 'Spain', 'Germany'),
labels = c(1, 2, 3)))
dataset$Gender = as.numeric(factor(dataset$Gender,
levels = c('Female', 'Male'),
labels = c(1, 2)))
# Splitting the Dataset into the Training Set and Test Set
library(caTools)
set.seed(123)
split = sample.split(dataset$Exited, SplitRatio = 0.8)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)
# Fitting XGBoost to the Training Set
library(xgboost)
classifier = xgboost(data = as.matrix(training_set[-11]), label = training_set$Exited, nrounds = 10)
classifier
# Predicting the Test Set Results
y_pred = predict(classifier, newdata = as.matrix(test_set[-11]))
y_pred = (y_pred >= 0.5)
y_pred
# Making the Confusion Matrix
cm = table(test_set[, 11], y_pred)
cm
# Applying k-Fold Cross Validation
library(caret)
folds = createFolds(training_set$Exited, k = 10)
folds
cv = lapply(folds, function(x) {
training_fold = training_set[-x, ]
test_fold = training_set[x, ]
classifier = xgboost(data = as.matrix(training_set[-11]), label = training_set$Exited, nrounds = 10)
y_pred = predict(classifier, newdata = as.matrix(test_fold[-11]))
y_pred = (y_pred >= 0.5)
cm = table(test_fold[, 11], y_pred)
accuracy = (cm[1,1] + cm[2,2]) / (cm[1,1] + cm[2,2] + cm[1,2] + cm[2,1])
return(accuracy)
})
cv
accuracy = mean(as.numeric(cv))