python - 如何在特征重要性图中显示原始特征名称?
问题描述
我创建了 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
不返回 apd.DataFrame
作为 的输出transform()
,因此,当您这样做时,您的列名会丢失
train_X = my_imputer.fit_transform(train_X)
test_X = my_imputer.transform(test_X)
简单的解决方案,将 imputer 输出包装到数据框中,例如:
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)
推荐阅读
- angular - Angular 5 - 将点击事件动态附加到动态创建的dom
- spring - 来自 JSON 的 Int 字段的 Kotlin + Spring Boot 默认值
- javascript - 使用 foreach 和 switch 循环遍历数组并在 angularjs 中返回不同的值
- angular - 我可以将我的组件作为 Angular 6 中其他组件的提供者输入吗?
- php - SOAP 在一级对象中返回 XML
- xml - xsd:double 限制到小数点后六位
- php - 在 Woocommerce 中为特定产品类别添加费用
- nlp - spaCy 的 lemmatizer 与其他库相比如何?
- css - 带有引导程序和 css 的响应式家谱
- dotnetnuke - DNN 9 修改角色