我收到此错误 'DataFrame.dtypes for data must be int, float, bool or categorical'

I got this error 'DataFrame.dtypes for data must be int, float, bool or categorical'

我要将其训练为 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))

但是出现了这种错误,我以前从未见过这样的。

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

我该如何解决这个问题?

感谢您的帮助。

看来你有分类数据。 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))

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