-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGBM-CLASSIFICATION.R
More file actions
75 lines (59 loc) · 1.64 KB
/
GBM-CLASSIFICATION.R
File metadata and controls
75 lines (59 loc) · 1.64 KB
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
library(ISLR)
library(tidyverse)
library(funModeling)
library(caret)
library(pROC)
library(class)#knn icin
library(e1071)#knn icin
library(kernlab) #svm icin
library(ROCR) #roc icin
library(neuralnet)
library(GGally)
library(nnet)
library(rpart)
library(cli)
library(tree)
library(rpart.plot)
library(randomForest)
library(gbm)
library(xgboost)
library(DiagrammeR)
library(mlbench)
library(ISLR)
df <- Carseats
str(df)
summary(df)
hist(df$Sales)
df$Sales <- factor(ifelse(df$Sales>7.5, "H", "L"))
summary(df$Sales)
set.seed(123)
train_indeks <- createDataPartition(df$Sales,
p = .8,
list = FALSE,
times = 1)
train <- df[train_indeks,]
test <- df[-train_indeks,]
train_x <- train %>% dplyr::select(-Sales)
train_y <- train$Sales
test_x <- test %>% dplyr::select(-Sales)
test_y <- test$Sales
#tek bir veri seti
training <- data.frame(train_x, Sales = train_y)
train$Sales <- as.factor(ifelse(as.numeric(train$Sales)==2,1,0))
train_y <- as.factor(ifelse(as.numeric(train_y)==2,1,0))
train$Sales <- as.numeric(train$Sales)
train <- transform(train, Sales = Sales - 1)
gbm_fit <- gbm(Sales~., data = train,
shrinkage = 0.01,
distribution = "bernoulli",
cv.folds = 5,
n.trees = 3000,
verbose = F)
gbm_fit
summary(gbm_fit)
gbm.perf(gbm_fit, method = "cv")
plot.gbm(gbm_fit, 3, gbm.perf(gbm_fit, method = "cv"))
y_pred <- predict.gbm(gbm_fit, test_x, type = "response")
y_pred <- ifelse(y_pred<0.5, "H","L")
y_pred <- as.factor(y_pred)
confusionMatrix(test_y, y_pred, positive = "H")