File: test_early_stopping.py

package info (click to toggle)
xgboost 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,796 kB
  • sloc: cpp: 67,502; python: 35,503; java: 4,676; ansic: 1,426; sh: 1,320; xml: 1,197; makefile: 204; javascript: 19
file content (159 lines) | stat: -rw-r--r-- 5,267 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from typing import Tuple

import numpy as np
import pytest

import xgboost as xgb
from xgboost import testing as tm
from xgboost.testing.updater import get_basescore

rng = np.random.RandomState(1994)


class TestEarlyStopping:
    @pytest.mark.skipif(**tm.no_sklearn())
    def test_early_stopping_nonparallel(self):
        from sklearn.datasets import load_digits
        from sklearn.model_selection import train_test_split

        X, y = load_digits(n_class=2, return_X_y=True)
        X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
        clf1 = xgb.XGBClassifier(
            learning_rate=0.1, early_stopping_rounds=5, eval_metric="auc"
        )
        clf1.fit(X_train, y_train, eval_set=[(X_test, y_test)])
        clf2 = xgb.XGBClassifier(
            learning_rate=0.1, early_stopping_rounds=4, eval_metric="auc"
        )
        clf2.fit(X_train, y_train, eval_set=[(X_test, y_test)])
        # should be the same
        assert clf1.best_score == clf2.best_score
        assert clf1.best_score != 1
        # check overfit
        clf3 = xgb.XGBClassifier(
            learning_rate=0.1, eval_metric="auc", early_stopping_rounds=10
        )
        clf3.fit(X_train, y_train, eval_set=[(X_test, y_test)])
        base_score = get_basescore(clf3)
        assert 0.53 > base_score > 0.5

        clf3 = xgb.XGBClassifier(
            learning_rate=0.1,
            base_score=0.5,
            eval_metric="auc",
            early_stopping_rounds=10,
        )
        clf3.fit(X_train, y_train, eval_set=[(X_test, y_test)])

        assert clf3.best_score == 1

    @staticmethod
    def assert_metrics_length(cv, expected_length):
        for key, value in cv.items():
            assert len(value) == expected_length

    @pytest.mark.skipif(**tm.no_sklearn())
    def test_cv_early_stopping(self) -> None:
        from sklearn.datasets import load_digits

        X, y = load_digits(n_class=2, return_X_y=True)
        dm = xgb.DMatrix(X, label=y)
        params = {
            "max_depth": 2,
            "eta": 1,
            "objective": "binary:logistic",
            "eval_metric": "error",
        }

        def evalerror(preds: np.ndarray, dtrain: xgb.DMatrix) -> Tuple[str, float]:
            from sklearn.metrics import mean_squared_error

            labels = dtrain.get_label()
            return "rmse", mean_squared_error(labels, preds)

        cv = xgb.cv(params, dm, num_boost_round=10, nfold=10, early_stopping_rounds=10)
        self.assert_metrics_length(cv, 10)
        cv = xgb.cv(params, dm, num_boost_round=10, nfold=10, early_stopping_rounds=5)
        self.assert_metrics_length(cv, 3)
        cv = xgb.cv(params, dm, num_boost_round=10, nfold=10, early_stopping_rounds=1)
        self.assert_metrics_length(cv, 1)

        cv = xgb.cv(
            params,
            dm,
            num_boost_round=10,
            nfold=10,
            custom_metric=evalerror,
            early_stopping_rounds=10,
        )
        self.assert_metrics_length(cv, 10)
        cv = xgb.cv(
            params,
            dm,
            num_boost_round=10,
            nfold=10,
            custom_metric=evalerror,
            early_stopping_rounds=1,
        )
        self.assert_metrics_length(cv, 5)
        cv = xgb.cv(
            params,
            dm,
            num_boost_round=10,
            nfold=10,
            custom_metric=evalerror,
            maximize=True,
            early_stopping_rounds=1,
        )
        self.assert_metrics_length(cv, 1)

        with pytest.raises(ValueError, match="`save_best`"):
            cv = xgb.cv(
                params,
                dm,
                num_boost_round=10,
                nfold=10,
                early_stopping_rounds=1,
                callbacks=[xgb.callback.EarlyStopping(3, save_best=True)],
            )

    @pytest.mark.skipif(**tm.no_sklearn())
    @pytest.mark.skipif(**tm.no_pandas())
    def test_cv_early_stopping_with_multiple_eval_sets_and_metrics(self):
        from sklearn.datasets import load_breast_cancer

        X, y = load_breast_cancer(return_X_y=True)
        dm = xgb.DMatrix(X, label=y)
        params = {"objective": "binary:logistic"}

        metrics = [
            ["auc"],
            ["error"],
            ["logloss"],
            ["logloss", "auc"],
            ["logloss", "error"],
            ["error", "logloss"],
        ]

        num_iteration_history = []

        # If more than one metrics is given, early stopping should use the last metric
        for i, m in enumerate(metrics):
            result = xgb.cv(
                params,
                dm,
                num_boost_round=1000,
                nfold=5,
                stratified=True,
                metrics=m,
                early_stopping_rounds=20,
                seed=42,
            )
            num_iteration_history.append(len(result))
            df = result["test-{}-mean".format(m[-1])]
            # When early stopping is invoked, the last metric should be as best it can be.
            if m[-1] == "auc":
                assert np.all(df <= df.iloc[-1])
            else:
                assert np.all(df >= df.iloc[-1])
        assert num_iteration_history[:3] == num_iteration_history[3:]