1

我正在尝试使用 XGBoostRegressor 来预测测试和训练集;但是,每次我运行代码生成预测时,都会出现特征不匹配错误。

下面的代码显示了我创建的模型,以及我如何每次选择 1 列 XTrain 训练数据来拟合模型。此外,我使用 SKLearn 的 Test Train split 拆分训练和测试数据,这意味着随机抽取行来创建训练和测试数据(80/20 拆分)。

当我运行预测“y_predfin”和“y_pred”时,会出现特征不匹配错误。

我尝试了以下方法: 将所有数据转换为 numpy 数组重新调整循环的 1 列数据,因为 XGBoost 不需要 1 列,将 Eval_Set 替换为手动计算训练和测试错误。

modelTrain = XGBRegressor(colsample_bytree=0.4,
                 gamma=0,                 
                 learning_rate=0.07,
                 max_depth=3,
                 min_child_weight=1.5,
                 n_estimators=10000,                                                                    
                 reg_alpha=0.75,
                 reg_lambda=0.45,
                 subsample=0.6,
                 seed=650318,
                 early_stopping_rounds = 10)

Columns = X_train.columns
print(Columns)
X_train = X_train.values
Y_train = Y_train.values
X_test = X_test.values
Y_test = Y_test.values
XTrainShuffeled = XTrainShuffeled.values
Y_testuse = np.reshape(Y_test, (1,-1))
Y_trainuse = np.reshape(Y_train, (1,-1))

for i in range(len(XFinalData) - 1):
    X_trainuse = np.reshape(X_train[:,i+1], (1,-1))
    X_testuse = np.reshape(X_test[:,i+1], (1,-1))
    modelTrain.fit(X_trainuse, Y_trainuse, eval_metric = "error", verbose = True)
    y_predfin = modelTrain.predict(X_testuse)
    y_pred = modelTrain.predict(X_trainuse)
    TrainErrorMSE = mean_squared_error(Y_trainuse, y_pred)
    TestErrorMSE = mean_squared_error(Y_testuse, y_predfin)
    Importance.update({str(Columns[i+1]) + " Importance" : modelTrain.feature_importances_})
    TrainError.update({str(Columns[i+1]) + " TrainError" : TrainErrorMSE})
    TestError.update({str(Columns[i+1]) + " TestError" : TestErrorMSE})

我希望得到预测,但是在使用 X_testuse 进行预测时出现了特征不匹配错误,因为行数与 X_trainuse 上的行数不匹配(80/20 拆分)。

感谢您的帮助!

4

0 回答 0