gpt4 book ai didi

python - 如何在特征重要性图中显示原始特征名称?

转载 作者:行者123 更新时间:2023-11-30 22:12:33 25 4
gpt4 key购买 nike

我创建的 XGBoost 模型如下:

y = XY.DELAY_MIN
X = standardized_df

train_X, test_X, train_y, test_y = train_test_split(X.as_matrix(), y.as_matrix(), test_size=0.25)

my_imputer = preprocessing.Imputer()
train_X = my_imputer.fit_transform(train_X)
test_X = my_imputer.transform(test_X)

xgb_model = XGBRegressor()

# Add silent=True to avoid printing out updates with each cycle
xgb_model = XGBRegressor(n_estimators=1000, learning_rate=0.05)
xgb_model.fit(train_X, train_y, early_stopping_rounds=5,
eval_set=[(test_X, test_y)], verbose=False)

当我创建特征重要性图时,特征名称显示为“f1”、“f2”等。如何显示原始特征名称?

fig, ax = plt.subplots(figsize=(12,18))
xgb.plot_importance(xgb_model, max_num_features=30, height=0.8, ax=ax)
plt.show()

最佳答案

问题是 Imputer 不会返回 pd.DataFrame 作为 transform() 的输出,因此,您的列名称当你这样做时迷路

train_X = my_imputer.fit_transform(train_X)
test_X = my_imputer.transform(test_X)

简单的解决方案,将输入器输出包装到数据帧中,例如如下所示:

train_X = pd.DataFrame(my_imputer.fit_transform(train_X), columns=train_X.columns)
test_X = pd.DataFrame(my_imputer.transform(test_X), columns=test_X.columns)

关于python - 如何在特征重要性图中显示原始特征名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51065490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com