Skip to content

Adding enable_categorical to .apply method#11550

Merged
trivialfis merged 4 commits into
dmlc:masterfrom
imeyer2:update-apply-method
Jul 10, 2025
Merged

Adding enable_categorical to .apply method#11550
trivialfis merged 4 commits into
dmlc:masterfrom
imeyer2:update-apply-method

Conversation

@imeyer2

@imeyer2 imeyer2 commented Jul 9, 2025

Copy link
Copy Markdown
Contributor

Currently, XGBModel can be trained with enable_categorical=True when the input data fields have categorical type when appropriate. However, the .apply method will throw when trying to retrieve leaf values.

Example

import xgboost as xgb
import numpy as np
import pandas as pd

model = xgb.XGBClassifier(enable_categorical = True)

arr = np.random.rand(5,5)

df = pd.DataFrame(arr)

df['test']= ['one', 'two','three', 'four', 'five']


df = df.convert_dtypes()

# convert string to categorical
df['test'] = df['test'].astype('category')

model.fit(df, [0,1,2,3,4]) # works


model.apply(df) # fails

Produces

Exception has occurred: ValueError
DataFrame.dtypes for data must be int, float, bool or category. When categorical type is supplied, the experimental DMatrix parameter`enable_categorical` must be set to `True`.  Invalid columns:test: category
KeyError: 'category'

During handling of the above exception, another exception occurred:

  File "/path/to/python/file/test.py", line 22, in <module>
    model.apply(df)
ValueError: DataFrame.dtypes for data must be int, float, bool or category. When categorical type is supplied, the experimental DMatrix parameter`enable_categorical` must be set to `True`.  Invalid columns:test: category

@trivialfis trivialfis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix! Could you please help add a small test in the test_with_sklearn.py file?

Added test for .apply
@imeyer2

imeyer2 commented Jul 10, 2025

Copy link
Copy Markdown
Contributor Author

Thank you for the fix! Could you please help add a small test in the test_with_sklearn.py file?

Done!

@trivialfis trivialfis merged commit 9c0efce into dmlc:master Jul 10, 2025
62 checks passed
@trivialfis trivialfis mentioned this pull request Jul 19, 2025
10 tasks
trivialfis added a commit to trivialfis/xgboost that referenced this pull request Jul 23, 2025
---------

Co-authored-by: Jiaming Yuan <jm.yuan@outlook.com>
trivialfis added a commit to trivialfis/xgboost that referenced this pull request Jul 23, 2025
---------

Co-authored-by: Jiaming Yuan <jm.yuan@outlook.com>
trivialfis added a commit that referenced this pull request Jul 23, 2025
)

---------

Co-authored-by: imeyer2 <130110431+imeyer2@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants