You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XGBoost回归启用enable_categorical仍报ValueError问题求助

XGBoost类别变量支持功能报错解决

问题情况

已将字符串特征转为category类型,且在定义XGBRegressor时设置enable_categorical=True,但训练时仍抛出ValueError,提示类别变量需开启enable_categorical参数。

训练数据信息

#   Column                    Non-Null Count  Dtype   
---  ------                    --------------  -----   
 0   column1                   96529 non-null  category
 1   column2                   96529 non-null  category
 2   column3                   96529 non-null  category
 3   column4                   96529 non-null  category
 4   column5                   96529 non-null  category
 5   column6                   96529 non-null  float64 
 6   column7                   96529 non-null  float64 
 7   column8                   96529 non-null  float64 
 8   column9                   96529 non-null  float64 
 9   column10                  96529 non-null  int64   
 10  column11                  96529 non-null  int64   
 11  column12                  96529 non-null  int64   
 12  column13                  96529 non-null  float64 
 13  column14                  96529 non-null  float64 
 14  column15                  96529 non-null  float64 
 15  column16                  96529 non-null  float64 
 16  column17                  96529 non-null  float64 
 17  column18                  96529 non-null  int64   
 18  column19                  96529 non-null  float64 
 19  column20                  96529 non-null  int64   
 20  column21                  96529 non-null  float64 
 21  column22                  96529 non-null  float64 
 22  column23                  96529 non-null  float64 
 23  column24                  96529 non-null  float64 
dtypes: category(5), float64(14), int64(5)

使用代码

xgb_model = xgb.XGBRegressor(tree_method="gpu_hist", enable_categorical=True, max_depth = 128,n_estimators=1000,min_child_weight=25,learning_rate=0.025)

xgb_model.fit(X_train, y_train, early_stopping_rounds=10, eval_set=[(X_val, y_val)])

错误信息

ValueError: DataFrame.dtypes for data must be int, float, bool or categorical.  When
                categorical type is supplied, DMatrix parameter
                `enable_categorical` must be set to `True`.column1, column2, column3, column4, column5

解决方案

1. 统一训练集与验证集的特征类型

检查X_val的类别列类型,确保和X_train一致。如果验证集的类别列被转为字符串或其他类型,会触发错误。可以用以下代码统一处理:

cat_cols = ['column1', 'column2', 'column3', 'column4', 'column5']
# 转换为category类型
X_train[cat_cols] = X_train[cat_cols].astype('category')
X_val[cat_cols] = X_val[cat_cols].astype('category')
# 对齐类别(避免验证集出现训练集没有的类别)
for col in cat_cols:
    X_val[col] = X_val[col].cat.set_categories(X_train[col].cat.categories)

2. 显式创建DMatrix并设置参数

直接传入DataFrame时,XGBoost可能无法正确识别参数,显式创建DMatrix可解决该问题:

from xgboost import DMatrix

dtrain = DMatrix(X_train, label=y_train, enable_categorical=True)
dval = DMatrix(X_val, label=y_val, enable_categorical=True)

xgb_model = xgb.XGBRegressor(tree_method="gpu_hist", enable_categorical=True, max_depth=128, n_estimators=1000, min_child_weight=25, learning_rate=0.025)
xgb_model.fit(dtrain, y_train, early_stopping_rounds=10, eval_set=[(dval, y_val)])

3. 升级XGBoost版本

旧版本对类别变量的支持存在bug,确保使用版本>=1.5.0,可通过以下命令升级:

pip install --upgrade xgboost

4. 验证tree_method兼容性

gpu_hist支持类别变量,但如果GPU环境配置有问题,可尝试切换为CPU版本的hist测试:

xgb_model = xgb.XGBRegressor(tree_method="hist", enable_categorical=True, max_depth=128, n_estimators=1000, min_child_weight=25, learning_rate=0.025)

内容的提问来源于stack exchange,提问作者sena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:17:20