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
e31cf04a
Unverified
Commit
e31cf04a
authored
Aug 06, 2024
by
Atanas Dimitrov
Committed by
GitHub
Aug 05, 2024
Browse files
[ci] prevent `categorical.model` and `model.txt` files being left behind after testing (#6590)
parent
3175a912
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
13 deletions
+16
-13
tests/c_api_test/test_.py
tests/c_api_test/test_.py
+4
-3
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+8
-7
tests/python_package_test/test_sklearn.py
tests/python_package_test/test_sklearn.py
+4
-3
No files found.
tests/c_api_test/test_.py
View file @
e31cf04a
...
@@ -175,11 +175,12 @@ def test_dataset():
...
@@ -175,11 +175,12 @@ def test_dataset():
free_dataset
(
train
)
free_dataset
(
train
)
def
test_booster
():
def
test_booster
(
tmp_path
):
binary_example_dir
=
Path
(
__file__
).
absolute
().
parents
[
2
]
/
"examples"
/
"binary_classification"
binary_example_dir
=
Path
(
__file__
).
absolute
().
parents
[
2
]
/
"examples"
/
"binary_classification"
train
=
load_from_mat
(
binary_example_dir
/
"binary.train"
,
None
)
train
=
load_from_mat
(
binary_example_dir
/
"binary.train"
,
None
)
test
=
load_from_mat
(
binary_example_dir
/
"binary.test"
,
train
)
test
=
load_from_mat
(
binary_example_dir
/
"binary.test"
,
train
)
booster
=
ctypes
.
c_void_p
()
booster
=
ctypes
.
c_void_p
()
model_path
=
tmp_path
/
"model.txt"
LIB
.
LGBM_BoosterCreate
(
train
,
c_str
(
"app=binary metric=auc num_leaves=31 verbose=0"
),
ctypes
.
byref
(
booster
))
LIB
.
LGBM_BoosterCreate
(
train
,
c_str
(
"app=binary metric=auc num_leaves=31 verbose=0"
),
ctypes
.
byref
(
booster
))
LIB
.
LGBM_BoosterAddValidData
(
booster
,
test
)
LIB
.
LGBM_BoosterAddValidData
(
booster
,
test
)
is_finished
=
ctypes
.
c_int
(
0
)
is_finished
=
ctypes
.
c_int
(
0
)
...
@@ -192,13 +193,13 @@ def test_booster():
...
@@ -192,13 +193,13 @@ def test_booster():
)
)
if
i
%
10
==
0
:
if
i
%
10
==
0
:
print
(
f
"
{
i
}
iteration test AUC
{
result
[
0
]:.
6
f
}
"
)
print
(
f
"
{
i
}
iteration test AUC
{
result
[
0
]:.
6
f
}
"
)
LIB
.
LGBM_BoosterSaveModel
(
booster
,
ctypes
.
c_int
(
0
),
ctypes
.
c_int
(
-
1
),
ctypes
.
c_int
(
0
),
c_str
(
"
model
.txt"
))
LIB
.
LGBM_BoosterSaveModel
(
booster
,
ctypes
.
c_int
(
0
),
ctypes
.
c_int
(
-
1
),
ctypes
.
c_int
(
0
),
c_str
(
str
(
model
_path
)
))
LIB
.
LGBM_BoosterFree
(
booster
)
LIB
.
LGBM_BoosterFree
(
booster
)
free_dataset
(
train
)
free_dataset
(
train
)
free_dataset
(
test
)
free_dataset
(
test
)
booster2
=
ctypes
.
c_void_p
()
booster2
=
ctypes
.
c_void_p
()
num_total_model
=
ctypes
.
c_int
(
0
)
num_total_model
=
ctypes
.
c_int
(
0
)
LIB
.
LGBM_BoosterCreateFromModelfile
(
c_str
(
"
model
.txt"
),
ctypes
.
byref
(
num_total_model
),
ctypes
.
byref
(
booster2
))
LIB
.
LGBM_BoosterCreateFromModelfile
(
c_str
(
str
(
model
_path
)
),
ctypes
.
byref
(
num_total_model
),
ctypes
.
byref
(
booster2
))
data
=
np
.
loadtxt
(
str
(
binary_example_dir
/
"binary.test"
),
dtype
=
np
.
float64
)
data
=
np
.
loadtxt
(
str
(
binary_example_dir
/
"binary.test"
),
dtype
=
np
.
float64
)
mat
=
data
[:,
1
:]
mat
=
data
[:,
1
:]
preb
=
np
.
empty
(
mat
.
shape
[
0
],
dtype
=
np
.
float64
)
preb
=
np
.
empty
(
mat
.
shape
[
0
],
dtype
=
np
.
float64
)
...
...
tests/python_package_test/test_engine.py
View file @
e31cf04a
...
@@ -1112,15 +1112,15 @@ def test_early_stopping_can_be_triggered_via_custom_callback():
...
@@ -1112,15 +1112,15 @@ def test_early_stopping_can_be_triggered_via_custom_callback():
assert
bst
.
current_iteration
()
==
7
assert
bst
.
current_iteration
()
==
7
def
test_continue_train
():
def
test_continue_train
(
tmp_path
):
X
,
y
=
make_synthetic_regression
()
X
,
y
=
make_synthetic_regression
()
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
)
params
=
{
"objective"
:
"regression"
,
"metric"
:
"l1"
,
"verbose"
:
-
1
}
params
=
{
"objective"
:
"regression"
,
"metric"
:
"l1"
,
"verbose"
:
-
1
}
lgb_train
=
lgb
.
Dataset
(
X_train
,
y_train
,
free_raw_data
=
False
)
lgb_train
=
lgb
.
Dataset
(
X_train
,
y_train
,
free_raw_data
=
False
)
lgb_eval
=
lgb
.
Dataset
(
X_test
,
y_test
,
reference
=
lgb_train
,
free_raw_data
=
False
)
lgb_eval
=
lgb
.
Dataset
(
X_test
,
y_test
,
reference
=
lgb_train
,
free_raw_data
=
False
)
init_gbm
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
20
)
init_gbm
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
20
)
model_
name
=
"model.txt"
model_
path
=
tmp_path
/
"model.txt"
init_gbm
.
save_model
(
model_
name
)
init_gbm
.
save_model
(
model_
path
)
evals_result
=
{}
evals_result
=
{}
gbm
=
lgb
.
train
(
gbm
=
lgb
.
train
(
params
,
params
,
...
@@ -1130,7 +1130,7 @@ def test_continue_train():
...
@@ -1130,7 +1130,7 @@ def test_continue_train():
# test custom eval metrics
# test custom eval metrics
feval
=
(
lambda
p
,
d
:
(
"custom_mae"
,
mean_absolute_error
(
p
,
d
.
get_label
()),
False
)),
feval
=
(
lambda
p
,
d
:
(
"custom_mae"
,
mean_absolute_error
(
p
,
d
.
get_label
()),
False
)),
callbacks
=
[
lgb
.
record_evaluation
(
evals_result
)],
callbacks
=
[
lgb
.
record_evaluation
(
evals_result
)],
init_model
=
"
model
.txt"
,
init_model
=
model
_path
,
)
)
ret
=
mean_absolute_error
(
y_test
,
gbm
.
predict
(
X_test
))
ret
=
mean_absolute_error
(
y_test
,
gbm
.
predict
(
X_test
))
assert
ret
<
13.6
assert
ret
<
13.6
...
@@ -1713,7 +1713,7 @@ def test_all_expected_params_are_written_out_to_model_text(tmp_path):
...
@@ -1713,7 +1713,7 @@ def test_all_expected_params_are_written_out_to_model_text(tmp_path):
# why fixed seed?
# why fixed seed?
# sometimes there is no difference how cols are treated (cat or not cat)
# sometimes there is no difference how cols are treated (cat or not cat)
def
test_pandas_categorical
(
rng_fixed_seed
):
def
test_pandas_categorical
(
rng_fixed_seed
,
tmp_path
):
pd
=
pytest
.
importorskip
(
"pandas"
)
pd
=
pytest
.
importorskip
(
"pandas"
)
X
=
pd
.
DataFrame
(
X
=
pd
.
DataFrame
(
{
{
...
@@ -1756,8 +1756,9 @@ def test_pandas_categorical(rng_fixed_seed):
...
@@ -1756,8 +1756,9 @@ def test_pandas_categorical(rng_fixed_seed):
gbm3
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
])
gbm3
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
10
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
])
pred3
=
gbm3
.
predict
(
X_test
)
pred3
=
gbm3
.
predict
(
X_test
)
assert
lgb_train
.
categorical_feature
==
[
"A"
,
"B"
,
"C"
,
"D"
]
assert
lgb_train
.
categorical_feature
==
[
"A"
,
"B"
,
"C"
,
"D"
]
gbm3
.
save_model
(
"categorical.model"
)
categorical_model_path
=
tmp_path
/
"categorical.model"
gbm4
=
lgb
.
Booster
(
model_file
=
"categorical.model"
)
gbm3
.
save_model
(
categorical_model_path
)
gbm4
=
lgb
.
Booster
(
model_file
=
categorical_model_path
)
pred4
=
gbm4
.
predict
(
X_test
)
pred4
=
gbm4
.
predict
(
X_test
)
model_str
=
gbm4
.
model_to_string
()
model_str
=
gbm4
.
model_to_string
()
gbm4
.
model_from_string
(
model_str
)
gbm4
.
model_from_string
(
model_str
)
...
...
tests/python_package_test/test_sklearn.py
View file @
e31cf04a
...
@@ -558,7 +558,7 @@ def test_feature_importances_type():
...
@@ -558,7 +558,7 @@ def test_feature_importances_type():
# why fixed seed?
# why fixed seed?
# sometimes there is no difference how cols are treated (cat or not cat)
# sometimes there is no difference how cols are treated (cat or not cat)
def
test_pandas_categorical
(
rng_fixed_seed
):
def
test_pandas_categorical
(
rng_fixed_seed
,
tmp_path
):
pd
=
pytest
.
importorskip
(
"pandas"
)
pd
=
pytest
.
importorskip
(
"pandas"
)
X
=
pd
.
DataFrame
(
X
=
pd
.
DataFrame
(
{
{
...
@@ -593,8 +593,9 @@ def test_pandas_categorical(rng_fixed_seed):
...
@@ -593,8 +593,9 @@ def test_pandas_categorical(rng_fixed_seed):
pred2
=
gbm2
.
predict
(
X_test
,
raw_score
=
True
)
pred2
=
gbm2
.
predict
(
X_test
,
raw_score
=
True
)
gbm3
=
lgb
.
sklearn
.
LGBMClassifier
(
n_estimators
=
10
).
fit
(
X
,
y
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
])
gbm3
=
lgb
.
sklearn
.
LGBMClassifier
(
n_estimators
=
10
).
fit
(
X
,
y
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
])
pred3
=
gbm3
.
predict
(
X_test
,
raw_score
=
True
)
pred3
=
gbm3
.
predict
(
X_test
,
raw_score
=
True
)
gbm3
.
booster_
.
save_model
(
"categorical.model"
)
categorical_model_path
=
tmp_path
/
"categorical.model"
gbm4
=
lgb
.
Booster
(
model_file
=
"categorical.model"
)
gbm3
.
booster_
.
save_model
(
categorical_model_path
)
gbm4
=
lgb
.
Booster
(
model_file
=
categorical_model_path
)
pred4
=
gbm4
.
predict
(
X_test
)
pred4
=
gbm4
.
predict
(
X_test
)
gbm5
=
lgb
.
sklearn
.
LGBMClassifier
(
n_estimators
=
10
).
fit
(
X
,
y
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
,
"E"
])
gbm5
=
lgb
.
sklearn
.
LGBMClassifier
(
n_estimators
=
10
).
fit
(
X
,
y
,
categorical_feature
=
[
"A"
,
"B"
,
"C"
,
"D"
,
"E"
])
pred5
=
gbm5
.
predict
(
X_test
,
raw_score
=
True
)
pred5
=
gbm5
.
predict
(
X_test
,
raw_score
=
True
)
...
...
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