Conversion error using XGBoost with numpy

Viewed 581

I would like to use the package XGboost in Python, however, when I want to create the model using numpy data and label with the same shape.

Following this:

If I run the following code, should work:

 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)

I get this error:

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

Both X and y has this content:

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]])

Updated:

If using

xg_reg.fit(X,y)

Then:

~\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: 

Any clue?

1 Answers

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

You're attempting to pass the xgb.DMatrix data matrix type via Scikit-Learn APIs, which do not know about it.

If you're working with low-level XGBoost APIs then you need to prepare and present data according to XGBoost conventions (eg. the data must be presented as zgb.DMatrix). However, when you're working with high-level XGBoost APIs such as the Scikit-Learn API, then you need to prepare and present data according to Scikit-Learn conventions (eg. Numpy arrays, Pandas DataFrames).

To fix the problem, simply do xg_reg.fit(X,y). The XGBRegressor class will create a proper xgb.DMatrix instance internally, it doesn't need your help for that.

Related