4

我有一个像这样的数据集:

print(X_test.dtypes)
metric1                    int64
rank                     float64
device_type                 int8
NA_estimate              float64

当我尝试对此数据集进行预测时,出现以下错误:

y_test_pred_xgb = clf_xgb.predict(xgb.DMatrix(X_test))
TypeError: Not supported type for data.<class 'xgboost.core.DMatrix'>

我搜索了一下,但只发现了object导致问题的可变数据类型的讨论。我的数据还有其他问题还是其他问题?我查看了各种博客和 Kaggle 代码,但没有运气。

4

2 回答 2

0

我遇到了同样的问题,并通过使用以下方式转换数据类型来解决它np.float32()

model.predict(np.float32(X_test))
于 2021-09-25T20:45:17.170 回答
0

在将数据框转换为 numpy 数组时,我遇到了同样的错误.values,然后将其传递给xgb.DMatrix.

dtest = xgb.DMatrix(X_test.values)

根据以下帖子https://datascience.stackexchange.com/a/43805/87921,我发现从 0.81 版开始xgboost直接支持 pandas ,因此不需要DataFrames调用。DMatrix这在我的情况下有效(我使用的是 1.3.3 版)

model = XGBRegressor().fit(X_train, y_train)  # X_train, y_train and X_test are DataFrames
predictions = model.predict(X_test)

希望,这有帮助!

于 2021-04-08T18:33:04.410 回答