【问题标题】:Conversion error using XGBoost with numpy使用带有 numpy 的 XGBoost 的转换错误
【发布时间】:2021-05-04 12:13:17
【问题描述】:

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

关注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: 

有什么线索吗?

【问题讨论】:

  • 为什么你把X变成DMatrix,而不是y?
  • 链接没有只转换 y X。无论如何,我都尝试过转换,但也没有
  • 我认为它可能是数据的类型,例如,如果 x 或 y 是 dtype 'object',那么您需要将它们转换为 np.float64 或您认为适合您的任何数字类型问题。

标签: python numpy matrix xgboost


【解决方案1】:

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

您正试图通过 Scikit-Learn API 传递 xgb.DMatrix 数据矩阵类型,而后者对此一无所知。

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

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

【讨论】:

  • 我也尝试过获取问题中提供的错误的方法(更新)
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 2017-04-27
  • 2017-04-19
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
相关资源
最近更新 更多