MCPcopy Create free account
hub / github.com/dmlc/xgboost / test_basic

Method test_basic

tests/python/test_basic.py:22–57  ·  view source on GitHub ↗
(self, tmp_path: Path)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

predictMethod · 0.95
get_labelMethod · 0.80
save_binaryMethod · 0.80
BoosterMethod · 0.80
trainMethod · 0.45
save_modelMethod · 0.45
DMatrixMethod · 0.45

Tested by

no test coverage detected