| 239 | assert len(cv) == (4) |
| 240 | |
| 241 | def test_cv_explicit_fold_indices_labels(self): |
| 242 | params = {"max_depth": 2, "eta": 1, "objective": "reg:squarederror"} |
| 243 | N = 100 |
| 244 | F = 3 |
| 245 | dm = xgb.DMatrix(data=np.random.randn(N, F), label=np.arange(N)) |
| 246 | folds = [ |
| 247 | # Train Test |
| 248 | ([1, 3], [5, 8]), |
| 249 | ([7, 9], [23, 43, 11]), |
| 250 | ] |
| 251 | |
| 252 | # Use callback to log the test labels in each fold |
| 253 | class Callback(xgb.callback.TrainingCallback): |
| 254 | def __init__(self) -> None: |
| 255 | super().__init__() |
| 256 | |
| 257 | def after_iteration( |
| 258 | self, |
| 259 | model, |
| 260 | epoch: int, |
| 261 | evals_log: xgb.callback.TrainingCallback.EvalsLog, |
| 262 | ): |
| 263 | print([fold.dtest.get_label() for fold in model.cvfolds]) |
| 264 | |
| 265 | cb = Callback() |
| 266 | |
| 267 | # Run cross validation and capture standard out to test callback result |
| 268 | with tm.captured_output() as (out, err): |
| 269 | xgb.cv( |
| 270 | params, |
| 271 | dm, |
| 272 | num_boost_round=1, |
| 273 | folds=folds, |
| 274 | callbacks=[cb], |
| 275 | as_pandas=False, |
| 276 | ) |
| 277 | output = out.getvalue().strip() |
| 278 | solution = ( |
| 279 | "[array([5., 8.], dtype=float32), array([23., 43., 11.]," |
| 280 | + " dtype=float32)]" |
| 281 | ) |
| 282 | assert output == solution |
| 283 | |
| 284 | |
| 285 | class TestBasicPathLike: |