File size: 1,566 Bytes
eb4abb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared XGBoost config used by training and evaluation. Loads from config/default.yaml when present."""

from copy import deepcopy

from xgboost import XGBClassifier


def _load_xgb_params():
    try:
        from config import get
        xgb = get("xgboost") or {}
        return {
            "n_estimators": xgb.get("n_estimators", 600),
            "max_depth": xgb.get("max_depth", 8),
            "learning_rate": xgb.get("learning_rate", 0.1489),
            "subsample": xgb.get("subsample", 0.9625),
            "colsample_bytree": xgb.get("colsample_bytree", 0.9013),
            "reg_alpha": xgb.get("reg_alpha", 1.1407),
            "reg_lambda": xgb.get("reg_lambda", 2.4181),
            "eval_metric": xgb.get("eval_metric", "logloss"),
        }
    except Exception:
        return {
            "n_estimators": 600,
            "max_depth": 8,
            "learning_rate": 0.1489,
            "subsample": 0.9625,
            "colsample_bytree": 0.9013,
            "reg_alpha": 1.1407,
            "reg_lambda": 2.4181,
            "eval_metric": "logloss",
        }


XGB_BASE_PARAMS = _load_xgb_params()


def get_xgb_params():
    return deepcopy(XGB_BASE_PARAMS)


def build_xgb_classifier(seed: int, *, verbosity: int = 0, early_stopping_rounds=None):
    params = get_xgb_params()
    params.update(
        {
            "random_state": seed,
            "verbosity": verbosity,
        }
    )
    if early_stopping_rounds is not None:
        params["early_stopping_rounds"] = early_stopping_rounds
    return XGBClassifier(**params)