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
5b72e4ab
Unverified
Commit
5b72e4ab
authored
Apr 25, 2023
by
James Lamb
Committed by
GitHub
Apr 25, 2023
Browse files
[ci] add tests on cv() with an init_model (#5851)
parent
611ce0d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+61
-0
No files found.
tests/python_package_test/test_engine.py
View file @
5b72e4ab
...
@@ -1075,6 +1075,67 @@ def test_cv():
...
@@ -1075,6 +1075,67 @@ def test_cv():
np
.
testing
.
assert_allclose
(
cv_res_lambda
[
'valid ndcg@3-mean'
],
cv_res_lambda_obj
[
'valid ndcg@3-mean'
])
np
.
testing
.
assert_allclose
(
cv_res_lambda
[
'valid ndcg@3-mean'
],
cv_res_lambda_obj
[
'valid ndcg@3-mean'
])
def
test_cv_works_with_init_model
(
tmp_path
):
X
,
y
=
make_synthetic_regression
()
params
=
{
'objective'
:
'regression'
,
'verbose'
:
-
1
}
num_train_rounds
=
2
lgb_train
=
lgb
.
Dataset
(
X
,
y
,
free_raw_data
=
False
)
bst
=
lgb
.
train
(
params
=
params
,
train_set
=
lgb_train
,
num_boost_round
=
num_train_rounds
)
preds_raw
=
bst
.
predict
(
X
,
raw_score
=
True
)
model_path_txt
=
str
(
tmp_path
/
'lgb.model'
)
bst
.
save_model
(
model_path_txt
)
num_cv_rounds
=
5
cv_kwargs
=
{
"num_boost_round"
:
num_cv_rounds
,
"nfold"
:
3
,
"stratified"
:
False
,
"shuffle"
:
False
,
"seed"
:
708
,
"return_cvbooster"
:
True
,
"params"
:
params
}
# init_model from an in-memory Booster
cv_res
=
lgb
.
cv
(
train_set
=
lgb_train
,
init_model
=
bst
,
**
cv_kwargs
)
cv_bst_w_in_mem_init_model
=
cv_res
[
"cvbooster"
]
assert
cv_bst_w_in_mem_init_model
.
current_iteration
()
==
[
num_train_rounds
+
num_cv_rounds
]
*
3
for
booster
in
cv_bst_w_in_mem_init_model
.
boosters
:
np
.
testing
.
assert_allclose
(
preds_raw
,
booster
.
predict
(
X
,
raw_score
=
True
,
num_iteration
=
num_train_rounds
)
)
# init_model from a text file
cv_res
=
lgb
.
cv
(
train_set
=
lgb_train
,
init_model
=
model_path_txt
,
**
cv_kwargs
)
cv_bst_w_file_init_model
=
cv_res
[
"cvbooster"
]
assert
cv_bst_w_file_init_model
.
current_iteration
()
==
[
num_train_rounds
+
num_cv_rounds
]
*
3
for
booster
in
cv_bst_w_file_init_model
.
boosters
:
np
.
testing
.
assert_allclose
(
preds_raw
,
booster
.
predict
(
X
,
raw_score
=
True
,
num_iteration
=
num_train_rounds
)
)
# predictions should be identical
for
i
in
range
(
3
):
np
.
testing
.
assert_allclose
(
cv_bst_w_in_mem_init_model
.
boosters
[
i
].
predict
(
X
),
cv_bst_w_file_init_model
.
boosters
[
i
].
predict
(
X
)
)
def
test_cvbooster
():
def
test_cvbooster
():
X
,
y
=
load_breast_cancer
(
return_X_y
=
True
)
X
,
y
=
load_breast_cancer
(
return_X_y
=
True
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
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