使用XGboost预测测试和训练时,功能名称不匹配

时间:2019-08-02 14:17:00

标签: python-3.x xgboost

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

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

当我运行预测“ y_predfin”和“ y_pred”时,这是我遇到功能不匹配错误的时候。

我尝试了以下方法: 将所有数据转换为numpy数组由于XGBoost不占用1列,因此为循环重整了我的1列数据,将Eval_Set替换为手动计算Train和Test错误。

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)。

谢谢您的帮助!

0 个答案:

没有答案