Error Invoking xgboost endpoint deployed locally : feature names mismatch

Viewed 146

I have trained a xgboost model locally and running into feature_names mismatch issue when invoking the endpoint. My model is a xgboost Regressor with some pre-processing (variable encoding) and hyper-parameter tuning. Code to train the model:

version xgboost 0.90

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
.....

# Fit model - convert to numpy arrays
model.fit(X_train.toarray(), y_train.values())

Here's what model object looks like:

XGBRegressor(colsample_bytree=xxx, gamma=xxx,
             learning_rate=xxx, max_depth=x, n_estimators=xxx,
             subsample=xxx)

My test data is a string of float values which is turned into an array as the data must be passed as numpy array to match how the model was trained.

testdata = [........., 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 2000, 200, 85, 412412, 123, 41, 552, 50000, 512, 0.1, 10.0, 2.0, 0.05]

Traceback


ValueError: feature_names mismatch: ['f0', 'f1', 'f2', 'f3', 'f4', 'f5'....'f419'] ['f0']
gc43e67lgg-algo-1-8jxpe  | expected f156, f266, f164, f207, f107, f296, f417, f188, f131, f225,.....

I can retrieve the feature names for the model by running model.get_booster().feature_names. Is there a way I can use these names and assign to test data point so that they are consistent?

['f0', 'f1', 'f2', 'f3', 'f4', 'f5',......'f417','f418','f419']

More info on inference script here: https://github.com/aws-samples/amazon-sagemaker-local-mode/blob/main/xgboost_script_mode_local_training_and_serving/code/inference.py

0 Answers
Related