ValueError:feature_names 不匹配:在 predict() 函数中的 xgboost [英] ValueError: feature_names mismatch: in xgboost in the predict() function

查看:29
本文介绍了ValueError:feature_names 不匹配:在 predict() 函数中的 xgboost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个 XGBoostRegressor 模型.当我必须使用这个经过训练的模型来预测新输入时,predict() 函数会抛出一个 feature_names 不匹配错误,尽管输入特征向量与训练数据具有相同的结构.

I have trained an XGBoostRegressor model. When I have to use this trained model for predicting for a new input, the predict() function throws a feature_names mismatch error, although the input feature vector has the same structure as the training data.

此外,为了构建与训练数据结构相同的特征向量,我做了很多低效的处理,例如添加新的空列(如果数据不存在),然后重新排列数据列,使其与训练结构相匹配.有没有更好、更简洁的方式来格式化输入,使其与训练结构相匹配?

Also, in order to build the feature vector in the same structure as the training data, I am doing a lot inefficient processing such as adding new empty columns (if data does not exist) and then rearranging the data columns so that it matches with the training structure. Is there a better and cleaner way of formatting the input so that it matches the training structure?

推荐答案

这是模型构建时列名顺序与模型评分时列名顺序不同的情况.

This is the case where the order of column-names while model building is different from order of column-names while model scoring.

我已使用以下步骤来克服此错误

I have used the following steps to overcome this error

首先加载pickle文件

First load the pickle file

model = pickle.load(open("saved_model_file", "rb"))

按使用顺序提取所有列

cols_when_model_builds = model.get_booster().feature_names

重新排序熊猫数据框

pd_dataframe = pd_dataframe[cols_when_model_builds]

这篇关于ValueError:feature_names 不匹配:在 predict() 函数中的 xgboost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆