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
8f17b30c
Commit
8f17b30c
authored
Aug 19, 2018
by
Nikita Titov
Committed by
Tsukasa OMOTO
Aug 19, 2018
Browse files
[python] use None for best_iter and -1 for all iters (#1575)
* refined num_iteration argument in python * hotfix
parent
c78f26ed
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
26 deletions
+32
-26
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+23
-19
python-package/lightgbm/sklearn.py
python-package/lightgbm/sklearn.py
+9
-7
No files found.
python-package/lightgbm/basic.py
View file @
8f17b30c
...
...
@@ -1421,7 +1421,7 @@ class Booster(object):
return
self
.
__deepcopy__
(
None
)
def
__deepcopy__
(
self
,
_
):
model_str
=
self
.
_save_model_to_string
()
model_str
=
self
.
_save_model_to_string
(
num_iteration
=-
1
)
booster
=
Booster
({
'model_str'
:
model_str
})
booster
.
pandas_categorical
=
self
.
pandas_categorical
return
booster
...
...
@@ -1432,7 +1432,7 @@ class Booster(object):
this
.
pop
(
'train_set'
,
None
)
this
.
pop
(
'valid_sets'
,
None
)
if
handle
is
not
None
:
this
[
"handle"
]
=
self
.
_save_model_to_string
()
this
[
"handle"
]
=
self
.
_save_model_to_string
(
num_iteration
=-
1
)
return
this
def
__setstate__
(
self
,
state
):
...
...
@@ -1710,18 +1710,19 @@ class Booster(object):
return
[
item
for
i
in
range_
(
1
,
self
.
__num_dataset
)
for
item
in
self
.
__inner_eval
(
self
.
name_valid_sets
[
i
-
1
],
i
,
feval
)]
def
save_model
(
self
,
filename
,
num_iteration
=
-
1
):
def
save_model
(
self
,
filename
,
num_iteration
=
None
):
"""Save Booster to file.
Parameters
----------
filename : string
Filename to save Booster.
num_iteration: int, optional (default=-1)
Index of the iteration that should to saved.
If <0, the best iteration (if exists) is saved.
num_iteration : int or None, optional (default=None)
Index of the iteration that should be saved.
If None, if the best iteration exists, it is saved; otherwise, all iterations are saved.
If <= 0, all iterations are saved.
"""
if
num_iteration
<=
0
:
if
num_iteration
is
None
:
num_iteration
=
self
.
best_iteration
_safe_call
(
_LIB
.
LGBM_BoosterSaveModel
(
self
.
handle
,
...
...
@@ -1748,9 +1749,9 @@ class Booster(object):
print
(
'Finished loading model, total used %d iterations'
%
(
int
(
out_num_iterations
.
value
)))
self
.
__num_class
=
out_num_class
.
value
def
_save_model_to_string
(
self
,
num_iteration
=
-
1
):
def
_save_model_to_string
(
self
,
num_iteration
=
None
):
"""[Private] Save model to string"""
if
num_iteration
<=
0
:
if
num_iteration
is
None
:
num_iteration
=
self
.
best_iteration
buffer_len
=
1
<<
20
tmp_out_len
=
ctypes
.
c_int64
(
0
)
...
...
@@ -1775,21 +1776,22 @@ class Booster(object):
ptr_string_buffer
))
return
string_buffer
.
value
.
decode
()
def
dump_model
(
self
,
num_iteration
=
-
1
):
def
dump_model
(
self
,
num_iteration
=
None
):
"""Dump Booster to json format.
Parameters
----------
num_iteration: int, optional (default=-1)
Index of the iteration that should to dumped.
If <0, the best iteration (if exists) is dumped.
num_iteration : int or None, optional (default=None)
Index of the iteration that should be dumped.
If None, if the best iteration exists, it is dumped; otherwise, all iterations are dumped.
If <= 0, all iterations are dumped.
Returns
-------
json_repr : dict
Json format of Booster.
"""
if
num_iteration
<=
0
:
if
num_iteration
is
None
:
num_iteration
=
self
.
best_iteration
buffer_len
=
1
<<
20
tmp_out_len
=
ctypes
.
c_int64
(
0
)
...
...
@@ -1814,7 +1816,7 @@ class Booster(object):
ptr_string_buffer
))
return
json
.
loads
(
string_buffer
.
value
.
decode
())
def
predict
(
self
,
data
,
num_iteration
=
-
1
,
raw_score
=
False
,
pred_leaf
=
False
,
pred_contrib
=
False
,
def
predict
(
self
,
data
,
num_iteration
=
None
,
raw_score
=
False
,
pred_leaf
=
False
,
pred_contrib
=
False
,
data_has_header
=
False
,
is_reshape
=
True
,
pred_parameter
=
None
,
**
kwargs
):
"""Make a prediction.
...
...
@@ -1823,9 +1825,11 @@ class Booster(object):
data : string, numpy array or scipy.sparse
Data source for prediction.
If string, it represents the path to txt file.
num_iteration : int, optional (default=-1)
Iteration used for prediction.
If <0, the best iteration (if exists) is used for prediction.
num_iteration : int or None, optional (default=None)
Limit number of iterations in the prediction.
If None, if the best iteration exists, it is used; otherwise, all iterations are used.
If <= 0, all iterations are used (no limits).
raw_score : bool, optional (default=False)
Whether to predict raw scores.
pred_leaf : bool, optional (default=False)
...
...
@@ -1854,7 +1858,7 @@ class Booster(object):
else
:
pred_parameter
=
kwargs
predictor
=
self
.
_to_predictor
(
pred_parameter
)
if
num_iteration
<=
0
:
if
num_iteration
is
None
:
num_iteration
=
self
.
best_iteration
return
predictor
.
predict
(
data
,
num_iteration
,
raw_score
,
pred_leaf
,
pred_contrib
,
data_has_header
,
is_reshape
)
...
...
python-package/lightgbm/sklearn.py
View file @
8f17b30c
...
...
@@ -492,7 +492,7 @@ class LGBMModel(_LGBMModelBase):
del
train_set
,
valid_sets
return
self
def
predict
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
-
1
,
def
predict
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
None
,
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
"""Return the predicted value for each sample.
...
...
@@ -502,9 +502,10 @@ class LGBMModel(_LGBMModelBase):
Input features matrix.
raw_score : bool, optional (default=False)
Whether to predict raw scores.
num_iteration : int, optional (default=
-1
)
num_iteration : int
or None
, optional (default=
None
)
Limit number of iterations in the prediction.
If <= 0, uses all trees (no limits).
If None, if the best iteration exists, it is used; otherwise, all trees are used.
If <= 0, all trees are used (no limits).
pred_leaf : bool, optional (default=False)
Whether to predict leaf index.
pred_contrib : bool, optional (default=False)
...
...
@@ -708,7 +709,7 @@ class LGBMClassifier(LGBMModel, _LGBMClassifierBase):
+
'eval_metric : string, list of strings, callable or None, optional (default="logloss")
\n
'
+
_base_doc
[
_base_doc
.
find
(
' If string, it should be a built-in evaluation metric to use.'
):])
def
predict
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
-
1
,
def
predict
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
None
,
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
result
=
self
.
predict_proba
(
X
,
raw_score
,
num_iteration
,
pred_leaf
,
pred_contrib
,
**
kwargs
)
...
...
@@ -718,7 +719,7 @@ class LGBMClassifier(LGBMModel, _LGBMClassifierBase):
class_index
=
np
.
argmax
(
result
,
axis
=
1
)
return
self
.
_le
.
inverse_transform
(
class_index
)
def
predict_proba
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
-
1
,
def
predict_proba
(
self
,
X
,
raw_score
=
False
,
num_iteration
=
None
,
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
"""Return the predicted probability for each class for each sample.
...
...
@@ -728,9 +729,10 @@ class LGBMClassifier(LGBMModel, _LGBMClassifierBase):
Input features matrix.
raw_score : bool, optional (default=False)
Whether to predict raw scores.
num_iteration : int, optional (default=
-1
)
num_iteration : int
or None
, optional (default=
None
)
Limit number of iterations in the prediction.
If <= 0, uses all trees (no limits).
If None, if the best iteration exists, it is used; otherwise, all trees are used.
If <= 0, all trees are used (no limits).
pred_leaf : bool, optional (default=False)
Whether to predict leaf index.
pred_contrib : bool, optional (default=False)
...
...
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