简体   繁体   English

XGB - 特征形状不匹配

[英]XGB - Feature shape mismatch

the function failed at prediction with the error: function 预测失败并出现以下错误:

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

testX - 1 X 395 (Dataframe) trainX - n X 395 (Dataframe) 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

I hit the same issue today, yet in a small percentage of my fit/predict cycles.我今天遇到了同样的问题,但在我的拟合/预测周期的一小部分。 This, below, appears to have gotten me around the problem, as I put this snippet after fitting the first time in a try: block and hitting the same error...下面,这似乎让我解决了这个问题,因为我在第一次尝试尝试后放置了这个片段:阻止并遇到相同的错误......

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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM