Python XGBoost分类器无法`预测':`TypeError:不支持数据的类型`

2024-05-15 00:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个数据集:

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代码,但运气不好


Tags: 数据testdevicetypeint8printnaestimate
2条回答

在将数据帧传递给xgb.DMatrix之前,使用.values将数据帧转换为numpy数组时,我遇到了相同的错误

dtest = xgb.DMatrix(X_test.values)

根据下面的帖子https://datascience.stackexchange.com/a/43805/87921,我发现xgboost直接支持0.81版以来的pandasDataFrames,所以调用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)

希望有帮助

我面临同样的问题,并通过使用np.float32()重新定义数据类型来解决它:

model.predict(np.float32(X_test))

相关问题 更多 >

    热门问题