-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlr3.R
215 lines (159 loc) · 4.41 KB
/
mlr3.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# load_libraries ----------------------------------------------------------
library("data.table")
library("mlr3verse")
library("magrittr")
library("ggplot2")
library("ggthemes")
library("neuralnet")
library("dplyr")
library("ggfortify")
library("plotluck")
library("inspectdf")
library("infer")
library("stringi")
library("ggplot2") # for awesome graphics
library("visdat") # for additional visualizations
# set_defaults ------------------------------------------------------------
setDTthreads(0L)
theme_set(theme_fivethirtyeight())
# Get Dataa -----------------------------------------------------------------
data("ames_raw", package = "AmesHousing" )
# __create Task -------------------------------------------------------------
ames_raw <- data.table(
ames_raw,
check.names = TRUE
)
ams_tsk <- TaskRegr$new(
id = "ames",
backend = ames_raw,
target = "SalePrice"
)
autoplot(ams_tsk)
ams_tsk$ncol
ams_tsk$nrow
# ams_tsk$data(
# rows = 1:10,
# cols = c("Electrical", "Heating")
# )
ams_tsk$target_names
ams_tsk$feature_names
ams_tsk$feature_types
ams_tsk %>%
as.data.table() %>%
summary()
ams_tsk$col_info
ams_tsk$col_roles
# ams_tsk$set_col_roles(
# cols = "Yr.Sold",
# roles = "order")
ams_tsk$row_ids
ams_tsk$row_names
ams_tsk$row_roles
ams_tsk$set_row_roles(
rows = ames_raw[,seq(to = .N-10,from = .N)],
roles = "validation"
)
# ams_tsk$select(c("Year.Built", "Year.Remod.Add"))
ams_tsk$select(
ams_tsk$feature_names[ams_tsk$feature_types$type == "integer"]
)
ams_tsk$col_roles
# ams_tsk$filter(1:5)
ams_tsk$head(10)
# ams_tsk$rbind(ames_raw)
# ams_tsk$cbind(ames_raw)
# ams_tsk$row_roles$use <- 1:2920
# ams_tsk$col_roles$feature <- names(ames_raw)
ams_tsk %>%
class()
ams_tsk$missings()
# ams_tsk$select(c("Year.Built", "Year.Remod.Add"))
# autoplot(ams_tsk, type = "pairs")
# ams_tsk$col_roles$feature <- names(ames_raw)
# __create learners ---------------------------------------------------------
# LearnerRegrLM$new()
# mlr_learners$get()
# ams_lrn <- lrn("regr.glmnet")
ams_lrn <- lrn("regr.lm")
ams_lrn$param_set
# ams_lrn$param_set$values = list(family = "gaussian")
# ams_lrn$param_set$values = mlr3misc::insert_named(
# ams_lrn$param_set$values,
# list(
# type.multinomial = "grouped",
# type.measure = "auc"
# )
# )
ams_lrn$param_set$values
ams_lrn$data_formats
ams_lrn$encapsulate
ams_lrn$errors
ams_lrn$feature_types
ams_lrn$fallback
# train and Predict -------------------------------------------------------
train <- sample(ams_tsk$nrow, ams_tsk$nrow * 0.70)
test <- setdiff(seq_len(ams_tsk$nrow), train)
ams_lrn$model
complete <- ams_tsk$data() %>%
complete.cases() %>%
which()
ams_tsk$filter(complete)
ams_tsk$missings()
ams_lrn$train(
task = ams_tsk,
row_ids = train
)
ams_lrn$model
test <- (test %in% complete) %>%
which() %>%
test[.]
ams_tsk$data(rows = test)
ams_prdt <- ams_lrn$predict(
task = ams_tsk,
row_ids = test)
ams_prdt$man
ams_prdt$missing
ams_prdt$truth
ams_prdt$task_type
ams_prdt$task_properties
ams_prdt$se
ams_prdt$row_ids
ams_prdt$response
ams_prdt$predict_types
autoplot(ams_prdt)
# autoplot(ams_prdt, type = "roc")
# ams_lrn$predict_type = "prob"
# __create Resampling -------------------------------------------------------
amsRsmp <- rsmp("holdout", ratio = 0.7)
amsRsmp <- rsmp("cv", folds = 10)
amsRsmp$instantiate(amsTsk)
# Data Churn -----------------------------------------------------------------
# __create Task -------------------------------------------------------------
chrnTsk <- TaskClassif$new(
id = "churn",
backend =
attrition %>%
mutate_if(is.ordered,
.funs = factor,
ordered = FALSE),
target = "Attrition"
)
chrnTsk$select(c("Age", "DailyRate", "DistanceFromHome"))
chrnTsk$col_roles$stratum = "Attrition"
autoplot(chrnTsk,
type = "pairs")
# __create Resampling -------------------------------------------------------------
chrnRsmp <- rsmp("holdout", ratio = 0.7)
# __create Learner -------------------------------------------------------------
chrnLrn <- lrn("classif.log_reg")
chrnRslt <- resample(task = chrnTsk,
learner = chrnLrn,
resampling = chrnRsmp,
store_models = TRUE)
chrnRslt$aggregate(msr("classif.ce"))
chrnRslt$score(msr("classif.ce"))
chrnPrd <- chrnRslt$prediction()
chrnPrd$confusion %>%
prop.table()
autoplot(chrnRslt,type = "roc")
# pipelines ---------------------------------