Skip to content
Snippets Groups Projects
Commit 3a106fc7 authored by fukatani's avatar fukatani Committed by Nelle Varoquaux
Browse files

Fix _get_importances. (#7487)

Not to use hasattr.
parent 5fcc7e5c
No related branches found
No related tags found
No related merge requests found
...@@ -14,17 +14,16 @@ from ..exceptions import NotFittedError ...@@ -14,17 +14,16 @@ from ..exceptions import NotFittedError
def _get_feature_importances(estimator): def _get_feature_importances(estimator):
"""Retrieve or aggregate feature importances from estimator""" """Retrieve or aggregate feature importances from estimator"""
if hasattr(estimator, "feature_importances_"): importances = getattr(estimator, "feature_importances_", None)
importances = estimator.feature_importances_
elif hasattr(estimator, "coef_"): if importances is None and hasattr(estimator, "coef_"):
if estimator.coef_.ndim == 1: if estimator.coef_.ndim == 1:
importances = np.abs(estimator.coef_) importances = np.abs(estimator.coef_)
else: else:
importances = np.sum(np.abs(estimator.coef_), axis=0) importances = np.sum(np.abs(estimator.coef_), axis=0)
else: elif importances is None:
raise ValueError( raise ValueError(
"The underlying estimator %s has no `coef_` or " "The underlying estimator %s has no `coef_` or "
"`feature_importances_` attribute. Either pass a fitted estimator" "`feature_importances_` attribute. Either pass a fitted estimator"
......
...@@ -171,9 +171,9 @@ class RFE(BaseEstimator, MetaEstimatorMixin, SelectorMixin): ...@@ -171,9 +171,9 @@ class RFE(BaseEstimator, MetaEstimatorMixin, SelectorMixin):
# Get coefs # Get coefs
if hasattr(estimator, 'coef_'): if hasattr(estimator, 'coef_'):
coefs = estimator.coef_ coefs = estimator.coef_
elif hasattr(estimator, 'feature_importances_'):
coefs = estimator.feature_importances_
else: else:
coefs = getattr(estimator, 'feature_importances_', None)
if coefs is None:
raise RuntimeError('The classifier does not expose ' raise RuntimeError('The classifier does not expose '
'"coef_" or "feature_importances_" ' '"coef_" or "feature_importances_" '
'attributes') 'attributes')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment