Skip to content
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
1 change: 1 addition & 0 deletions python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ def apply(
missing=self.missing,
feature_types=self.feature_types,
nthread=self.n_jobs,
enable_categorical=self.enable_categorical,
)
return self.get_booster().predict(
test_dmatrix, pred_leaf=True, iteration_range=iteration_range
Expand Down
20 changes: 20 additions & 0 deletions tests/python/test_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,3 +1567,23 @@ def test_doc_link() -> None:
name = est.__class__.__name__
link = est._get_doc_link()
assert f"xgboost.{name}" in link


def test_apply_method():
import pandas as pd

X_num = np.random.rand(5, 5)
df = pd.DataFrame(X_num, columns=[f"f{i}" for i in range(X_num.shape[1])])
df["test"] = pd.Series(
["one", "two", "three", "four", "five"], dtype="category"
) # <- categorical column
y = np.arange(len(df))

model = xgb.XGBClassifier(enable_categorical=True)
model.fit(df, y)

model.apply(df) # this must not raise

model.set_params(enable_categorical=False)
with pytest.raises(ValueError, match="`enable_categorical`"):
model.apply(df)
Loading