我收到此错误“数据的 DataFrame.dtypes 必须是 int、float、bool 或 categorical”

min*_*ral 12 python xgboost

我将把它训练为 xgboost 模型。

在此输入图像描述

“start_time”、“end_time”列采用 yyyy-mm-dd hh:mm:ss 格式。

我使用 astype(str) 将其更改为字符串,并使用正则表达式将其更改为 yyyymmddhhmmss 格式。

xgb_model = xgboost.XGBClassifier(eta=0.1, nrounds=1000, max_depth=8, colsample_bytree=0.5, scale_pos_weight=1.1, booster='gbtree', 
                                  metric='multi:softmax')
hr_pred = xgb_model.fit(x_train, np.ravel(y_train, order='C')).predict(x_test)
print(classification_report(y_test, hr_pred))
Run Code Online (Sandbox Code Playgroud)

但发生了这种错误,我以前从未见过这样的错误。

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`.start_time, end_time
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

感谢您的帮助。

Car*_*gan 17

看来你有分类数据。Start_timeend_time是对象类型。

您需要删除它们或对它们进行编码。

放下它们

xgb_model = xgboost.XGBClassifier(eta=0.1, nrounds=1000, max_depth=8, colsample_bytree=0.5, scale_pos_weight=1.1, booster='gbtree', 
                                  metric='multi:softmax')
hr_pred = xgb_model.fit(x_train._get_numeric_data(), np.ravel(y_train, order='C')).predict(x_test._get_numeric_data())
print(classification_report(y_test, hr_pred))
Run Code Online (Sandbox Code Playgroud)

要对它们进行编码,请查看此库https://contrib.scikit-learn.org/category_encoders/

  • @mineral如果答案有帮助,很高兴说谢谢,但同时你应该接受答案以奖励回答的人。 (2认同)