test_basic.py 2.57 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)
wxchan's avatar
wxchan committed
18
19
        train_data = lgb.Dataset(X_train, max_bin=255, label=y_train)
        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
27
            "verbose": -1,
            "num_threads": 1
wxchan's avatar
wxchan committed
28
29
30
        }
        bst = lgb.Booster(params, train_data)
        bst.add_valid(valid_data, "valid_1")
wxchan's avatar
wxchan committed
31

wxchan's avatar
wxchan committed
32
33
34
35
36
37
38
        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:
39
40
            tname = f.name
        with open(tname, "w+b") as f:
Guolin Ke's avatar
Guolin Ke committed
41
            dump_svmlight_file(X_test, y_test, f)
42
43
        pred_from_file = bst.predict(tname)
        os.remove(tname)
wxchan's avatar
wxchan committed
44
45
        self.assertEqual(len(pred_from_matr), len(pred_from_file))
        for preds in zip(pred_from_matr, pred_from_file):
46
            self.assertAlmostEqual(*preds, places=15)
cbecker's avatar
cbecker committed
47

wxchan's avatar
wxchan committed
48
        # check saved model persistence
49
50
51
52
        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):
53
            # we need to check the consistency of model file here, so test for exact equal
54
            self.assertEqual(*preds)
cbecker's avatar
cbecker committed
55
56
57
58
59
60
61
62
63

        # check early stopping is working. Make it stop very early, so the scores should be very close to zero
        estop = lgb.PredictionEarlyStopInstance("binary", round_period=5, margin_threshold=1.5)
        pred_early_stopping = bst.predict(X_test, early_stop_instance=estop)
        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)

wxchan's avatar
wxchan committed
64
        # check pmml
65
        subprocess.call(['python', os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../pmml/pmml.py'), 'model.txt'])