simple_example.py 1.64 KB
Newer Older
wxchan's avatar
wxchan committed
1
2
3
4
5
6
7
# coding: utf-8
# pylint: disable = invalid-name, C0111
import json
import lightgbm as lgb
import pandas as pd
from sklearn.metrics import mean_squared_error

8

wxchan's avatar
wxchan committed
9
# load or create your dataset
10
print('Load data...')
wxchan's avatar
wxchan committed
11
12
13
df_train = pd.read_csv('../regression/regression.train', header=None, sep='\t')
df_test = pd.read_csv('../regression/regression.test', header=None, sep='\t')

wxchan's avatar
wxchan committed
14
15
16
17
y_train = df_train[0].values
y_test = df_test[0].values
X_train = df_train.drop(0, axis=1).values
X_test = df_test.drop(0, axis=1).values
wxchan's avatar
wxchan committed
18
19
20
21

# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
Guolin Ke's avatar
Guolin Ke committed
22

wxchan's avatar
wxchan committed
23
24
# specify your configurations as a dict
params = {
wxchan's avatar
wxchan committed
25
26
27
28
29
30
31
32
    'task': 'train',
    'boosting_type': 'gbdt',
    'objective': 'regression',
    'metric': {'l2', 'auc'},
    'num_leaves': 31,
    'learning_rate': 0.05,
    'feature_fraction': 0.9,
    'bagging_fraction': 0.8,
wxchan's avatar
wxchan committed
33
    'bagging_freq': 5,
wxchan's avatar
wxchan committed
34
    'verbose': 0
wxchan's avatar
wxchan committed
35
36
}

37
print('Start training...')
wxchan's avatar
wxchan committed
38
39
40
# train
gbm = lgb.train(params,
                lgb_train,
41
                num_boost_round=20,
Guolin Ke's avatar
Guolin Ke committed
42
                valid_sets=lgb_eval,
43
                early_stopping_rounds=5)
wxchan's avatar
wxchan committed
44

45
print('Save model...')
wxchan's avatar
wxchan committed
46
47
48
# save model to file
gbm.save_model('model.txt')

49
50
51
52
# dump model to json format
with open('model.json', 'w') as model:
    model.write(gbm.dump_model(num_iteration=gbm.best_iteration))

53
print('Start predicting...')
wxchan's avatar
wxchan committed
54
55
56
57
58
# predict
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
# eval
print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)

wxchan's avatar
wxchan committed
59
60
print('Feature names:', gbm.feature_name())

61
# feature importances
62
print('Feature importances:', list(gbm.feature_importance()))