使用带numpy的XGBoost时发生转换错误

2024-04-19 15:43:48 发布

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

但是,当我想使用numpy数据和具有相同形状的标签创建模型时,我想使用Python中的XGboost包

以下this

如果我运行以下代码,应该可以工作:

 xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
                max_depth = 5, alpha = 10, n_estimators = 10)
 xg_reg.fit(xgb.DMatrix(X),y)

我得到这个错误:

TypeError: Not supported type for data.<class 'xgboost.core.DMatrix'>

X和y都有以下内容:

array([[41.4049364,  2.177356 ],
       [41.4049656,  2.1773926],
       [41.4049938,  2.1774287],
       [41.4050204,  2.1774638],
       [41.4050453,  2.1774975],
       [41.4050682,  2.1775296],
       [41.4050895,  2.1775597],
       [41.4051093,  2.1775874],
       [41.4051278,  2.1776125]])

更新:

如果使用

xg_reg.fit(X,y)

然后:

~\anaconda3\lib\site-packages\xgboost\data.py in _validate_meta_shape(data)
    615 def _validate_meta_shape(data):
    616     if hasattr(data, 'shape'):
--> 617         assert len(data.shape) == 1 or (
    618             len(data.shape) == 2 and
    619             (data.shape[1] == 0 or data.shape[1] == 1))

AssertionError: 

有线索吗


Tags: or数据numpydatalenregvalidatemeta
1条回答
网友
1楼 · 发布于 2024-04-19 15:43:48

xg_reg = xgb.XGBRegressor(..) xg_reg.fit(xgb.DMatrix(X),y)

您正试图通过Scikit学习API传递xgb.DMatrix数据矩阵类型,而Scikit学习API对此一无所知

如果您使用的是低级XGBoost API,那么您需要根据XGBoost约定准备和呈现数据(例如,数据必须以zgb.DMatrix的形式呈现)。但是,当您使用高级XGBoost API(如Scikit学习API)时,您需要根据Scikit学习约定(如Numpy数组、数据帧)准备和呈现数据

要解决此问题,只需执行xg_reg.fit(X,y)XGBRegressor类将在内部创建一个适当的xgb.DMatrix实例,它不需要您的帮助

相关问题 更多 >