python - 给定的特征/列名称与拟合期间给定的数据不匹配。错误

标签 python pandas scikit-learn

我写了下面的代码,它给了我这个错误:

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

训练和预测数据具有相同的特征。

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)

我在预测过程中出错。我该如何解决这个问题?我找不到任何解决方案。

最佳答案

尝试对 X_predicted 中的列重新排序,以便它们与 X_train 完全匹配。

关于python - 给定的特征/列名称与拟合期间给定的数据不匹配。错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68362413/

相关文章:

python - 重写的哈希函数的计算值和返回值不同

Python - 使用音频 openCV 播放无限视频,卡在音频

python - 并行运行 Flair 嵌入

python - 使用切片和数据帧系列作为结尾的子字符串

python - 如何在 Pandas DataFrame 散点图中添加图例?

python - 将 scikit-learn 中 GridSearchCV 中每个折叠的准确性和平均值制成表格

machine-learning - 来自 Scikit_Learn 混淆矩阵和 Scikit_Learn Recall_Score 的灵敏度不匹配

python - Flask 出现 400 错误

Python - 在数据框中编码基因组数据

machine-learning - 多类分类的 sklearn 指标