Skip to content

Commit

Permalink
[python][examples] Added example of custom feval with sklearn wrapper (
Browse files Browse the repository at this point in the history
…#1503)

* added example about custom feval with sklearn wrapper

* fixed pylint

* added new example in contents

* unified run example commands for Windows and Linux

* added note about comments in examples conf files
  • Loading branch information
StrikerRUS authored and chivee committed Jul 13, 2018
1 parent c647b66 commit ca30afe
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 77 deletions.
2 changes: 2 additions & 0 deletions examples/README.md
Expand Up @@ -3,6 +3,8 @@ Examples

You can learn how to use LightGBM by these examples.

Comments in configuration files might be outdated. Actual information about parameters always can be found [here](https://github.com/Microsoft/LightGBM/blob/master/docs/Parameters.rst).

Machine Learning Challenge Winning Solutions
============================================

Expand Down
24 changes: 6 additions & 18 deletions examples/binary_classification/README.md
Expand Up @@ -5,34 +5,22 @@ Here is an example for LightGBM to run binary classification task.

***You should copy executable file to this folder first.***

Trainin
-------
Training
--------

For Windows, by running following command in this folder:
Run the following command in this folder:

```
lightgbm.exe config=train.conf
```

For Linux, by running following command in this folder:

```
./lightgbm config=train.conf
"./lightgbm" config=train.conf
```

Prediction
----------

You should finish training first.

For Windows, by running following command in this folder:

```
lightgbm.exe config=predict.conf
```

For Linux, by running following command in this folder:
Run the following command in this folder:

```
./lightgbm config=predict.conf
"./lightgbm" config=predict.conf
```
20 changes: 4 additions & 16 deletions examples/lambdarank/README.md
Expand Up @@ -8,31 +8,19 @@ Here is an example for LightGBM to run lambdarank task.
Training
--------

For Windows, by running following command in this folder:
Run the following command in this folder:

```
lightgbm.exe config=train.conf
```

For Linux, by running following command in this folder:

```
./lightgbm config=train.conf
"./lightgbm" config=train.conf
```

Prediction
----------

You should finish training first.

For Windows, by running following command in this folder:

```
lightgbm.exe config=predict.conf
```

For Linux, by running following command in this folder:
Run the following command in this folder:

```
./lightgbm config=predict.conf
"./lightgbm" config=predict.conf
```
20 changes: 4 additions & 16 deletions examples/multiclass_classification/README.md
Expand Up @@ -8,31 +8,19 @@ Here is an example for LightGBM to run multiclass classification task.
Training
--------

For Windows, by running following command in this folder:
Run the following command in this folder:

```
lightgbm.exe config=train.conf
```

For Linux, by running following command in this folder:

```
./lightgbm config=train.conf
"./lightgbm" config=train.conf
```

Prediction
----------

You should finish training first.

For Windows, by running following command in this folder:

```
lightgbm.exe config=predict.conf
```

For Linux, by running following command in this folder:
Run the following command in this folder:

```
./lightgbm config=predict.conf
"./lightgbm" config=predict.conf
```
16 changes: 7 additions & 9 deletions examples/parallel_learning/README.md
Expand Up @@ -3,21 +3,19 @@ Parallel Learning Example

Here is an example for LightGBM to perform parallel learning for 2 machines.

1. Edit mlist.txt, write the ip of these 2 machines that you want to run application on.
1. Edit [mlist.txt](./mlist.txt): write the ip of these 2 machines that you want to run application on.

```
machine1_ip 12400
machine2_ip 12400
```
```
machine1_ip 12400
machine2_ip 12400
```

2. Copy this folder and executable file to these 2 machines that you want to run application on.

3. Run command in this folder on both 2 machines:

For Windows: ```lightgbm.exe config=train.conf```
```"./lightgbm" config=train.conf```

For Linux: ```./lightgbm config=train.conf```

This parallel learning example is based on socket. LightGBM also support parallel learning based on mpi.
This parallel learning example is based on socket. LightGBM also supports parallel learning based on mpi.

For more details about the usage of parallel learning, please refer to [this](https://github.com/Microsoft/LightGBM/blob/master/docs/Parallel-Learning-Guide.rst).
1 change: 1 addition & 0 deletions examples/python-guide/README.md
Expand Up @@ -29,6 +29,7 @@ Examples include:
- Create data for learning with sklearn interface
- Basic train and predict with sklearn interface
- Feature importances with sklearn interface
- Self-defined eval metric with sklearn interface
- Find best parameters for the model with sklearn's GridSearchCV
- [advanced_example.py](https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/advanced_example.py)
- Set feature names
Expand Down
2 changes: 1 addition & 1 deletion examples/python-guide/advanced_example.py
Expand Up @@ -144,7 +144,7 @@ def loglikelood(preds, train_data):


# self-defined eval metric
# f(preds: array, train_data: Dataset) -> name: string, value: array, is_higher_better: bool
# f(preds: array, train_data: Dataset) -> name: string, eval_result: float, is_higher_better: bool
# binary error
def binary_error(preds, train_data):
labels = train_data.get_label()
Expand Down
25 changes: 24 additions & 1 deletion examples/python-guide/sklearn_example.py
@@ -1,7 +1,9 @@
# coding: utf-8
# pylint: disable = invalid-name, C0111
import lightgbm as lgb
import numpy as np
import pandas as pd
import lightgbm as lgb

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV

Expand Down Expand Up @@ -35,6 +37,27 @@
# feature importances
print('Feature importances:', list(gbm.feature_importances_))


# self-defined eval metric
# f(y_true: array, y_pred: array) -> name: string, eval_result: float, is_higher_better: bool
# Root Mean Squared Logarithmic Error (RMSLE)
def rmsle(y_true, y_pred):
return 'RMSLE', np.sqrt(np.mean(np.power(np.log1p(y_pred) - np.log1p(y_true), 2))), False


print('Start training with custom eval function...')
# train
gbm.fit(X_train, y_train,
eval_set=[(X_test, y_test)],
eval_metric=rmsle,
early_stopping_rounds=5)

print('Start predicting...')
# predict
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration_)
# eval
print('The rmsle of prediction is:', rmsle(y_test, y_pred)[1])

# other scikit-learn modules
estimator = lgb.LGBMRegressor(num_leaves=31)

Expand Down
20 changes: 4 additions & 16 deletions examples/regression/README.md
Expand Up @@ -8,31 +8,19 @@ Here is an example for LightGBM to run regression task.
Training
--------

For Windows, by running following command in this folder:
Run the following command in this folder:

```
lightgbm.exe config=train.conf
```

For Linux, by running following command in this folder:

```
./lightgbm config=train.conf
"./lightgbm" config=train.conf
```

Prediction
----------

You should finish training first.

For Windows, by running following command in this folder:

```
lightgbm.exe config=predict.conf
```

For Linux, by running following command in this folder:
Run the following command in this folder:

```
./lightgbm config=predict.conf
"./lightgbm" config=predict.conf
```

0 comments on commit ca30afe

Please sign in to comment.