From ae0a67487af4f49aae8e3ed32e5f3035096359c1 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Thu, 4 Jan 2024 23:24:45 +0800 Subject: [PATCH] cleanup. --- tests/python/test_basic.py | 139 +++++++++++++++++------------- tests/python/test_basic_models.py | 2 +- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/tests/python/test_basic.py b/tests/python/test_basic.py index 58135de44eae..cdc571a916df 100644 --- a/tests/python/test_basic.py +++ b/tests/python/test_basic.py @@ -10,46 +10,48 @@ import xgboost as xgb from xgboost import testing as tm -dpath = 'demo/data/' +dpath = "demo/data/" rng = np.random.RandomState(1994) class TestBasic: def test_compat(self): from xgboost.compat import lazy_isinstance + a = np.array([1, 2, 3]) - assert lazy_isinstance(a, 'numpy', 'ndarray') - assert not lazy_isinstance(a, 'numpy', 'dataframe') + assert lazy_isinstance(a, "numpy", "ndarray") + assert not lazy_isinstance(a, "numpy", "dataframe") def test_basic(self): dtrain, dtest = tm.load_agaricus(__file__) - param = {'max_depth': 2, 'eta': 1, - 'objective': 'binary:logistic'} + param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} # specify validations set to watch performance - watchlist = [(dtrain, 'train')] + watchlist = [(dtrain, "train")] num_round = 2 - bst = xgb.train(param, dtrain, num_round, watchlist, verbose_eval=True) + bst = xgb.train(param, dtrain, num_round, evals=watchlist, verbose_eval=True) preds = bst.predict(dtrain) labels = dtrain.get_label() - err = sum(1 for i in range(len(preds)) - if int(preds[i] > 0.5) != labels[i]) / float(len(preds)) + err = sum( + 1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i] + ) / float(len(preds)) # error must be smaller than 10% assert err < 0.1 preds = bst.predict(dtest) labels = dtest.get_label() - err = sum(1 for i in range(len(preds)) - if int(preds[i] > 0.5) != labels[i]) / float(len(preds)) + err = sum( + 1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i] + ) / float(len(preds)) # error must be smaller than 10% assert err < 0.1 with tempfile.TemporaryDirectory() as tmpdir: - dtest_path = os.path.join(tmpdir, 'dtest.dmatrix') + dtest_path = os.path.join(tmpdir, "dtest.dmatrix") # save dmatrix into binary buffer dtest.save_binary(dtest_path) # save model - model_path = os.path.join(tmpdir, 'model.booster') + model_path = os.path.join(tmpdir, "model.ubj") bst.save_model(model_path) # load model and data in bst2 = xgb.Booster(model_file=model_path) @@ -59,17 +61,21 @@ def test_basic(self): assert np.sum(np.abs(preds2 - preds)) == 0 def test_metric_config(self): - # Make sure that the metric configuration happens in booster so the - # string `['error', 'auc']` doesn't get passed down to core. + # Make sure that the metric configuration happens in booster so the string + # `['error', 'auc']` doesn't get passed down to core. dtrain, dtest = tm.load_agaricus(__file__) - param = {'max_depth': 2, 'eta': 1, - 'objective': 'binary:logistic', 'eval_metric': ['error', 'auc']} - watchlist = [(dtest, 'eval'), (dtrain, 'train')] + param = { + "max_depth": 2, + "eta": 1, + "objective": "binary:logistic", + "eval_metric": ["error", "auc"], + } + watchlist = [(dtest, "eval"), (dtrain, "train")] num_round = 2 - booster = xgb.train(param, dtrain, num_round, watchlist) + booster = xgb.train(param, dtrain, num_round, evals=watchlist) predt_0 = booster.predict(dtrain) with tempfile.TemporaryDirectory() as tmpdir: - path = os.path.join(tmpdir, 'model.json') + path = os.path.join(tmpdir, "model.json") booster.save_model(path) booster = xgb.Booster(params=param, model_file=path) @@ -78,22 +84,23 @@ def test_metric_config(self): def test_multiclass(self): dtrain, dtest = tm.load_agaricus(__file__) - param = {'max_depth': 2, 'eta': 1, 'num_class': 2} + param = {"max_depth": 2, "eta": 1, "num_class": 2} # specify validations set to watch performance - watchlist = [(dtest, 'eval'), (dtrain, 'train')] + watchlist = [(dtest, "eval"), (dtrain, "train")] num_round = 2 - bst = xgb.train(param, dtrain, num_round, watchlist) + bst = xgb.train(param, dtrain, num_round, evals=watchlist) # this is prediction preds = bst.predict(dtest) labels = dtest.get_label() - err = sum(1 for i in range(len(preds)) - if preds[i] != labels[i]) / float(len(preds)) + err = sum(1 for i in range(len(preds)) if preds[i] != labels[i]) / float( + len(preds) + ) # error must be smaller than 10% assert err < 0.1 with tempfile.TemporaryDirectory() as tmpdir: - dtest_path = os.path.join(tmpdir, 'dtest.buffer') - model_path = os.path.join(tmpdir, 'xgb.ubj') + dtest_path = os.path.join(tmpdir, "dtest.buffer") + model_path = os.path.join(tmpdir, "model.ubj") # save dmatrix into binary buffer dtest.save_binary(dtest_path) # save model @@ -108,33 +115,39 @@ def test_multiclass(self): def test_dump(self): data = np.random.randn(100, 2) target = np.array([0, 1] * 50) - features = ['Feature1', 'Feature2'] + features = ["Feature1", "Feature2"] dm = xgb.DMatrix(data, label=target, feature_names=features) - params = {'objective': 'binary:logistic', - 'eval_metric': 'logloss', - 'eta': 0.3, - 'max_depth': 1} + params = { + "objective": "binary:logistic", + "eval_metric": "logloss", + "eta": 0.3, + "max_depth": 1, + } bst = xgb.train(params, dm, num_boost_round=1) # number of feature importances should == number of features dump1 = bst.get_dump() - assert len(dump1) == 1, 'Expected only 1 tree to be dumped.' - len(dump1[0].splitlines()) == 3, 'Expected 1 root and 2 leaves - 3 lines in dump.' + assert len(dump1) == 1, "Expected only 1 tree to be dumped." + len( + dump1[0].splitlines() + ) == 3, "Expected 1 root and 2 leaves - 3 lines in dump." dump2 = bst.get_dump(with_stats=True) - assert dump2[0].count('\n') == 3, 'Expected 1 root and 2 leaves - 3 lines in dump.' - msg = 'Expected more info when with_stats=True is given.' - assert dump2[0].find('\n') > dump1[0].find('\n'), msg + assert ( + dump2[0].count("\n") == 3 + ), "Expected 1 root and 2 leaves - 3 lines in dump." + msg = "Expected more info when with_stats=True is given." + assert dump2[0].find("\n") > dump1[0].find("\n"), msg dump3 = bst.get_dump(dump_format="json") dump3j = json.loads(dump3[0]) - assert dump3j['nodeid'] == 0, 'Expected the root node on top.' + assert dump3j["nodeid"] == 0, "Expected the root node on top." dump4 = bst.get_dump(dump_format="json", with_stats=True) dump4j = json.loads(dump4[0]) - assert 'gain' in dump4j, "Expected 'gain' to be dumped in JSON." + assert "gain" in dump4j, "Expected 'gain' to be dumped in JSON." with pytest.raises(ValueError): bst.get_dump(fmap="foo") @@ -163,12 +176,14 @@ def test_feature_score(self): def test_load_file_invalid(self): with pytest.raises(xgb.core.XGBoostError): - xgb.Booster(model_file='incorrect_path') + xgb.Booster(model_file="incorrect_path") with pytest.raises(xgb.core.XGBoostError): - xgb.Booster(model_file=u'不正なパス') + xgb.Booster(model_file="不正なパス") - @pytest.mark.parametrize("path", ["모델.ubj", "がうる・ぐら.json"], ids=["path-0", "path-1"]) + @pytest.mark.parametrize( + "path", ["모델.ubj", "がうる・ぐら.json"], ids=["path-0", "path-1"] + ) def test_unicode_path(self, tmpdir, path): model_path = pathlib.Path(tmpdir) / path dtrain, _ = tm.load_agaricus(__file__) @@ -180,12 +195,11 @@ def test_unicode_path(self, tmpdir, path): assert bst.get_dump(dump_format="text") == bst2.get_dump(dump_format="text") def test_dmatrix_numpy_init_omp(self): - rows = [1000, 11326, 15000] cols = 50 for row in rows: X = np.random.randn(row, cols) - y = np.random.randn(row).astype('f') + y = np.random.randn(row).astype("f") dm = xgb.DMatrix(X, y, nthread=0) np.testing.assert_array_equal(dm.get_label(), y) assert dm.num_row() == row @@ -198,7 +212,7 @@ def test_dmatrix_numpy_init_omp(self): def test_cv(self): dm, _ = tm.load_agaricus(__file__) - params = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'} + params = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} # return np.ndarray cv = xgb.cv(params, dm, num_boost_round=10, nfold=10, as_pandas=False) @@ -207,17 +221,18 @@ def test_cv(self): def test_cv_no_shuffle(self): dm, _ = tm.load_agaricus(__file__) - params = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'} + params = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} # return np.ndarray - cv = xgb.cv(params, dm, num_boost_round=10, shuffle=False, nfold=10, - as_pandas=False) + cv = xgb.cv( + params, dm, num_boost_round=10, shuffle=False, nfold=10, as_pandas=False + ) assert isinstance(cv, dict) assert len(cv) == (4) def test_cv_explicit_fold_indices(self): dm, _ = tm.load_agaricus(__file__) - params = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'} + params = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"} folds = [ # Train Test ([1, 3], [5, 8]), @@ -225,14 +240,13 @@ def test_cv_explicit_fold_indices(self): ] # return np.ndarray - cv = xgb.cv(params, dm, num_boost_round=10, folds=folds, - as_pandas=False) + cv = xgb.cv(params, dm, num_boost_round=10, folds=folds, as_pandas=False) assert isinstance(cv, dict) assert len(cv) == (4) @pytest.mark.skipif(**tm.skip_s390x()) def test_cv_explicit_fold_indices_labels(self): - params = {'max_depth': 2, 'eta': 1, 'objective': 'reg:squarederror'} + params = {"max_depth": 2, "eta": 1, "objective": "reg:squarederror"} N = 100 F = 3 dm = xgb.DMatrix(data=np.random.randn(N, F), label=np.arange(N)) @@ -248,9 +262,10 @@ def __init__(self) -> None: super().__init__() def after_iteration( - self, model, + self, + model, epoch: int, - evals_log: xgb.callback.TrainingCallback.EvalsLog + evals_log: xgb.callback.TrainingCallback.EvalsLog, ): print([fold.dtest.get_label() for fold in model.cvfolds]) @@ -259,12 +274,18 @@ def after_iteration( # Run cross validation and capture standard out to test callback result with tm.captured_output() as (out, err): xgb.cv( - params, dm, num_boost_round=1, folds=folds, callbacks=[cb], - as_pandas=False + params, + dm, + num_boost_round=1, + folds=folds, + callbacks=[cb], + as_pandas=False, ) output = out.getvalue().strip() - solution = ('[array([5., 8.], dtype=float32), array([23., 43., 11.],' + - ' dtype=float32)]') + solution = ( + "[array([5., 8.], dtype=float32), array([23., 43., 11.]," + + " dtype=float32)]" + ) assert output == solution @@ -281,7 +302,7 @@ def test_DMatrix_save_to_path(self): """Saving to a binary file using pathlib from a DMatrix.""" data = np.random.randn(100, 2) target = np.array([0, 1] * 50) - features = ['Feature1', 'Feature2'] + features = ["Feature1", "Feature2"] dm = xgb.DMatrix(data, label=target, feature_names=features) diff --git a/tests/python/test_basic_models.py b/tests/python/test_basic_models.py index 9049758680f5..ca35c4e9105d 100644 --- a/tests/python/test_basic_models.py +++ b/tests/python/test_basic_models.py @@ -49,7 +49,7 @@ def test_dart(self): with tempfile.TemporaryDirectory() as tmpdir: dtest_path = os.path.join(tmpdir, 'dtest.dmatrix') - model_path = os.path.join(tmpdir, 'xgboost.model.dart') + model_path = os.path.join(tmpdir, "xgboost.model.dart.ubj") # save dmatrix into binary buffer dtest.save_binary(dtest_path) model_path = model_path