Skip to content

Commit

Permalink
[python] add python examples to test && fix dump model examples (#924)
Browse files Browse the repository at this point in the history
* add python examples to test [test should fail]

* fix example && move some to advanced_example

* fix last line

* refine test python version
  • Loading branch information
wxchan authored and guolinke committed Sep 21, 2017
1 parent 2693d1c commit 350b461
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ env:
- PYTHON_VERSION=3.6
matrix:
- TASK=regular
- TASK=mpi
- TASK=mpi PYTHON_VERSION=2.7
- TASK=pylint
- TASK=if-else
- TASK=sdist
- TASK=bdist
- TASK=sdist PYTHON_VERSION=3.4
- TASK=bdist PYTHON_VERSION=3.5
- TASK=gpu METHOD=source
- TASK=gpu METHOD=pip
- TASK=regular PYTHON_VERSION=2.7
- TASK=regular PYTHON_VERSION=3.4
- TASK=regular PYTHON_VERSION=3.5

matrix:
exclude:
- os: osx
env: TASK=gpu METHOD=source
- os: osx
env: TASK=gpu METHOD=pip
- os: osx
env: TASK=if-else
- os: osx
env: TASK=pylint

Expand Down
4 changes: 4 additions & 0 deletions .travis/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 && 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
5 changes: 3 additions & 2 deletions examples/python-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ Examples include:
- Eval during training
- Early stopping
- 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)
- Basic train and predict with sklearn interface
- Feature importances with sklearn interface
- [advanced_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/advanced_example.py)
- Set feature names
- Directly use categorical features without one-hot encoding
- Dump model to json format
- Get feature importances
- Get feature names
- Load model to predict
- Dump and load model with pickle
- Load model file to continue training
Expand Down
14 changes: 14 additions & 0 deletions examples/python-guide/advanced_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
# pylint: disable = invalid-name, C0111
import json
import lightgbm as lgb
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -63,6 +64,19 @@
# save model to file
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
print('Load model to predict')
bst = lgb.Booster(model_file='model.txt')
Expand Down
9 changes: 0 additions & 9 deletions examples/python-guide/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,8 @@
# save model to file
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...')
# 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)

print('Feature names:', gbm.feature_name())

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

0 comments on commit 350b461

Please sign in to comment.