From dd477ac903eb6f658d6fb2984763c3f8a4516389 Mon Sep 17 00:00:00 2001 From: ccphillippi Date: Thu, 1 Dec 2016 13:17:37 -0500 Subject: [PATCH] Move feature_importances_ to base XGBModel for XGBRegressor access (#1591) --- python-package/xgboost/sklearn.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 5f8ff852e..f874a63bc 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -328,6 +328,20 @@ class XGBModel(XGBModelBase): return evals_result + @property + def feature_importances_(self): + """ + Returns + ------- + feature_importances_ : array of shape = [n_features] + + """ + b = self.booster() + fs = b.get_fscore() + all_features = [fs.get(f, 0.) for f in b.feature_names] + all_features = np.array(all_features, dtype=np.float32) + return all_features / all_features.sum() + class XGBClassifier(XGBModel, XGBClassifierBase): # pylint: disable=missing-docstring,too-many-arguments,invalid-name @@ -518,20 +532,6 @@ class XGBClassifier(XGBModel, XGBClassifierBase): return evals_result - @property - def feature_importances_(self): - """ - Returns - ------- - feature_importances_ : array of shape = [n_features] - - """ - b = self.booster() - fs = b.get_fscore() - all_features = [fs.get(f, 0.) for f in b.feature_names] - all_features = np.array(all_features, dtype=np.float32) - return all_features / all_features.sum() - class XGBRegressor(XGBModel, XGBRegressorBase): # pylint: disable=missing-docstring