"git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "2543c8edccd50e54c71c414c3d99f551afe4046d"
Commit 350b4612 authored by wxchan's avatar wxchan Committed by Guolin Ke
Browse files

[python] add python examples to test && fix dump model examples (#924)

* add python examples to test [test should fail]

* fix example && move some to advanced_example

* fix last line

* refine test python version
parent 2693d1c7
...@@ -17,16 +17,13 @@ env: ...@@ -17,16 +17,13 @@ env:
- PYTHON_VERSION=3.6 - PYTHON_VERSION=3.6
matrix: matrix:
- TASK=regular - TASK=regular
- TASK=mpi - TASK=mpi PYTHON_VERSION=2.7
- TASK=pylint - TASK=pylint
- TASK=if-else - TASK=if-else
- TASK=sdist - TASK=sdist PYTHON_VERSION=3.4
- TASK=bdist - TASK=bdist PYTHON_VERSION=3.5
- TASK=gpu METHOD=source - TASK=gpu METHOD=source
- TASK=gpu METHOD=pip - TASK=gpu METHOD=pip
- TASK=regular PYTHON_VERSION=2.7
- TASK=regular PYTHON_VERSION=3.4
- TASK=regular PYTHON_VERSION=3.5
matrix: matrix:
exclude: exclude:
...@@ -34,6 +31,8 @@ matrix: ...@@ -34,6 +31,8 @@ matrix:
env: TASK=gpu METHOD=source env: TASK=gpu METHOD=source
- os: osx - os: osx
env: TASK=gpu METHOD=pip env: TASK=gpu METHOD=pip
- os: osx
env: TASK=if-else
- os: osx - os: osx
env: TASK=pylint env: TASK=pylint
......
...@@ -92,3 +92,7 @@ make _lightgbm || exit -1 ...@@ -92,3 +92,7 @@ make _lightgbm || exit -1
cd $TRAVIS_BUILD_DIR/python-package && python setup.py install --precompile || exit -1 cd $TRAVIS_BUILD_DIR/python-package && python setup.py install --precompile || exit -1
cd $TRAVIS_BUILD_DIR && pytest . || exit -1 cd $TRAVIS_BUILD_DIR && pytest . || exit -1
if [[ ${TASK} == "regular" ]]; then
cd $TRAVIS_BUILD_DIR/examples/python-guide && python simple_example.py && python sklearn_example.py && python advanced_example.py || exit -1
fi
...@@ -27,14 +27,15 @@ Examples include: ...@@ -27,14 +27,15 @@ Examples include:
- Eval during training - Eval during training
- Early stopping - Early stopping
- Save model to file - Save model to file
- Dump model to json format
- Feature importances
- [sklearn_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/sklearn_example.py) - [sklearn_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/sklearn_example.py)
- Basic train and predict with sklearn interface - Basic train and predict with sklearn interface
- Feature importances with sklearn interface - Feature importances with sklearn interface
- [advanced_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/advanced_example.py) - [advanced_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/advanced_example.py)
- Set feature names - Set feature names
- Directly use categorical features without one-hot encoding - Directly use categorical features without one-hot encoding
- Dump model to json format
- Get feature importances
- Get feature names
- Load model to predict - Load model to predict
- Dump and load model with pickle - Dump and load model with pickle
- Load model file to continue training - Load model file to continue training
......
# coding: utf-8 # coding: utf-8
# pylint: disable = invalid-name, C0111 # pylint: disable = invalid-name, C0111
import json
import lightgbm as lgb import lightgbm as lgb
import pandas as pd import pandas as pd
import numpy as np import numpy as np
...@@ -63,6 +64,19 @@ print('7th feature name is:', repr(lgb_train.feature_name[6])) ...@@ -63,6 +64,19 @@ print('7th feature name is:', repr(lgb_train.feature_name[6]))
# save model to file # save model to file
gbm.save_model('model.txt') gbm.save_model('model.txt')
# dump model to json (and save to file)
print('Dump model to JSON...')
model_json = gbm.dump_model()
with open('model.json', 'w+') as f:
json.dump(model_json, f, indent=4)
# feature names
print('Feature names:', gbm.feature_name())
# feature importances
print('Feature importances:', list(gbm.feature_importance()))
# load model to predict # load model to predict
print('Load model to predict') print('Load model to predict')
bst = lgb.Booster(model_file='model.txt') bst = lgb.Booster(model_file='model.txt')
......
...@@ -46,17 +46,8 @@ print('Save model...') ...@@ -46,17 +46,8 @@ print('Save model...')
# save model to file # save model to file
gbm.save_model('model.txt') gbm.save_model('model.txt')
# dump model to json format
with open('model.json', 'w') as model:
model.write(gbm.dump_model(num_iteration=gbm.best_iteration))
print('Start predicting...') print('Start predicting...')
# predict # predict
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration) y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
# eval # eval
print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5) print('The rmse of prediction is:', mean_squared_error(y_test, y_pred) ** 0.5)
print('Feature names:', gbm.feature_name())
# feature importances
print('Feature importances:', list(gbm.feature_importance()))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment