plot_importance 绘制的特征重要性由其参数确定
importance_type,默认为weight。有 3 个选项:weight、gain 和 cover。不过,它们都不是百分比。
来自documentation 这个方法:
importance_type (str, default "weight") – 如何计算重要性:“weight”、“gain”或“cover”
- “权重”是特征在树中出现的次数
- “增益”是使用该特征的分割的平均增益
- “cover”是分割的平均覆盖率,它使用覆盖率定义为受分割影响的样本数的特征
所以,长话短说:你想要的没有简单的解决方案。
解决方法
模型的属性feature_importances_随心所欲地归一化,你可以自己绘制,不过会是手工制作的图表。
首先,确保将 Classifier 的 importance_type 参数设置为上面列举的选项之一(构造函数的默认值为 gain,因此如果你不要改变它)。
best_model = xgb.XGBClassifier(importance_type='weight')
之后你可以在这一行尝试一些东西:
import pandas as pd
best_model.feature_importances_
# In my toy example: array([0.21473685, 0.19157895, 0.28842106, 0.30526316], dtype=float32)
best_model.feature_importances_.sum()
# 1.0
# Build a simple dataframe with the feature importances
# You can change the naming fN to something more human readable
fs = len(best_model.feature_importances_)
df = pd.DataFrame(zip([f"f{n}" for n in range(fs)], best_model.feature_importances_), columns=['Features', 'Feature Importance'])
df = df.set_index('Features').sort_values('Feature Importance')
# Build horizontal bar char
ax = df.plot.barh(color='red', alpha=0.5, grid=True, legend=False, title='Feature importance', figsize=(15, 5))
# Annotate bar chart, adapted from this SO answer:
# https://stackoverflow.com/questions/25447700/annotate-bars-with-values-on-pandas-bar-plots
for p, value in zip(ax.patches, df['Feature Importance']):
ax.annotate(round(value, 2), (p.get_width() * 1.005, p.get_y() * 1.005))
通过这种方法,我得到了如下图表,它与原始图表足够接近: