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

Method test_dart

tests/python/test_basic_models.py:34–102  ·  view source on GitHub ↗
(self, tmp_path: Path)

Source from the content-addressed store, hash-verified

32 assert err < 0.2
33
34 def test_dart(self, tmp_path: Path) -> None:
35 dtrain, dtest = tm.load_agaricus(__file__)
36 param = {
37 "max_depth": 5,
38 "objective": "binary:logistic",
39 "eval_metric": "logloss",
40 "booster": "dart",
41 "verbosity": 1,
42 }
43 # specify validations set to watch performance
44 watchlist = [(dtest, "eval"), (dtrain, "train")]
45 num_round = 2
46 bst = xgb.train(param, dtrain, num_round, watchlist)
47 # this is prediction
48 preds = bst.predict(dtest, iteration_range=(0, num_round))
49 labels = dtest.get_label()
50 err = sum(
51 1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i]
52 ) / float(len(preds))
53 # error must be smaller than 10%
54 assert err < 0.1
55
56 dtest_path = tmp_path / "dtest.dmatrix"
57 model_path = tmp_path / "xgboost.model.dart.ubj"
58 # save dmatrix into binary buffer
59 dtest.save_binary(dtest_path)
60 # save model
61 bst.save_model(model_path)
62 # load model and data in
63 bst2 = xgb.Booster(params=param, model_file=model_path)
64 dtest2 = xgb.DMatrix(dtest_path)
65
66 preds2 = bst2.predict(dtest2, iteration_range=(0, num_round))
67
68 # assert they are the same
69 assert np.sum(np.abs(preds2 - preds)) == 0
70
71 def my_logloss(preds, dtrain):
72 labels = dtrain.get_label()
73 return "logloss", np.sum(np.log(np.where(labels, preds, 1 - preds)))
74
75 # check whether custom evaluation metrics work
76 bst = xgb.train(
77 param, dtrain, num_round, evals=watchlist, custom_metric=my_logloss
78 )
79 preds3 = bst.predict(dtest, iteration_range=(0, num_round))
80 assert all(preds3 == preds)
81
82 # check whether sample_type and normalize_type work
83 num_round = 50
84 param["learning_rate"] = 0.1
85 param["rate_drop"] = 0.1
86 preds_list = []
87 for p in [
88 [p0, p1] for p0 in ["uniform", "weighted"] for p1 in ["tree", "forest"]
89 ]:
90 param["sample_type"] = p[0]
91 param["normalize_type"] = p[1]

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