python - Pycebox IcePlot 不能在 Xgboost 上工作,而在随机森林上工作
问题描述
当我使用 XGBoost 运行 Pycebox 时出现以下错误,训练运行完美,但不确定为什么在使用 iceplot 时会出现 [fx] 字段。我也双重确认它们不在数据集中
ValueError: feature_names mismatch: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] ['f0', 'f1', 'f2', 'f3']
expected petal width (cm), petal length (cm), sepal length (cm), sepal width (cm) in input data
training data did not have the following fields: ***f3, f1, f0, f2***
我创建了一个使用 iris 数据的示例
XGboost 代码:
from sklearn.datasets import load_iris
from pycebox.ice import ice, ice_plot
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
import xgboost as xgb
import matplotlib.pyplot as plt
iris = load_iris()
data1 = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])
target = data1['target']
training = data1.drop(['target'],axis=1)
X_train, X_test, y_train, y_test = train_test_split(training, target, test_size=0.4)
xg_reg = xgb.XGBRegressor(random_state=1234,eval_metric='rmse',n_jobs=-1)
xg_reg.fit(X_train,y_train)
forty_ice_df = ice(data=X_train, column='petal length (cm)',
predict=xg_reg.predict)
ice_plot(forty_ice_df, c='dimgray', linewidth=0.3)
plt.ylabel('Pred. Target')
plt.xlabel('petal length (cm)')
虽然它适用于随机森林
rf = RandomForestRegressor(random_state = 1234, n_jobs=18)
rf.fit(X_train, y_train)
forty_ice_df = ice(data=X_train, column='petal length (cm)',
predict=rf.predict)
ice_plot(forty_ice_df, c='dimgray', linewidth=0.3)
plt.ylabel('Pred. Target')
plt.xlabel('petal length (cm)')
解决方案
只需X_train
更改X_train.values
推荐阅读
- c# - 为什么 goo.gl 短网址没有返回预期的 301 状态码?
- r - 如何在 R 中编写循环循环以通过在名称内建立索引来消除列?
- sql - 删除表是否会删除其触发器和函数?
- f# - 如何使 Nuget.org 包中的 ExcelDna 函数在包用户的 Excel 会话中可见
- regex - 至少一个带有现有正则表达式的数字
- python - 验证子目录的大小
- sql - 从 JdbcTemplate 中选择 count(*) 查询返回的值与从 SQL 终端运行查询不同
- python - 为什么使用 if in 检查字符串值不起作用?
- jmeter - 如何更改在 JMetter 中计算请求时间的方式?
- flutter - 如何在 Flutter 中分配一个无法删除的固定值