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
5b5b9823
Unverified
Commit
5b5b9823
authored
Apr 13, 2019
by
Nikita Titov
Committed by
GitHub
Apr 13, 2019
Browse files
[python] make possibility to create Booster from string official (#2098)
parent
0a4a7a86
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
7 deletions
+10
-7
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+9
-6
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+1
-1
No files found.
python-package/lightgbm/basic.py
View file @
5b5b9823
...
@@ -397,7 +397,7 @@ class _InnerPredictor(object):
...
@@ -397,7 +397,7 @@ class _InnerPredictor(object):
self
.
num_total_iteration
=
out_num_iterations
.
value
self
.
num_total_iteration
=
out_num_iterations
.
value
self
.
pandas_categorical
=
None
self
.
pandas_categorical
=
None
else
:
else
:
raise
TypeError
(
'Need
M
odel
file or
B
ooster
handle to create a predictor'
)
raise
TypeError
(
'Need
m
odel
_
file or
b
ooster
_
handle to create a predictor'
)
pred_parameter
=
{}
if
pred_parameter
is
None
else
pred_parameter
pred_parameter
=
{}
if
pred_parameter
is
None
else
pred_parameter
self
.
pred_parameter
=
param_dict_to_str
(
pred_parameter
)
self
.
pred_parameter
=
param_dict_to_str
(
pred_parameter
)
...
@@ -1578,7 +1578,7 @@ class Dataset(object):
...
@@ -1578,7 +1578,7 @@ class Dataset(object):
class
Booster
(
object
):
class
Booster
(
object
):
"""Booster in LightGBM."""
"""Booster in LightGBM."""
def
__init__
(
self
,
params
=
None
,
train_set
=
None
,
model_file
=
None
,
silent
=
False
):
def
__init__
(
self
,
params
=
None
,
train_set
=
None
,
model_file
=
None
,
model_str
=
None
,
silent
=
False
):
"""Initialize the Booster.
"""Initialize the Booster.
Parameters
Parameters
...
@@ -1589,6 +1589,8 @@ class Booster(object):
...
@@ -1589,6 +1589,8 @@ class Booster(object):
Training dataset.
Training dataset.
model_file : string or None, optional (default=None)
model_file : string or None, optional (default=None)
Path to the model file.
Path to the model file.
model_str : string or None, optional (default=None)
Model will be loaded from this string.
silent : bool, optional (default=False)
silent : bool, optional (default=False)
Whether to print messages during construction.
Whether to print messages during construction.
"""
"""
...
@@ -1666,10 +1668,11 @@ class Booster(object):
...
@@ -1666,10 +1668,11 @@ class Booster(object):
ctypes
.
byref
(
out_num_class
)))
ctypes
.
byref
(
out_num_class
)))
self
.
__num_class
=
out_num_class
.
value
self
.
__num_class
=
out_num_class
.
value
self
.
pandas_categorical
=
_load_pandas_categorical
(
file_name
=
model_file
)
self
.
pandas_categorical
=
_load_pandas_categorical
(
file_name
=
model_file
)
elif
'
model_str
'
i
n
params
:
elif
model_str
i
s
not
None
:
self
.
model_from_string
(
params
[
'
model_str
'
],
False
)
self
.
model_from_string
(
model_str
,
not
silent
)
else
:
else
:
raise
TypeError
(
'Need at least one training dataset or model file to create booster instance'
)
raise
TypeError
(
'Need at least one training dataset or model file or model string '
'to create Booster instance'
)
self
.
params
=
params
self
.
params
=
params
def
__del__
(
self
):
def
__del__
(
self
):
...
@@ -1689,7 +1692,7 @@ class Booster(object):
...
@@ -1689,7 +1692,7 @@ class Booster(object):
def
__deepcopy__
(
self
,
_
):
def
__deepcopy__
(
self
,
_
):
model_str
=
self
.
model_to_string
(
num_iteration
=-
1
)
model_str
=
self
.
model_to_string
(
num_iteration
=-
1
)
booster
=
Booster
(
{
'
model_str
'
:
model_str
}
)
booster
=
Booster
(
model_str
=
model_str
)
return
booster
return
booster
def
__getstate__
(
self
):
def
__getstate__
(
self
):
...
...
tests/python_package_test/test_engine.py
View file @
5b5b9823
...
@@ -583,7 +583,7 @@ class TestEngine(unittest.TestCase):
...
@@ -583,7 +583,7 @@ class TestEngine(unittest.TestCase):
model_str
=
gbm4
.
model_to_string
()
model_str
=
gbm4
.
model_to_string
()
gbm4
.
model_from_string
(
model_str
,
False
)
gbm4
.
model_from_string
(
model_str
,
False
)
pred5
=
gbm4
.
predict
(
X_test
)
pred5
=
gbm4
.
predict
(
X_test
)
gbm5
=
lgb
.
Booster
(
{
'
model_str
'
:
model_str
}
)
gbm5
=
lgb
.
Booster
(
model_str
=
model_str
)
pred6
=
gbm5
.
predict
(
X_test
)
pred6
=
gbm5
.
predict
(
X_test
)
np
.
testing
.
assert_almost_equal
(
pred0
,
pred1
)
np
.
testing
.
assert_almost_equal
(
pred0
,
pred1
)
np
.
testing
.
assert_almost_equal
(
pred0
,
pred2
)
np
.
testing
.
assert_almost_equal
(
pred0
,
pred2
)
...
...
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