Skip to content

Commit 8bc4f67

Browse files
author
xuming06
committed
update xgboost demo. xuming 20180125
1 parent 7ea99ee commit 8bc4f67

6 files changed

Lines changed: 29 additions & 31 deletions

File tree

19xgboost/data/test.demo.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TRUE 1 0 0 0.93
2+
TRUE 1 1 0 0.11
3+
TRUE 2 2.46E-170 -1 0.26
4+
TRUE 2 2.30E-61 -1 0.23
5+
TRUE 1 3.50E-16 -1 0.69
6+
FALSE 1 1.24E-29 -1 1.21

19xgboost/data/train.demo.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
TRUE 1 0 0 0.23
2+
TRUE 1 1 0 0.12
3+
TRUE 2 2.46E-170 -1 0.26
4+
TRUE 2 2.30E-61 -1 0.23
5+
TRUE 1 3.50E-16 -1 0.69
6+
FALSE 1 1.24E-29 -1 1.21
7+
FALSE 1 3.69E-75 -1 1.23
8+
FALSE 3 1 -1 0.26
9+
TRUE 4 4.58E-159 -1 0.36

19xgboost/test_variant.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@
99
from xgb import XGB
1010
from xgb_lr import XGBLR
1111

12-
train_file = "./data/train.data.sample"
13-
test_file = "./data/test.data.sample"
12+
train_file = "./data/train.demo.txt"
13+
test_file = "./data/test.demo.txt"
1414
max_feature_cnt = 40
1515
feature_max_df = 0.55
1616
feature_min_df = 3
1717
ngram_range = (1, 2)
1818
model_path = './data/'
19-
tfidf_model_name = model_path + 'tfidf_feature.model'
20-
best_feature_model_name = model_path + 'best_feature.model'
2119
xgb_model_name = model_path + 'xgb.model'
2220
lr_model_name = model_path + 'lr.model'
2321
xgblr_xgb_model_name = model_path + 'xgblr_xgb.model'
2422
xgblr_lr_model_name = model_path + 'xgblr_lr.model'
2523
one_hot_encoder_model_name = model_path + 'xgblr_ont_hot_encoder.model'
2624

2725

28-
class ClassificationTest(unittest.TestCase):
26+
class ATest(unittest.TestCase):
2927
"""Test Case for classification
3028
"""
3129

@@ -40,52 +38,35 @@ def tearDownClass(cls):
4038
def test_init(self):
4139
print("test_init")
4240
"""测试初始化函数,捕捉异常"""
43-
data_x, data_y = load_load(train_file)
41+
data_x, data_y = load_variant_data(train_file)
4442
self.assertEqual(len(data_x) > 0, True)
4543

4644
def model_train(self, train_file):
47-
train_x, train_y = load_load(train_file)
48-
features = Feature(tfidf_model_name, best_feature_model_name)
49-
features.fit(max_feature_cnt, feature_max_df,
50-
feature_min_df, ngram_range, train_x, train_y)
51-
model_train_x_feature = features.transform(train_x)
45+
train_x, train_y = load_variant_data(train_file)
5246
# xgboost
5347
print('train a single xgb model...')
5448
xgb_clf = LR(xgb_model_name)
55-
xgb_clf.train_model(model_train_x_feature, train_y)
49+
xgb_clf.train_model(train_x, train_y)
5650
print('train a single xgb model done.\n')
5751

5852
# lr
5953
print('train a single lr model...')
6054
lr_clf = LR(lr_model_name)
61-
lr_clf.train_model(model_train_x_feature, train_y)
55+
lr_clf.train_model(train_x, train_y)
6256
print('train a single LR model done.\n')
6357

64-
# xgboost+lr
65-
print('train a xgboost+lr model...')
66-
xgb_lr_clf = XGBLR(xgblr_xgb_model_name, xgblr_lr_model_name, one_hot_encoder_model_name)
67-
xgb_lr_clf.train_model(model_train_x_feature, train_y)
68-
print('train a xgboost+lr model done.\n')
69-
7058
def model_test(self, test_file):
71-
test_x, test_y = load_load(test_file)
72-
features = Feature(tfidf_model_name, best_feature_model_name)
73-
features.load_model()
74-
model_test_x_feature = features.transform(test_x)
59+
test_x, test_y = load_variant_data(test_file)
7560

7661
xgb_clf = XGB(xgb_model_name)
77-
xgb_clf.test_model(model_test_x_feature, test_y)
62+
xgb_clf.test_model(test_x, test_y)
7863

7964
lr_clf = LR(lr_model_name)
80-
lr_clf.test_model(model_test_x_feature, test_y)
81-
82-
83-
xgb_lr_clf = XGBLR(xgblr_xgb_model_name, xgblr_lr_model_name, one_hot_encoder_model_name)
84-
xgb_lr_clf.test_model(model_test_x_feature, test_y)
65+
lr_clf.test_model(test_x, test_y)
8566

8667
def test_models(self):
8768
self.model_train(train_file)
88-
self.model_test(test_file=test_file)
69+
self.model_test(test_file)
8970

9071

9172
if __name__ == '__main__':

19xgboost/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def load_variant_data(data_path):
4242
if len(parts) < 2:
4343
print('err, must more than 2 parts.')
4444
continue
45-
data = ' '.join(parts[1:])
45+
data_content = ' '.join(parts[1:])
46+
data = data_content.strip().split()
47+
data = [float(x) for x in data]
4648
tag = parts[0].strip()
4749
if tag == '':
4850
continue

0 commit comments

Comments
 (0)