test_basic.py 2.46 KB
Newer Older
wxchan's avatar
wxchan committed
1
# coding: utf-8
wxchan's avatar
wxchan committed
2
# pylint: skip-file
wxchan's avatar
wxchan committed
3
import os
4
import subprocess
wxchan's avatar
wxchan committed
5
6
7
8
import tempfile
import unittest

import lightgbm as lgb
wxchan's avatar
wxchan committed
9
import numpy as np
Guolin Ke's avatar
Guolin Ke committed
10
from sklearn.datasets import load_breast_cancer, dump_svmlight_file
wxchan's avatar
wxchan committed
11
from sklearn.model_selection import train_test_split
wxchan's avatar
wxchan committed
12

wxchan's avatar
wxchan committed
13

wxchan's avatar
wxchan committed
14
class TestBasic(unittest.TestCase):
wxchan's avatar
wxchan committed
15

wxchan's avatar
wxchan committed
16
    def test(self):
Guolin Ke's avatar
Guolin Ke committed
17
        X_train, X_test, y_train, y_test = train_test_split(*load_breast_cancer(True), test_size=0.1, random_state=2)
18
        train_data = lgb.Dataset(X_train, label=y_train)
wxchan's avatar
wxchan committed
19
        valid_data = train_data.create_valid(X_test, label=y_test)
wxchan's avatar
wxchan committed
20

wxchan's avatar
wxchan committed
21
        params = {
wxchan's avatar
wxchan committed
22
23
            "objective": "binary",
            "metric": "auc",
Guolin Ke's avatar
Guolin Ke committed
24
            "min_data": 10,
wxchan's avatar
wxchan committed
25
            "num_leaves": 15,
26
            "verbose": -1,
27
28
            "num_threads": 1,
            "max_bin": 255
wxchan's avatar
wxchan committed
29
30
31
        }
        bst = lgb.Booster(params, train_data)
        bst.add_valid(valid_data, "valid_1")
wxchan's avatar
wxchan committed
32

wxchan's avatar
wxchan committed
33
34
35
36
37
38
39
        for i in range(30):
            bst.update()
            if i % 10 == 0:
                print(bst.eval_train(), bst.eval_valid())
        bst.save_model("model.txt")
        pred_from_matr = bst.predict(X_test)
        with tempfile.NamedTemporaryFile() as f:
40
41
            tname = f.name
        with open(tname, "w+b") as f:
Guolin Ke's avatar
Guolin Ke committed
42
            dump_svmlight_file(X_test, y_test, f)
43
44
        pred_from_file = bst.predict(tname)
        os.remove(tname)
wxchan's avatar
wxchan committed
45
46
        self.assertEqual(len(pred_from_matr), len(pred_from_file))
        for preds in zip(pred_from_matr, pred_from_file):
47
            self.assertAlmostEqual(*preds, places=15)
cbecker's avatar
cbecker committed
48

wxchan's avatar
wxchan committed
49
        # check saved model persistence
50
51
52
53
        bst = lgb.Booster(params, model_file="model.txt")
        pred_from_model_file = bst.predict(X_test)
        self.assertEqual(len(pred_from_matr), len(pred_from_model_file))
        for preds in zip(pred_from_matr, pred_from_model_file):
54
            # we need to check the consistency of model file here, so test for exact equal
55
            self.assertEqual(*preds)
cbecker's avatar
cbecker committed
56
57

        # check early stopping is working. Make it stop very early, so the scores should be very close to zero
58
59
        pred_parameter = {"pred_early_stop": True, "pred_early_stop_freq": 5, "pred_early_stop_margin": 1.5}
        pred_early_stopping = bst.predict(X_test, pred_parameter=pred_parameter)
cbecker's avatar
cbecker committed
60
61
62
63
        self.assertEqual(len(pred_from_matr), len(pred_early_stopping))
        for preds in zip(pred_early_stopping, pred_from_matr):
            # scores likely to be different, but prediction should still be the same
            self.assertEqual(preds[0] > 0, preds[1] > 0)