From 480600b3afaf2a0a6f32cf417edf9567f625b2c3 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Mon, 16 Dec 2024 10:45:06 -0600 Subject: [PATCH] [python-package] simplify eval result printing (#6749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Morales --- python-package/lightgbm/callback.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index ae1e72c549d4..c64fb8ba755b 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -73,15 +73,13 @@ class CallbackEnv: def _format_eval_result(value: _EvalResultTuple, show_stdv: bool) -> str: """Format metric string.""" - if len(value) == 4: - return f"{value[0]}'s {value[1]}: {value[2]:g}" - elif len(value) == 5: - if show_stdv: - return f"{value[0]}'s {value[1]}: {value[2]:g} + {value[4]:g}" # type: ignore[misc] - else: - return f"{value[0]}'s {value[1]}: {value[2]:g}" - else: - raise ValueError("Wrong metric value") + dataset_name, metric_name, metric_value, *_ = value + out = f"{dataset_name}'s {metric_name}: {metric_value:g}" + # tuples from cv() sometimes have a 5th item, with standard deviation of + # the evaluation metric (taken over all cross-validation folds) + if show_stdv and len(value) == 5: + out += f" + {value[4]:g}" + return out class _LogEvaluationCallback: