forked from iterative/dvclive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xgboost.py
58 lines (44 loc) · 1.44 KB
/
test_xgboost.py
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
import os
import numpy as np
import pandas as pd
import pytest
import xgboost as xgb
from funcy import first
from sklearn import datasets
from dvclive.data.scalar import Scalar
from dvclive.xgb import DvcLiveCallback
from tests.test_main import read_logs
# pylint: disable=redefined-outer-name, unused-argument
@pytest.fixture
def train_params():
return {"objective": "multi:softmax", "num_class": 3, "seed": 0}
@pytest.fixture
def iris_data():
iris = datasets.load_iris()
x = pd.DataFrame(iris["data"], columns=iris["feature_names"])
y = iris["target"]
return xgb.DMatrix(x, y)
def test_xgb_integration(tmp_dir, train_params, iris_data):
xgb.train(
train_params,
iris_data,
callbacks=[DvcLiveCallback("eval_data")],
num_boost_round=5,
evals=[(iris_data, "eval_data")],
)
assert os.path.exists("dvclive")
logs, _ = read_logs(tmp_dir / "dvclive" / Scalar.subfolder)
assert len(logs) == 1
assert len(first(logs.values())) == 5
def test_xgb_model_file(tmp_dir, train_params, iris_data):
model = xgb.train(
train_params,
iris_data,
callbacks=[DvcLiveCallback("eval_data", model_file="model_xgb.json")],
num_boost_round=5,
evals=[(iris_data, "eval_data")],
)
preds = model.predict(iris_data)
model2 = xgb.Booster(model_file="model_xgb.json")
preds2 = model2.predict(iris_data)
assert np.sum(np.abs(preds2 - preds)) == 0