0

该函数在预测时失败并出现错误:

        f"Feature shape mismatch, expected: {self.num_features()}, "
ValueError: Feature shape mismatch, expected: 395, got 395

testX - 1 X 395(数据帧) trainX - n X 395(数据帧)

def xgboost_forecast(train, testX):
    # split into input and output columns
    testX = np.asarray(testX)
    testX = testX.reshape(-1, 1)
    trainX, trainy = train.iloc[:, :-1], train.iloc[:, -1]
    trainy = np.asarray(trainy)
    trainy = trainy.reshape(-1, 1)
    # fit model
    model = xgb.XGBClassifier()
    model.fit(trainX.values, trainy.values)
    yhat = model.predict(testX) ##crash
4

1 回答 1

0

我今天遇到了同样的问题,但在我的拟合/预测周期的一小部分。下面,这似乎让我解决了这个问题,因为我在第一次尝试尝试后放置了这个片段:阻止并遇到相同的错误......

try:
    self.y_pred_DEBUG = pModel.predict( dataForPreds )
except ValueError:
    fNames_Error = pModel.get_booster().feature_names;
    alteredDataForPreds = dataForPreds[fNames_Error];
    self.y_pred_DEBUG = pModel.predict( alteredDataForPreds )
于 2021-06-15T04:42:59.377 回答