generated from slds-lmu/lecture_template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ziyu-Mu <mu.ziyu.ovo@gmail.com>
- Loading branch information
Showing
4 changed files
with
64 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# ------------------------------------------------------------------------------ | ||
# intro | ||
# TABLE: | ||
# train and test MSE table using Neural Network and CART (overfitting). | ||
# DATA: mtcars | ||
# ------------------------------------------------------------------------------ | ||
|
||
library(nnet) | ||
library(xtable) | ||
library(mlr3) | ||
library(mlr3learners) | ||
set.seed(123) | ||
|
||
# DATA ------------------------------------------------------------------------- | ||
|
||
lgr::get_logger("mlr3")$set_threshold("info") | ||
|
||
task = tsk("mtcars") | ||
|
||
lrn1 = lrn("regr.nnet", size = 100, maxit = 20000, MaxNWts = 10000, decay = 0, abstol = 1e-7) | ||
lrn1$encapsulate = c(train = "evaluate", predict = "evaluate") | ||
lrn2 = lrn("regr.rpart", minsplit = 2, cp = 0) | ||
|
||
my_learners = list(lrn1, lrn2) | ||
for (x in my_learners){ | ||
x$predict_sets = c("train", "test") | ||
} | ||
|
||
bg = benchmark_grid(task, my_learners, rsmp("cv", folds = 10)) | ||
|
||
bmr = benchmark(bg) | ||
|
||
m1 = msr("regr.mse", predict_sets = c("test"), id = "mse-test") | ||
m2 = msr("regr.mse", predict_sets = c("train"), id = "mse-train") | ||
|
||
a = bmr$aggregate(measures = list(m1, m2)) | ||
print(a) | ||
|
||
# TABLE ------------------------------------------------------------------------ | ||
|
||
# Create a 2x2 comparison table with rounded results | ||
res = as.data.frame(a) | ||
res = res[, c("mse-train", "mse-test")] | ||
|
||
rownames(res) = c("Neural Network", "CART") | ||
colnames(res) = c("Train MSE", "Test MSE") | ||
|
||
latex_tab = xtable(res) | ||
|
||
print(latex_tab, file = "table_overfitting.tex", include.rownames = TRUE, include.colnames = TRUE, comment = FALSE) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
\begin{table}[ht] | ||
\centering | ||
\begin{tabular}{rrr} | ||
\hline | ||
& Train MSE & Test MSE \\ | ||
\hline | ||
Neural Network & 1.47 & 345.84 \\ | ||
CART & 0.00 & 6.91 \\ | ||
\hline | ||
\end{tabular} | ||
\end{table} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters