(self, tmp_path: Path)
| 20 | assert not lazy_isinstance(a, "numpy", "dataframe") |
| 21 | |
| 22 | def test_basic(self, tmp_path: Path) -> None: |
| 23 | dtrain, dtest = tm.load_agaricus(__file__) |
| 24 | param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} |
| 25 | # specify validations set to watch performance |
| 26 | watchlist = [(dtrain, "train")] |
| 27 | num_round = 2 |
| 28 | bst = xgb.train(param, dtrain, num_round, evals=watchlist, verbose_eval=True) |
| 29 | |
| 30 | preds = bst.predict(dtrain) |
| 31 | labels = dtrain.get_label() |
| 32 | err = sum( |
| 33 | 1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i] |
| 34 | ) / float(len(preds)) |
| 35 | # error must be smaller than 10% |
| 36 | assert err < 0.1 |
| 37 | |
| 38 | preds = bst.predict(dtest) |
| 39 | labels = dtest.get_label() |
| 40 | err = sum( |
| 41 | 1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i] |
| 42 | ) / float(len(preds)) |
| 43 | # error must be smaller than 10% |
| 44 | assert err < 0.1 |
| 45 | |
| 46 | dtest_path = tmp_path / "dtest.dmatrix" |
| 47 | # save dmatrix into binary buffer |
| 48 | dtest.save_binary(dtest_path) |
| 49 | # save model |
| 50 | model_path = tmp_path / "model.ubj" |
| 51 | bst.save_model(model_path) |
| 52 | # load model and data in |
| 53 | bst2 = xgb.Booster(model_file=model_path) |
| 54 | dtest2 = xgb.DMatrix(dtest_path) |
| 55 | preds2 = bst2.predict(dtest2) |
| 56 | # assert they are the same |
| 57 | assert np.sum(np.abs(preds2 - preds)) == 0 |
| 58 | |
| 59 | def test_metric_config(self, tmp_path: Path) -> None: |
| 60 | # Make sure that the metric configuration happens in booster so the string |
nothing calls this directly
no test coverage detected