【问题标题】:XGBoost Plot Importance F-Score Values >100XGBoost 绘图重要性 F 值 >100
【发布时间】:2020-10-05 14:33:46
【问题描述】:

我已经为我的模型中的所有特征绘制了 XGBoost 特征重要性,如下图所示。但是你可以看到图中的 F Score 值没有被归一化(不在 0 到 100 的范围内)。如果您知道为什么会这样,请告诉我。我需要在 plot_importance 函数中传递任何参数来进行标准化吗?

【问题讨论】:

  • 我对 plot_importance 有同样的问题!你能解决问题吗?谢谢!

标签: scikit-learn xgboost xgbclassifier


【解决方案1】:

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))

通过这种方法,我得到了如下图表,它与原始图表足够接近:

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 2017-07-16
    • 2019-01-25
    • 1970-01-01
    • 2020-11-13
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多