test_basic.py 1.94 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
4
5
6
7
import os
import tempfile
import unittest

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

wxchan's avatar
wxchan committed
12

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

wxchan's avatar
wxchan committed
15
    def test(self):
Guolin Ke's avatar
Guolin Ke committed
16
        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
17
18
        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
19

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

wxchan's avatar
wxchan committed
30
31
32
33
34
35
36
        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:
37
38
            tname = f.name
        with open(tname, "w+b") as f:
Guolin Ke's avatar
Guolin Ke committed
39
            dump_svmlight_file(X_test, y_test, f)
40
41
        pred_from_file = bst.predict(tname)
        os.remove(tname)
wxchan's avatar
wxchan committed
42
43
        self.assertEqual(len(pred_from_matr), len(pred_from_file))
        for preds in zip(pred_from_matr, pred_from_file):
44
            self.assertAlmostEqual(*preds, places=15)
wxchan's avatar
wxchan committed
45
        # check saved model persistence
46
47
48
49
        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):
50
            self.assertEqual(*preds)
wxchan's avatar
wxchan committed
51
        # check pmml
52
        os.system('python ../../pmml/pmml.py model.txt')
wxchan's avatar
wxchan committed
53

wxchan's avatar
wxchan committed
54

wxchan's avatar
wxchan committed
55
56
57
print("----------------------------------------------------------------------")
print("running test_basic.py")
unittest.main()