Skip to content

Commit

Permalink
Disable feature validation on sklearn predict prob. (#5953)
Browse files Browse the repository at this point in the history
* Fix issue when scikit learn interface receives transformed inputs.
  • Loading branch information
trivialfis authored Jul 29, 2020
1 parent 18349a7 commit f5fdcbe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def predict(self, data, output_margin=False, ntree_limit=None,
'Label encoder is not defined. Returning class probability.')
return class_probs

def predict_proba(self, data, ntree_limit=None, validate_features=True,
def predict_proba(self, data, ntree_limit=None, validate_features=False,
base_margin=None):
"""
Predict the probability of each `data` example being of a given class.
Expand Down
32 changes: 32 additions & 0 deletions tests/python/test_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,38 @@ def test_parameter_validation():
assert len(output) == 0


@pytest.mark.skipif(**tm.no_pandas())
def test_pandas_input():
import pandas as pd
from sklearn.calibration import CalibratedClassifierCV
rng = np.random.RandomState(1994)

kRows = 100
kCols = 6

X = rng.randint(low=0, high=2, size=kRows*kCols)
X = X.reshape(kRows, kCols)

df = pd.DataFrame(X)
feature_names = []
for i in range(1, kCols):
feature_names += ['k'+str(i)]

df.columns = ['status'] + feature_names

target = df['status']
train = df.drop(columns=['status'])
model = xgb.XGBClassifier()
model.fit(train, target)
clf_isotonic = CalibratedClassifierCV(model,
cv='prefit', method='isotonic')
clf_isotonic.fit(train, target)
assert isinstance(clf_isotonic.calibrated_classifiers_[0].base_estimator,
xgb.XGBClassifier)
np.testing.assert_allclose(np.array(clf_isotonic.classes_),
np.array([0, 1]))


class TestBoostFromPrediction(unittest.TestCase):
def run_boost_from_prediction(self, tree_method):
from sklearn.datasets import load_breast_cancer
Expand Down

0 comments on commit f5fdcbe

Please sign in to comment.