【发布时间】: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