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

[backport] Fix prediction with cat data in sklearn interface. (#7306) #7312

Merged
merged 1 commit into from Oct 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions python-package/xgboost/sklearn.py
Expand Up @@ -174,7 +174,9 @@ def inner(preds: np.ndarray, dmatrix: DMatrix) -> Tuple[np.ndarray, np.ndarray]:
Device ordinal.
validate_parameters : Optional[bool]
Give warnings for unknown parameter.

predictor : Optional[str]
Force XGBoost to use specific predictor, available choices are [cpu_predictor,
gpu_predictor].
enable_categorical : bool

.. versionadded:: 1.5.0
Expand Down Expand Up @@ -807,7 +809,11 @@ def _can_use_inplace_predict(self) -> bool:
# Inplace predict doesn't handle as many data types as DMatrix, but it's
# sufficient for dask interface where input is simpiler.
predictor = self.get_params().get("predictor", None)
if predictor in ("auto", None) and self.booster != "gblinear":
if (
not self.enable_categorical
and predictor in ("auto", None)
and self.booster != "gblinear"
):
return True
return False

Expand All @@ -834,7 +840,9 @@ def predict(
iteration_range: Optional[Tuple[int, int]] = None,
) -> np.ndarray:
"""Predict with `X`. If the model is trained with early stopping, then `best_iteration`
is used automatically.
is used automatically. For tree models, when data is on GPU, like cupy array or
cuDF dataframe and `predictor` is not specified, the prediction is run on GPU
automatically, otherwise it will run on CPU.

.. note:: This function is only thread safe for `gbtree` and `dart`.

Expand Down Expand Up @@ -862,6 +870,7 @@ def predict(
Returns
-------
prediction

"""
iteration_range = _convert_ntree_limit(
self.get_booster(), ntree_limit, iteration_range
Expand All @@ -886,7 +895,10 @@ def predict(
pass

test = DMatrix(
X, base_margin=base_margin, missing=self.missing, nthread=self.n_jobs
X, base_margin=base_margin,
missing=self.missing,
nthread=self.n_jobs,
enable_categorical=self.enable_categorical
)
return self.get_booster().predict(
data=test,
Expand Down
1 change: 1 addition & 0 deletions tests/python-gpu/test_gpu_with_sklearn.py
Expand Up @@ -59,6 +59,7 @@ def test_categorical():
)
X = pd.DataFrame(X.todense()).astype("category")
clf.fit(X, y)
assert not clf._can_use_inplace_predict()

with tempfile.TemporaryDirectory() as tempdir:
model = os.path.join(tempdir, "categorial.json")
Expand Down