(
self,
D_train: xgb.DMatrix,
D_valid: xgb.DMatrix,
rounds: int,
verbose_eval: Union[bool, int],
)
| 40 | |
| 41 | class TestCallbacks: |
| 42 | def run_evaluation_monitor( |
| 43 | self, |
| 44 | D_train: xgb.DMatrix, |
| 45 | D_valid: xgb.DMatrix, |
| 46 | rounds: int, |
| 47 | verbose_eval: Union[bool, int], |
| 48 | ): |
| 49 | def check_output(output: str) -> None: |
| 50 | if int(verbose_eval) == 1: |
| 51 | # Should print each iteration info |
| 52 | assert len(output.split("\n")) == rounds |
| 53 | elif int(verbose_eval) > rounds: |
| 54 | # Should print first and latest iteration info |
| 55 | assert len(output.split("\n")) == 2 |
| 56 | else: |
| 57 | # Should print info by each period additionaly to first and latest |
| 58 | # iteration |
| 59 | num_periods = rounds // int(verbose_eval) |
| 60 | # Extra information is required for latest iteration |
| 61 | is_extra_info_required = num_periods * int(verbose_eval) < (rounds - 1) |
| 62 | assert len(output.split("\n")) == ( |
| 63 | 1 + num_periods + int(is_extra_info_required) |
| 64 | ) |
| 65 | |
| 66 | evals_result: xgb.callback.TrainingCallback.EvalsLog = {} |
| 67 | params = {"objective": "binary:logistic", "eval_metric": "error"} |
| 68 | with tm.captured_output() as (out, err): |
| 69 | xgb.train( |
| 70 | params, |
| 71 | D_train, |
| 72 | evals=[(D_train, "Train"), (D_valid, "Valid")], |
| 73 | num_boost_round=rounds, |
| 74 | evals_result=evals_result, |
| 75 | verbose_eval=verbose_eval, |
| 76 | ) |
| 77 | output: str = out.getvalue().strip() |
| 78 | check_output(output) |
| 79 | |
| 80 | with tm.captured_output() as (out, err): |
| 81 | xgb.cv(params, D_train, num_boost_round=rounds, verbose_eval=verbose_eval) |
| 82 | output = out.getvalue().strip() |
| 83 | check_output(output) |
| 84 | |
| 85 | def test_evaluation_monitor(self, breast_cancer: BreastCancer) -> None: |
| 86 | D_train = xgb.DMatrix(breast_cancer.tr[0], breast_cancer.tr[1]) |
no test coverage detected