# Source: content/notes/libraries/boosting-libraries.md
# Independent CPU example; use the curriculum environment.
# See /notes/ml/#example-environment or /notes/deep-learning/#example-environment.

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from xgboost import XGBClassifier
g = np.array([.5, .5, -.5, -.5])
h = np.full(4, .25)
gain = .5*(g[:2].sum()**2/(h[:2].sum()+1)+g[2:].sum()**2/(h[2:].sum()+1))-.1
assert np.isclose(gain, 17/30)
X, y = make_classification(n_samples=240, n_features=6, n_informative=4, random_state=8)
train, valid, yt, yv = train_test_split(X, y, test_size=.25, stratify=y, random_state=2)
model = XGBClassifier(n_estimators=60, max_depth=3, learning_rate=.1,
    tree_method="hist", device="cpu", n_jobs=1, random_state=0,
    eval_metric="logloss", early_stopping_rounds=5)
model.fit(train, yt, eval_set=[(valid, yv)], verbose=False)
automatic = model.predict_proba(valid)
explicit = model.predict_proba(valid, iteration_range=(0, model.best_iteration+1))
np.testing.assert_allclose(automatic, explicit)
assert roc_auc_score(yv, automatic[:, 1]) > .75
print("gain", gain, "best iteration", model.best_iteration, "wrapper parity passed")
