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:
- 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:
......@@ -34,6 +31,8 @@ matrix:
env: TASK=gpu METHOD=source
- os: osx
env: TASK=gpu METHOD=pip
- os: osx
env: TASK=if-else
- os: osx
env: TASK=pylint
......
......@@ -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
......@@ -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
......
# coding: utf-8
# pylint: disable = invalid-name, C0111
import json
import lightgbm as lgb
import pandas as pd
import numpy as np
......@@ -63,6 +64,19 @@ print('7th feature name is:', repr(lgb_train.feature_name[6]))
# 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')
......
......@@ -46,17 +46,8 @@ print('Save model...')
# 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()))
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