From e04ab56b57a6e9b3eb0df0c38df8cb0a564e2ab4 Mon Sep 17 00:00:00 2001 From: Philip Hyunsu Cho Date: Fri, 2 Nov 2018 01:44:37 -0700 Subject: [PATCH] Fix #3747: Add coef_ and intercept_ as properties of sklearn wrapper (#3855) * Fix #3747: Add coef_ and intercept_ as properties of sklearn wrapper Scikit-learn expects linear learners to expose `coef_` and `intercept_` as properties. Closes #3747. * Fix lint --- python-package/xgboost/sklearn.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 1f373687e..39bf66703 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -5,6 +5,7 @@ from __future__ import absolute_import import numpy as np import warnings +import json from .core import Booster, DMatrix, XGBoostError from .training import train @@ -512,12 +513,57 @@ class XGBModel(XGBModelBase): feature_importances_ : array of shape ``[n_features]`` """ + if self.booster != 'gbtree': + raise AttributeError('Feature importance is not defined for Booster type {}' + .format(self.booster)) b = self.get_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() + @property + def coef_(self): + """ + Coefficients property + + .. note:: Coefficients are defined only for linear learners + + Coefficients are only defined when the linear model is chosen as base + learner (`booster=gblinear`). It is not defined for other base learner types, such + as tree learners (`booster=gbtree`). + + Returns + ------- + coef_ : array of shape ``[n_features]`` + """ + if self.booster != 'gblinear': + raise AttributeError('Coefficients are not defined for Booster type {}' + .format(self.booster)) + b = self.get_booster() + return json.loads(b.get_dump(dump_format='json')[0])['weight'] + + @property + def intercept_(self): + """ + Intercept (bias) property + + .. note:: Intercept is defined only for linear learners + + Intercept (bias) is only defined when the linear model is chosen as base + learner (`booster=gblinear`). It is not defined for other base learner types, such + as tree learners (`booster=gbtree`). + + Returns + ------- + intercept_ : array of shape ``[n_features]`` + """ + if self.booster != 'gblinear': + raise AttributeError('Intercept (bias) is not defined for Booster type {}' + .format(self.booster)) + b = self.get_booster() + return json.loads(b.get_dump(dump_format='json')[0])['bias'] + class XGBClassifier(XGBModel, XGBClassifierBase): # pylint: disable=missing-docstring,too-many-arguments,invalid-name