Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checking booster. #5505

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def get_params(self, deep=True):
np.iinfo(np.int32).max)

def parse_parameter(value):
for t in (int, float):
for t in (int, float, str):
try:
ret = t(value)
return ret
Expand Down Expand Up @@ -678,10 +678,10 @@ def feature_importances_(self):
feature_importances_ : array of shape ``[n_features]``

"""
if getattr(self, 'booster', None) is not None and self.booster not in {
'gbtree', 'dart'}:
raise AttributeError('Feature importance is not defined for Booster type {}'
.format(self.booster))
if self.get_params()['booster'] not in {'gbtree', 'dart'}:
raise AttributeError(
'Feature importance is not defined for Booster type {}'
.format(self.booster))
b = self.get_booster()
score = b.get_score(importance_type=self.importance_type)
all_features = [score.get(f, 0.) for f in b.feature_names]
Expand All @@ -703,11 +703,13 @@ def coef_(self):
-------
coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
"""
if getattr(self, 'booster', None) is not None and self.booster != 'gblinear':
raise AttributeError('Coefficients are not defined for Booster type {}'
.format(self.booster))
if self.get_params()['booster'] != 'gblinear':
raise AttributeError(
'Coefficients are not defined for Booster type {}'
.format(self.booster))
b = self.get_booster()
coef = np.array(json.loads(b.get_dump(dump_format='json')[0])['weight'])
coef = np.array(json.loads(
b.get_dump(dump_format='json')[0])['weight'])
# Logic for multiclass classification
n_classes = getattr(self, 'n_classes_', None)
if n_classes is not None:
Expand All @@ -732,9 +734,10 @@ def intercept_(self):
-------
intercept_ : array of shape ``(1,)`` or ``[n_classes]``
"""
if getattr(self, 'booster', None) is not None and self.booster != 'gblinear':
raise AttributeError('Intercept (bias) is not defined for Booster type {}'
.format(self.booster))
if self.get_params()['booster'] != 'gblinear':
raise AttributeError(
'Intercept (bias) is not defined for Booster type {}'
.format(self.booster))
b = self.get_booster()
return np.array(json.loads(b.get_dump(dump_format='json')[0])['bias'])

Expand Down
13 changes: 13 additions & 0 deletions tests/python/test_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ def test_feature_importances_gain():
np.testing.assert_almost_equal(xgb_model.feature_importances_, exp)


def test_select_feature():
from sklearn.datasets import load_digits
from sklearn.feature_selection import SelectFromModel
digits = load_digits(2)
y = digits['target']
X = digits['data']
cls = xgb.XGBClassifier()
cls.fit(X, y)
selector = SelectFromModel(cls, prefit=True, max_features=1)
X_selected = selector.transform(X)
assert X_selected.shape[1] == 1


def test_num_parallel_tree():
from sklearn.datasets import load_boston
reg = xgb.XGBRegressor(n_estimators=4, num_parallel_tree=4,
Expand Down