Skip to content

Commit

Permalink
Merge pull request dmlc#818 from webgeist/master
Browse files Browse the repository at this point in the history
Add feature_importances_ property for XGBClassifier
  • Loading branch information
tqchen committed Feb 16, 2016
2 parents ba4ec55 + 31c0408 commit 2baea12
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ def fit(self, X, y, sample_weight=None, eval_set=None, eval_metric=None,
else:
evals = ()

self._features_count = X.shape[1]

self._le = LabelEncoder().fit(y)
training_labels = self._le.transform(y)

Expand Down Expand Up @@ -414,6 +416,22 @@ def evals_result(self):

return evals_result

@property
def feature_importances_(self):
"""
Returns
-------
feature_importances_ : array of shape = [n_features]
"""
fs = self.booster().get_fscore()
keys = [int(k.replace('f', '')) for k in fs.keys()]
fs_dict = dict(zip(keys, fs.values()))
all_features_dict = dict.fromkeys(range(0, self._features_count), 0)
all_features_dict.update(fs_dict)
return np.array(all_features_dict.values())


class XGBRegressor(XGBModel, XGBRegressorBase):
# pylint: disable=missing-docstring
__doc__ = """Implementation of the scikit-learn API for XGBoost regression.
Expand Down

0 comments on commit 2baea12

Please sign in to comment.