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

Method test_multiclass

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

Source from the content-addressed store, hash-verified

78 np.testing.assert_allclose(predt_0, predt_1)
79
80 def test_multiclass(self, tmp_path: Path) -> None:
81 dtrain, dtest = tm.load_agaricus(__file__)
82 param = {"max_depth": 2, "eta": 1, "num_class": 2}
83 # specify validations set to watch performance
84 watchlist = [(dtest, "eval"), (dtrain, "train")]
85 num_round = 2
86 bst = xgb.train(param, dtrain, num_round, evals=watchlist)
87 # this is prediction
88 preds = bst.predict(dtest)
89 labels = dtest.get_label()
90 err = sum(1 for i in range(len(preds)) if preds[i] != labels[i]) / float(
91 len(preds)
92 )
93 # error must be smaller than 10%
94 assert err < 0.1
95
96 dtest_path = tmp_path / "dtest.buffer"
97 model_path = tmp_path / "model.ubj"
98 # save dmatrix into binary buffer
99 dtest.save_binary(dtest_path)
100 # save model
101 bst.save_model(model_path)
102 # load model and data in
103 bst2 = xgb.Booster(model_file=model_path)
104 dtest2 = xgb.DMatrix(dtest_path)
105 preds2 = bst2.predict(dtest2)
106 # assert they are the same
107 assert np.sum(np.abs(preds2 - preds)) == 0
108
109 def test_dump(self):
110 data = np.random.randn(100, 2)

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