Unverified Commit f3987f37 authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

[python][sklearn] use `__sklearn_is_fitted__()` in all estimator fitness checks (#4654)

parent 92473592
...@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment