Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG + 1] Fix _get_importances. #7487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions sklearn/feature_selection/from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@

def _get_feature_importances(estimator):
"""Retrieve or aggregate feature importances from estimator"""
if hasattr(estimator, "feature_importances_"):
importances = estimator.feature_importances_
importances = getattr(estimator, "feature_importances_", None)

elif hasattr(estimator, "coef_"):
if importances is None and hasattr(estimator, "coef_"):
if estimator.coef_.ndim == 1:
importances = np.abs(estimator.coef_)

else:
importances = np.sum(np.abs(estimator.coef_), axis=0)

else:
elif importances is None:
raise ValueError(
"The underlying estimator %s has no `coef_` or "
"`feature_importances_` attribute. Either pass a fitted estimator"
Expand Down
4 changes: 2 additions & 2 deletions sklearn/feature_selection/rfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def _fit(self, X, y, step_score=None):
# Get coefs
if hasattr(estimator, 'coef_'):
coefs = estimator.coef_
elif hasattr(estimator, 'feature_importances_'):
coefs = estimator.feature_importances_
else:
coefs = getattr(estimator, 'feature_importances_', None)
if coefs is None:
raise RuntimeError('The classifier does not expose '
'"coef_" or "feature_importances_" '
'attributes')
Expand Down