forked from bkrai/Top-10-Machine-Learning-Methods-With-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinningKaggleCompetition
48 lines (43 loc) · 1.24 KB
/
WinningKaggleCompetition
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
#Libraries
library(xgboost)
library(caret)
library(tictoc)
#Data
data <- read.csv(file.choose(), header = T)
data$Buy <- as.factor(data$Buy)
data <- data[,-1]
# Data Partition
set.seed(123)
ind <- sample(2, nrow(data), replace = T, prob = c(0.80, 0.20))
train <- data[ind==1,]
test <- data[ind==2,]
# Random search
modelLookup("xgbTree")
set.seed(123)
cv <- trainControl(method="repeatedcv",
number = 3,
repeats = 2,
allowParallel = T,
verboseIter = T,
returnData = F,
search = 'random')
set.seed(123)
boo <- train(Buy ~ . ,
data = train,
trControl = cv,
method = "xgbTree",
tuneLength = 3)
#Confusion matrix
p <- predict(boo, newdata = train, 'raw')
(tab <- table(Predicted = p, Actual = train$Buy))
p1 <- predict(boo, newdata = test, 'raw')
(tab1 <- table(Predicted = p1, Actual = test$Buy))
# Submission
new <- read.csv(file.choose(), header = T)
p2 <- predict(boo, newdata = new, 'prob')
p2 <- p2[,-1]
submission <- new$Id
submission <- data.frame(submission)
submission$Predicted <- p2
colnames(submission) <- c('Id', 'Predicted')
write.csv(submission, 'submission.csv', row.names=F)