-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXGBOOST.Rmd
186 lines (112 loc) · 3.43 KB
/
XGBOOST.Rmd
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "R Notebook"
output: html_notebook
author: M. Vahit Keskin
---
### M. Vahit Keskin'in Udemy Derslerinden alıntılanmıştır.
# XGBoost
## Giris
Ozellikleri
Hiz:
XGBoost OpenMD sayesinde otomatik olarak paralel hesaplama yapar. Boylece klasik GBM'den 10 kat daha hizli calisir.
Girdi Tipleri:
Yogunluk matrisi - R'in yogunluk matrisi: matrix
Seyrek matrisi - R'in seyreklik matrisi - Matrix::dgCMatrix
Kendi veri sinifi: xgb.DMatrix
Seyreklik:
Regresyon ya da siniflandirma problemleri icin seyrek girdileri kabul eder buna gore optimize edilmistir.
Ozellestirme:
Objective fonksiyonlari ve evaluation fonksiyonlari ozellestirilebilir.
Yani makine ogrenmesi problem turune gore olceklenebilir ve basari degerlendirme kriterleri de duzenlenebilir.
Kurulum
```{r}
#en guncel versiyon icin
#install.packages("drat", repos="https://cran.rstudio.com")
#drat:::addRepo("dmlc")
#install.packages("xgboost", repos="http://dmlc.ml/drat/", type = "source")
#cran versiyonu icin
install.packages("xgboost")
library(xgboost)
```
## Model
Model
```{r}
xgboost_fit <-xgboost(data = as.matrix(train_x),
label = train_y,
booster = "gblinear",
max.depth = 2,
eta = 1,
nthread = 2,
nrounds = 1000)
dtrain <- xgb.DMatrix(data = as.matrix(train_x), label = train_y)
dtest <- xgb.DMatrix(data = as.matrix(test_x), label = test_y)
dtrain
xgboost_fit <-xgboost(data = dtrain,
booster = "gblinear",
max.depth = 2,
eta = 1,
nthread = 2,
nrounds = 3)
xgboost_fit
class(dtrain)
imp_matris <- xgb.importance(model = xgboost_fit)
imp_matris
xgb.plot.importance(imp_matris)
```
Model Takip: watchlist
```{r}
watchlist <- list(train = dtrain, test = dtest)
xgb_fit <- xgb.train(data = dtrain,
booster = "gblinear",
max.depth = 4,
eta = 0.1,
nthread = 2,
nrounds = 100,
watchlist = watchlist)
imp_matris <- xgb.importance(model = xgb_fit)
xgb.plot.importance(imp_matris)
xgb_fit$evaluation_log
```
## Tahmin
```{r}
predict(xgb_fit, as.matrix(test_x))
plot(predict(xgb_fit, as.matrix(test_x)), test_y,
xlab = "Tahmin Edilen", ylab = "Gercek",
main = "Tahmin Edilen vs Gercek: XGBoost",
col = "dodgerblue", pch = 20)
grid()
abline(0, 1, col = "darkorange", lwd = 2)
library(caret)
defaultSummary(data.frame(obs = test_y,
pred = predict(xgb_fit, as.matrix(test_x))))
```
## Model Tuning
```{r}
ctrl <- trainControl(method = "cv", number = 10)
xgb_grid <- expand.grid(
nrounds = 1000,
lambda = c(1,2,3),
alpha = c(0, 0.5, 1),
eta = c(0, 0.5, 1)
)
xgb_tune_fit <- train(
x = data.matrix(train_x),
y = train_y,
trControl = ctrl,
tuneGrid = xgb_grid,
method = "xgbLinear"
)
defaultSummary(data.frame(obs = test_y,
pred = predict(xgb_tune_fit, as.matrix(test_x))))
```
## Model Kaydetme
```{r}
save(xgb_tune_fit, file = "son_model.rda")
#deneme yapalım
rm(xgb_tune_fit) #kurduğumuz modeli kaldırıp dışarıdan alalım
load("son_model.rda")
#modeli yüklediğimizde daha önce kaydettiğimiz isimle kullanabilir
#zaten yükleme yapıldığında environment kısmına ismiyle gelecektir ve o isimden kullanabiliriz.
defaultSummary(data.frame(obs = test_y,
pred = predict(xgb_tune_fit, as.matrix(test_x))))
```