Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
4546a8fd
Unverified
Commit
4546a8fd
authored
Nov 02, 2023
by
david-cortes
Committed by
GitHub
Nov 02, 2023
Browse files
[R-package] Use `cat()` instead of `print()` for metrics and callbacks (#6171)
parent
3405ee82
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
7 deletions
+29
-7
.ci/lint_r_code.R
.ci/lint_r_code.R
+1
-2
R-package/R/callback.R
R-package/R/callback.R
+5
-5
R-package/tests/testthat/test_basic.R
R-package/tests/testthat/test_basic.R
+23
-0
No files found.
.ci/lint_r_code.R
View file @
4546a8fd
...
@@ -78,8 +78,7 @@ LINTERS_TO_USE <- list(
...
@@ -78,8 +78,7 @@ LINTERS_TO_USE <- list(
,
"true_false"
=
lintr
::
T_and_F_symbol_linter
()
,
"true_false"
=
lintr
::
T_and_F_symbol_linter
()
,
"undesirable_function"
=
lintr
::
undesirable_function_linter
(
,
"undesirable_function"
=
lintr
::
undesirable_function_linter
(
fun
=
c
(
fun
=
c
(
"cat"
=
"CRAN forbids the use of cat() in packages except in special cases. Use message() or warning()."
"cbind"
=
paste0
(
,
"cbind"
=
paste0
(
"cbind is an unsafe way to build up a data frame. merge() or direct "
"cbind is an unsafe way to build up a data frame. merge() or direct "
,
"column assignment is preferred."
,
"column assignment is preferred."
)
)
...
...
R-package/R/callback.R
View file @
4546a8fd
...
@@ -90,7 +90,7 @@ cb_print_evaluation <- function(period) {
...
@@ -90,7 +90,7 @@ cb_print_evaluation <- function(period) {
# Check if message is existing
# Check if message is existing
if
(
nchar
(
msg
)
>
0L
)
{
if
(
nchar
(
msg
)
>
0L
)
{
prin
t
(
.merge_eval_string
(
env
=
env
))
ca
t
(
.merge_eval_string
(
env
=
env
)
,
"\n"
)
}
}
}
}
...
@@ -208,9 +208,9 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
...
@@ -208,9 +208,9 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
msg
<-
paste0
(
msg
<-
paste0
(
"Will train until there is no improvement in "
"Will train until there is no improvement in "
,
stopping_rounds
,
stopping_rounds
,
" rounds."
,
" rounds.
\n
"
)
)
prin
t
(
msg
)
ca
t
(
msg
)
}
}
# Internally treat everything as a maximization task
# Internally treat everything as a maximization task
...
@@ -284,7 +284,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
...
@@ -284,7 +284,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
}
}
if
(
isTRUE
(
verbose
))
{
if
(
isTRUE
(
verbose
))
{
prin
t
(
paste0
(
"Early stopping, best iteration is: "
,
best_msg
[[
i
]]))
ca
t
(
paste0
(
"Early stopping, best iteration is: "
,
best_msg
[[
i
]]
,
"\n"
))
}
}
# Store best iteration and stop
# Store best iteration and stop
...
@@ -302,7 +302,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
...
@@ -302,7 +302,7 @@ cb_early_stop <- function(stopping_rounds, first_metric_only, verbose) {
}
}
if
(
isTRUE
(
verbose
))
{
if
(
isTRUE
(
verbose
))
{
prin
t
(
paste0
(
"Did not meet early stopping, best iteration is: "
,
best_msg
[[
i
]]))
ca
t
(
paste0
(
"Did not meet early stopping, best iteration is: "
,
best_msg
[[
i
]]
,
"\n"
))
}
}
# Store best iteration and stop
# Store best iteration and stop
...
...
R-package/tests/testthat/test_basic.R
View file @
4546a8fd
...
@@ -3805,3 +3805,26 @@ test_that("lightgbm() correctly sets objective when passing lgb.Dataset as input
...
@@ -3805,3 +3805,26 @@ test_that("lightgbm() correctly sets objective when passing lgb.Dataset as input
)
)
expect_equal
(
model
$
params
$
objective
,
"regression"
)
expect_equal
(
model
$
params
$
objective
,
"regression"
)
})
})
test_that
(
"Evaluation metrics aren't printed as a single-element vector"
,
{
log_txt
<-
capture_output
({
data
(
mtcars
)
y
<-
mtcars
$
mpg
x
<-
as.matrix
(
mtcars
[,
-1L
])
cv_result
<-
lgb.cv
(
data
=
lgb.Dataset
(
x
,
label
=
y
)
,
params
=
list
(
objective
=
"regression"
,
metric
=
"l2"
,
min_data_in_leaf
=
5L
,
max_depth
=
3L
,
num_threads
=
.LGB_MAX_THREADS
)
,
nrounds
=
2L
,
nfold
=
3L
,
verbose
=
1L
,
eval_train_metric
=
TRUE
)
})
expect_false
(
grepl
(
"[1] \"[1]"
,
log_txt
,
fixed
=
TRUE
))
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment