xgboost predict error : can not initialize DMatrix from DMatrix

I have trained a xgboost model with pre-processing + hyper-parameter tuning. I save the model using pickle.dump and then upload to the ML engine. When I try to make prediction / run inference, I get the following error / exception:

version: python 3.6
         xgboost 0.90

TypeError('no supported conversion for types: %r' % (args,)) 
TypeError: can not initialize DMatrix from DMatrix


import pandas as pd
import pickle
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder 


# split df into train and test
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,0:21], df.iloc[:,-1], test_size=0.1)

X_train.shape
(1000,21)

# Encode categorical variables  
cat_vars = ['cat1','cat2','cat3']
cat_transform = ColumnTransformer([('cat', OneHotEncoder(handle_unknown='ignore'), cat_vars)], remainder='passthrough')

encoder = cat_transform.fit(X_train)
X_train = encoder.transform(X_train)
X_test = encoder.transform(X_test)

X_train.shape
(1000,420)

# Define a xgboost regression model
model = XGBRegressor()

# Do hyper-parameter tuning
.... and save as rsearch

# Fit model
rsearch.fit(X_train, y_train)

# save the model
model_file_name = "xgboost-model"

pickle.dump(xgbest, open(model_file_name, 'wb'))

# to tar.gz file for AWS compatibility
!tar czvf xgboost_model.tar.gz $model_file_name

# upload to aws s3
key = 'xgboost_model.tar.gz'

with open('xgboost_model.tar.gz', 'rb') as f:
     client.upload_fileobj(f, bucket, key)

When I run the code to invoke the model for inference, which includes read the saved model, accept test data for prediction, make prediction and return output, I get the exception. Full traceback below:

raise TypeError('no supported conversion for types: %r' % (args,))
TypeError: no supported conversion for types: (dtype('O'),)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/miniconda3/lib/python3.6/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper

File "/opt/ml/code/inference.py", line 70, in predict_fn
prediction = model.predict(input_data)
File "/miniconda3/lib/python3.6/site-packages/xgboost/sklearn.py", line 448, in predict
test_dmatrix = DMatrix(data, missing=self.missing, nthread=self.n_jobs)
File "/miniconda3/lib/python3.6/site-packages/xgboost/core.py", line 413, in __init__
' {}'.format(type(data).__name__))
TypeError: can not initialize DMatrix from DMatrix

Source: Python Questions

LEAVE A COMMENT