Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
74ce2cfe
"python-package/vscode:/vscode.git/clone" did not exist on "c04c0917203fbdd1578e4dbf2d166243048adee3"
Commit
74ce2cfe
authored
Mar 14, 2019
by
Nikita Titov
Committed by
Guolin Ke
Mar 14, 2019
Browse files
added examples for multiple custom metrics (#2021)
parent
ffb134cc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
0 deletions
+37
-0
examples/python-guide/advanced_example.py
examples/python-guide/advanced_example.py
+21
-0
examples/python-guide/sklearn_example.py
examples/python-guide/sklearn_example.py
+16
-0
No files found.
examples/python-guide/advanced_example.py
View file @
74ce2cfe
...
...
@@ -163,6 +163,27 @@ gbm = lgb.train(params,
print
(
'Finished 40 - 50 rounds with self-defined objective function and eval metric...'
)
# another self-defined eval metric
# f(preds: array, train_data: Dataset) -> name: string, eval_result: float, is_higher_better: bool
# accuracy
def
accuracy
(
preds
,
train_data
):
labels
=
train_data
.
get_label
()
return
'accuracy'
,
np
.
mean
(
labels
==
(
preds
>
0.5
)),
True
gbm
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
init_model
=
gbm
,
fobj
=
loglikelihood
,
feval
=
lambda
preds
,
train_data
:
[
binary_error
(
preds
,
train_data
),
accuracy
(
preds
,
train_data
)],
valid_sets
=
lgb_eval
)
print
(
'Finished 50 - 60 rounds with self-defined objective function '
'and multiple self-defined eval metrics...'
)
print
(
'Starting a new training job...'
)
...
...
examples/python-guide/sklearn_example.py
View file @
74ce2cfe
...
...
@@ -51,11 +51,27 @@ gbm.fit(X_train, y_train,
eval_metric
=
rmsle
,
early_stopping_rounds
=
5
)
# another self-defined eval metric
# f(y_true: array, y_pred: array) -> name: string, eval_result: float, is_higher_better: bool
# Relative Absolute Error (RAE)
def
rae
(
y_true
,
y_pred
):
return
'RAE'
,
np
.
sum
(
np
.
abs
(
y_pred
-
y_true
))
/
np
.
sum
(
np
.
abs
(
np
.
mean
(
y_true
)
-
y_true
)),
False
print
(
'Starting training with multiple custom eval functions...'
)
# train
gbm
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_test
,
y_test
)],
eval_metric
=
lambda
y_true
,
y_pred
:
[
rmsle
(
y_true
,
y_pred
),
rae
(
y_true
,
y_pred
)],
early_stopping_rounds
=
5
)
print
(
'Starting 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
])
print
(
'The rae of prediction is:'
,
rae
(
y_test
,
y_pred
)[
1
])
# other scikit-learn modules
estimator
=
lgb
.
LGBMRegressor
(
num_leaves
=
31
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment