Xgboost: How to feed test data to my xgb model?

Created on 2 Feb 2017  路  2Comments  路  Source: dmlc/xgboost

I trained my xgboost model and saved it with pickle.dumps(model.pkl). However, it can not test.

I tried to convert it to DMatrix, by converting my list to np.array. But it requires the array to be 2-dimensional. Why the test data needs to be two-dimensional? And how can i fix it?

fv is the input list, containing 10 numbers. eg: ['0.080037', '-0.0186235', '-0.231992', '0.0478659', '-0.0359493', '0.575675', '0.0375554', '0.0888634', '0.154359', '0.0245678']

code:
clf = pickle.load( open( "model.pkl", "rb" ))
fv = [float(i) for i in fv]
fv = numpy.array(fv)
fv = xgb.DMatrix(fv)
print fv
value = clf.predict(fv)`

error: (i looked up in the core.py but don't understand)
File "/usr/local/lib/python2.7/dist-packages/xgboost/core.py", line 314, in _init_from_npy2d
raise ValueError('Input numpy.ndarray must be 2 dimensional')
ValueError: Input numpy.ndarray must be 2 dimensional

Most helpful comment

xgboost (like sklearn) expects X as 2D data (n_samples, n_features). If you want to predict only one sample you can reshape your feature vector to a 2D arry like this:

fv = [1,2,3,4,5]
fv = numpy.array(fv).reshape((1,-1))
clf.predict(fv)

All 2 comments

xgboost (like sklearn) expects X as 2D data (n_samples, n_features). If you want to predict only one sample you can reshape your feature vector to a 2D arry like this:

fv = [1,2,3,4,5]
fv = numpy.array(fv).reshape((1,-1))
clf.predict(fv)

I'm a little late to the party for this issue. The solution proposed by Gunthard does work, but it seems a little hackish. Is there an important reason why XGBoost can't accept a 1D array? It seems like testing samples one at a time would be something that would come up a lot once the model has been tweaked sufficiently and the learning system is going to go into production.

I'd be willing to look into trying to put together a merge request for this issue, if it is indeed an issue that should be fixed.

Was this page helpful?
0 / 5 - 0 ratings