如何使用投票回归器预测特定实例?

我从一些回归器中创建了一个投票回归器,例如

voting_regressor = VotingRegressor(estimators=[('xg',xgbregressor),('gb',gradient_boosting_regressor),('et',extra_trees_regressor),('rf',random_forest_regressor)])voting_regressor.fit(X_train, y_train)

该回归器在测试集上的预测效果很好

y_pred = voting_regressor.predict(X_test)

但是当我尝试预测特定实例时

voting_regressor.predict(X_test.iloc[0].values.reshape(1,-1))

它显示以下错误

ValueError: 特征名称不匹配: [‘yearpublished’, ‘minplayers’, ‘maxplayers’, ‘playingtime’, ‘minplaytime’, ‘maxplaytime’, ‘minage’, ‘users_rated’, ‘total_owners’, ‘total_traders’, ‘total_wanters’, ‘total_wishers’, ‘total_comments’, ‘total_weights’, ‘average_weight’] [‘f0’, ‘f1’, ‘f2’, ‘f3’, ‘f4’, ‘f5’, ‘f6’, ‘f7’, ‘f8’, ‘f9’, ‘f10’, ‘f11’, ‘f12’, ‘f13’, ‘f14’] 预期输入数据中的 users_rated, total_wishers, yearpublished, maxplayers, maxplaytime, total_owners, total_weights, average_weight, minplaytime, total_wanters, total_traders, playingtime, minage, total_comments, minplayers 训练数据中没有以下字段: f9, f3, f13, f0, f8, f4, f14, f5, f2, f6, f12, f11, f7, f10, f1


回答:

你在使用 iloc 时传递了 pandas.Series 而不是 pandas.DataFrame,错误信息表明需要列名。

如果你想返回包含一个例子的数据框,可以像这样用另一个列表包装它:

voting_regressor.predict(X_test.iloc[[0]])

这样可以保留列名

你也可以通过使用 [0, 1, 2, 3] 来指定多个示例。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注