From dd700f44dfe2fedee2e877b829eeee0dfd8b9eb4 Mon Sep 17 00:00:00 2001 From: Adrin Jalali Date: Sun, 12 Aug 2018 09:20:14 +0200 Subject: [PATCH] MNT hasattr->getattr on coef_ (#11698) --- sklearn/feature_selection/from_model.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sklearn/feature_selection/from_model.py b/sklearn/feature_selection/from_model.py index f6f9c2776b7..3e2efdbeb1e 100644 --- a/sklearn/feature_selection/from_model.py +++ b/sklearn/feature_selection/from_model.py @@ -16,12 +16,13 @@ def _get_feature_importances(estimator, norm_order=1): """Retrieve or aggregate feature importances from estimator""" importances = getattr(estimator, "feature_importances_", None) - if importances is None and hasattr(estimator, "coef_"): + coef_ = getattr(estimator, "coef_", None) + if importances is None and coef_ is not None: if estimator.coef_.ndim == 1: - importances = np.abs(estimator.coef_) + importances = np.abs(coef_) else: - importances = np.linalg.norm(estimator.coef_, axis=0, + importances = np.linalg.norm(coef_, axis=0, ord=norm_order) elif importances is None: