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
79463dfb
Unverified
Commit
79463dfb
authored
Sep 10, 2021
by
Nikita Titov
Committed by
GitHub
Sep 09, 2021
Browse files
[python] [sklearn] respect `eval_at` aliases in keyword arguments (#4599)
parent
c8490075
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
4 deletions
+20
-4
python-package/lightgbm/sklearn.py
python-package/lightgbm/sklearn.py
+7
-4
tests/python_package_test/test_sklearn.py
tests/python_package_test/test_sklearn.py
+13
-0
No files found.
python-package/lightgbm/sklearn.py
View file @
79463dfb
...
...
@@ -26,7 +26,7 @@ class _ObjectiveFunctionWrapper:
Parameters
----------
func : callable
Expects a callable with signature ``func(y_true, y_pred)`` or ``func(y_true, y_pred, group)
Expects a callable with signature ``func(y_true, y_pred)`` or ``func(y_true, y_pred, group)
``
and returns (grad, hess):
y_true : array-like of shape = [n_samples]
...
...
@@ -611,9 +611,12 @@ class LGBMModel(_LGBMModelBase):
params
.
pop
(
alias
,
None
)
params
[
'num_class'
]
=
self
.
_n_classes
if
hasattr
(
self
,
'_eval_at'
):
eval_at
=
self
.
_eval_at
for
alias
in
_ConfigAliases
.
get
(
'eval_at'
):
params
.
pop
(
alias
,
None
)
params
[
'eval_at'
]
=
self
.
_eval_at
if
alias
in
params
:
_log_warning
(
f
"Found '
{
alias
}
' in params. Will use it instead of 'eval_at' argument"
)
eval_at
=
params
.
pop
(
alias
)
params
[
'eval_at'
]
=
eval_at
params
[
'objective'
]
=
self
.
_objective
if
self
.
_fobj
:
params
[
'objective'
]
=
'None'
# objective = nullptr for unknown objective
...
...
@@ -752,7 +755,7 @@ class LGBMModel(_LGBMModelBase):
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
"""Docstring is set after definition, using a template."""
if
self
.
_n_features
is
None
:
raise
LGBMNotFittedError
(
"Estimator not fitted, call
`
fit
`
before exploiting the model."
)
raise
LGBMNotFittedError
(
"Estimator not fitted, call fit before exploiting the model."
)
if
not
isinstance
(
X
,
(
pd_DataFrame
,
dt_DataTable
)):
X
=
_LGBMCheckArray
(
X
,
accept_sparse
=
True
,
force_all_finite
=
False
)
n_features
=
X
.
shape
[
1
]
...
...
tests/python_package_test/test_sklearn.py
View file @
79463dfb
...
...
@@ -143,6 +143,19 @@ def test_xendcg():
assert
gbm
.
best_score_
[
'valid_0'
][
'ndcg@3'
]
>
0.6253
def
test_eval_at_aliases
():
rank_example_dir
=
Path
(
__file__
).
absolute
().
parents
[
2
]
/
'examples'
/
'lambdarank'
X_train
,
y_train
=
load_svmlight_file
(
str
(
rank_example_dir
/
'rank.train'
))
X_test
,
y_test
=
load_svmlight_file
(
str
(
rank_example_dir
/
'rank.test'
))
q_train
=
np
.
loadtxt
(
str
(
rank_example_dir
/
'rank.train.query'
))
q_test
=
np
.
loadtxt
(
str
(
rank_example_dir
/
'rank.test.query'
))
for
alias
in
(
'eval_at'
,
'ndcg_eval_at'
,
'ndcg_at'
,
'map_eval_at'
,
'map_at'
):
gbm
=
lgb
.
LGBMRanker
(
n_estimators
=
5
,
**
{
alias
:
[
1
,
2
,
3
,
9
]})
with
pytest
.
warns
(
UserWarning
,
match
=
f
"Found '
{
alias
}
' in params. Will use it instead of 'eval_at' argument"
):
gbm
.
fit
(
X_train
,
y_train
,
group
=
q_train
,
eval_set
=
[(
X_test
,
y_test
)],
eval_group
=
[
q_test
])
assert
list
(
gbm
.
evals_result_
[
'valid_0'
].
keys
())
==
[
'ndcg@1'
,
'ndcg@2'
,
'ndcg@3'
,
'ndcg@9'
]
def
test_regression_with_custom_objective
():
X
,
y
=
load_boston
(
return_X_y
=
True
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
...
...
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