From c23783a0d1db3d795befe8e4588f211465b82a3f Mon Sep 17 00:00:00 2001 From: Philip Hyunsu Cho Date: Sun, 7 Oct 2018 14:09:09 -0700 Subject: [PATCH] Add notes to doc (#3765) --- python-package/xgboost/core.py | 20 ++++++++++++++++++++ python-package/xgboost/sklearn.py | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 062627365..30ab6125d 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -875,6 +875,10 @@ class Booster(object): ctypes.byref(self.handle))) self.set_param({'seed': 0}) self.set_param(params or {}) + if (params is not None) and ('booster' in params): + self.booster = params['booster'] + else: + self.booster = 'gbtree' if model_file is not None: self.load_model(model_file) @@ -1379,6 +1383,12 @@ class Booster(object): def get_fscore(self, fmap=''): """Get feature importance of each feature. + .. note:: Feature importance is defined only for tree boosters + + Feature importance is only defined when the decision tree model is chosen as base + learner (`booster=gbtree`). It is not defined for other base learner types, such + as linear learners (`booster=gblinear`). + Parameters ---------- fmap: str (optional) @@ -1397,6 +1407,12 @@ class Booster(object): * 'total_gain': the total gain across all splits the feature is used in. * 'total_cover': the total coverage across all splits the feature is used in. + .. note:: Feature importance is defined only for tree boosters + + Feature importance is only defined when the decision tree model is chosen as base + learner (`booster=gbtree`). It is not defined for other base learner types, such + as linear learners (`booster=gblinear`). + Parameters ---------- fmap: str (optional) @@ -1405,6 +1421,10 @@ class Booster(object): One of the importance types defined above. """ + if self.booster != 'gbtree': + raise ValueError('Feature importance is not defined for Booster type {}' + .format(self.booster)) + allowed_importance_types = ['weight', 'gain', 'cover', 'total_gain', 'total_cover'] if importance_type not in allowed_importance_types: msg = ("importance_type mismatch, got '{}', expected one of " + diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 0b57cbd30..047aa15f2 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -466,6 +466,12 @@ class XGBModel(XGBModelBase): """ Feature importances property + .. note:: Feature importance is defined only for tree boosters + + Feature importance is only defined when the decision tree model is chosen as base + learner (`booster=gbtree`). It is not defined for other base learner types, such + as linear learners (`booster=gblinear`). + Returns ------- feature_importances_ : array of shape ``[n_features]``