Given feature/column names do not match the ones for the data given during fit. Error

Viewed 1200

I wrote following code and it gives me this error :

"Given feature/column names do not match the ones for the data given during fit."

Train and predict data has the same features.

df_train = data_preprocessing(df_train)

#Split X and Y
X_train = df_train.drop(target_columns,axis=1)
y_train = df_train[target_columns]

#Create a boolean mask for categorical columns
categorical_columns = X_train.columns[X_train.dtypes == 'O'].tolist()

# Create a boolean mask for numerical columns
numerical_columns = X_train.columns[X_train.dtypes != 'O'].tolist()

# Scaling & Encoding objects
numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])

categorical_transformer = OneHotEncoder(handle_unknown='ignore')

col_transformers = ColumnTransformer(
                        # name, transformer itself, columns to apply
                        transformers=[("scaler_onestep", numeric_transformer, numerical_columns),
                        ("ohe_onestep", categorical_transformer, categorical_columns)])

#Manual PROCESSING
model = MultiOutputClassifier(
        xgb.XGBClassifier(objective="binary:logistic",
                        colsample_bytree = 0.5
                        ))

#Define a pipeline
pipeline = Pipeline([("preprocessing", col_transformers), ("XGB", model)])

pipeline.fit(X_train, y_train)

#Data Preprocessing
predicted = data_preprocessing(predicted)
X_predicted = predicted.drop(target_columns,axis=1)

predictions=pipeline.predict(X_predicted)

I got error on prediction process. How can i fix this problem? I couldn't find any solution.

3 Answers

Try reordering the columns in X_predicted so that they exactly match X_train.

I am guessing the features names in the training dataset are not identical with the predicted dataset. For example if you have 19 features in the training dataset, it must be the same 19 samples in the predicted dataset. The model cannot test on features, it has not seen before.

Adding to the above answers - if you're using a ColumnTransformer like OP and are unsure about what the column names were at the time the model was fit, you can use pipeline.named_steps['preprocessing']._feature_names_in to figure it out.

Related