"src/git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "45dd49ec0d8e3bbea7adbf855bb48f261739cf21"
sklearn_example.py 1.09 KB
Newer Older
wxchan's avatar
wxchan committed
1
2
3
4
5
6
7
# coding: utf-8
# pylint: disable = invalid-name, C0111
import lightgbm as lgb
import pandas as pd
from sklearn.metrics import mean_squared_error

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

y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)

17
print('Start training...')
wxchan's avatar
wxchan committed
18
19
20
21
# train
gbm = lgb.LGBMRegressor(objective='regression',
                        num_leaves=31,
                        learning_rate=0.05,
22
                        n_estimators=20)
wxchan's avatar
wxchan committed
23
24
gbm.fit(X_train, y_train,
        eval_set=[(X_test, y_test)],
25
26
        eval_metric='l1',
        early_stopping_rounds=5)
wxchan's avatar
wxchan committed
27

28
print('Start predicting...')
wxchan's avatar
wxchan committed
29
30
31
32
# 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)
33

34
print('Calculate feature importances...')
35
# feature importances
36
print('Feature importances:', list(gbm.feature_importance()))