【问题标题】:XGB - Feature shape mismatchXGB - 特征形状不匹配
【发布时间】:2021-08-01 06:20:42
【问题描述】:

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

        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

【问题讨论】:

标签: python decision-tree xgboost predict xgbclassifier


【解决方案1】:

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

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 )

【讨论】:

  • BTW - 我认为打印出错误消息的 XGBoost 代码中有一个错误,我在堆栈跟踪中看到我不再有。该代码似乎正在 df.shape[1] 上进行测试,但异常中的错误消息是使用 df.shape[0] 进行打印。请注意,在您的错误中显示“预期:395,得到 395”,如果我们谈论的是两个值应该不同的异常,这似乎是错误的。
猜你喜欢
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 2020-10-10
  • 2019-08-08
  • 1970-01-01
  • 2019-06-10
  • 2019-03-05
  • 2016-05-13
相关资源
最近更新 更多