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
f3987f37
Unverified
Commit
f3987f37
authored
Oct 10, 2021
by
Nikita Titov
Committed by
GitHub
Oct 10, 2021
Browse files
[python][sklearn] use `__sklearn_is_fitted__()` in all estimator fitness checks (#4654)
parent
92473592
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
12 deletions
+12
-12
python-package/lightgbm/sklearn.py
python-package/lightgbm/sklearn.py
+12
-12
No files found.
python-package/lightgbm/sklearn.py
View file @
f3987f37
...
@@ -791,7 +791,7 @@ class LGBMModel(_LGBMModelBase):
...
@@ -791,7 +791,7 @@ class LGBMModel(_LGBMModelBase):
def
predict
(
self
,
X
,
raw_score
=
False
,
start_iteration
=
0
,
num_iteration
=
None
,
def
predict
(
self
,
X
,
raw_score
=
False
,
start_iteration
=
0
,
num_iteration
=
None
,
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
pred_leaf
=
False
,
pred_contrib
=
False
,
**
kwargs
):
"""Docstring is set after definition, using a template."""
"""Docstring is set after definition, using a template."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
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
)):
if
not
isinstance
(
X
,
(
pd_DataFrame
,
dt_DataTable
)):
X
=
_LGBMCheckArray
(
X
,
accept_sparse
=
True
,
force_all_finite
=
False
)
X
=
_LGBMCheckArray
(
X
,
accept_sparse
=
True
,
force_all_finite
=
False
)
...
@@ -815,49 +815,49 @@ class LGBMModel(_LGBMModelBase):
...
@@ -815,49 +815,49 @@ class LGBMModel(_LGBMModelBase):
@
property
@
property
def
n_features_
(
self
):
def
n_features_
(
self
):
""":obj:`int`: The number of features of fitted model."""
""":obj:`int`: The number of features of fitted model."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No n_features found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No n_features found. Need to call fit beforehand.'
)
return
self
.
_n_features
return
self
.
_n_features
@
property
@
property
def
n_features_in_
(
self
):
def
n_features_in_
(
self
):
""":obj:`int`: The number of features of fitted model."""
""":obj:`int`: The number of features of fitted model."""
if
self
.
_
n_features_in
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No n_features_in found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No n_features_in found. Need to call fit beforehand.'
)
return
self
.
_n_features_in
return
self
.
_n_features_in
@
property
@
property
def
best_score_
(
self
):
def
best_score_
(
self
):
""":obj:`dict`: The best score of fitted model."""
""":obj:`dict`: The best score of fitted model."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No best_score found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No best_score found. Need to call fit beforehand.'
)
return
self
.
_best_score
return
self
.
_best_score
@
property
@
property
def
best_iteration_
(
self
):
def
best_iteration_
(
self
):
""":obj:`int` or :obj:`None`: The best iteration of fitted model if ``early_stopping()`` callback has been specified."""
""":obj:`int` or :obj:`None`: The best iteration of fitted model if ``early_stopping()`` callback has been specified."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No best_iteration found. Need to call fit with early_stopping callback beforehand.'
)
raise
LGBMNotFittedError
(
'No best_iteration found. Need to call fit with early_stopping callback beforehand.'
)
return
self
.
_best_iteration
return
self
.
_best_iteration
@
property
@
property
def
objective_
(
self
):
def
objective_
(
self
):
""":obj:`str` or :obj:`callable`: The concrete objective used while fitting this model."""
""":obj:`str` or :obj:`callable`: The concrete objective used while fitting this model."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No objective found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No objective found. Need to call fit beforehand.'
)
return
self
.
_objective
return
self
.
_objective
@
property
@
property
def
booster_
(
self
):
def
booster_
(
self
):
"""Booster: The underlying Booster of this model."""
"""Booster: The underlying Booster of this model."""
if
self
.
_
Booster
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No booster found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No booster found. Need to call fit beforehand.'
)
return
self
.
_Booster
return
self
.
_Booster
@
property
@
property
def
evals_result_
(
self
):
def
evals_result_
(
self
):
""":obj:`dict` or :obj:`None`: The evaluation results if validation sets have been specified."""
""":obj:`dict` or :obj:`None`: The evaluation results if validation sets have been specified."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No results found. Need to call fit with eval_set beforehand.'
)
raise
LGBMNotFittedError
(
'No results found. Need to call fit with eval_set beforehand.'
)
return
self
.
_evals_result
return
self
.
_evals_result
...
@@ -870,14 +870,14 @@ class LGBMModel(_LGBMModelBase):
...
@@ -870,14 +870,14 @@ class LGBMModel(_LGBMModelBase):
``importance_type`` attribute is passed to the function
``importance_type`` attribute is passed to the function
to configure the type of importance values to be extracted.
to configure the type of importance values to be extracted.
"""
"""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No feature_importances found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No feature_importances found. Need to call fit beforehand.'
)
return
self
.
_Booster
.
feature_importance
(
importance_type
=
self
.
importance_type
)
return
self
.
_Booster
.
feature_importance
(
importance_type
=
self
.
importance_type
)
@
property
@
property
def
feature_name_
(
self
):
def
feature_name_
(
self
):
""":obj:`array` of shape = [n_features]: The names of features."""
""":obj:`array` of shape = [n_features]: The names of features."""
if
self
.
_
n_features
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No feature_name found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No feature_name found. Need to call fit beforehand.'
)
return
self
.
_Booster
.
feature_name
()
return
self
.
_Booster
.
feature_name
()
...
@@ -1017,14 +1017,14 @@ class LGBMClassifier(_LGBMClassifierBase, LGBMModel):
...
@@ -1017,14 +1017,14 @@ class LGBMClassifier(_LGBMClassifierBase, LGBMModel):
@
property
@
property
def
classes_
(
self
):
def
classes_
(
self
):
""":obj:`array` of shape = [n_classes]: The class label array."""
""":obj:`array` of shape = [n_classes]: The class label array."""
if
self
.
_
classes
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No classes found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No classes found. Need to call fit beforehand.'
)
return
self
.
_classes
return
self
.
_classes
@
property
@
property
def
n_classes_
(
self
):
def
n_classes_
(
self
):
""":obj:`int`: The number of classes."""
""":obj:`int`: The number of classes."""
if
self
.
_
n_classes
is
None
:
if
not
self
.
_
_sklearn_is_fitted__
()
:
raise
LGBMNotFittedError
(
'No classes found. Need to call fit beforehand.'
)
raise
LGBMNotFittedError
(
'No classes found. Need to call fit beforehand.'
)
return
self
.
_n_classes
return
self
.
_n_classes
...
...
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