XGBoost plot_importance 不显示特征名称

XGBoost plot_importance doesn't show feature names

我将 XGBoost 与 Python 一起使用,并使用调用 DMatrix 数据的 XGBoost train() 函数成功训练了一个模型。该矩阵是从 Pandas 数据框创建的,该数据框具有列的特征名称。

Xtrain, Xval, ytrain, yval = train_test_split(df[feature_names], y, \
                                    test_size=0.2, random_state=42)
dtrain = xgb.DMatrix(Xtrain, label=ytrain)

model = xgb.train(xgb_params, dtrain, num_boost_round=60, \
                  early_stopping_rounds=50, maximize=False, verbose_eval=10)

fig, ax = plt.subplots(1,1,figsize=(10,10))
xgb.plot_importance(model, max_num_features=5, ax=ax)

我现在想使用 xgboost.plot_importance() 函数查看特征重要性,但生成的绘图不显示特征名称。相反,功能列为 f1f2f3 等,如下所示。

我认为问题在于我将原始 Pandas 数据框转换为 DMatrix。如何正确关联特征名称以便特征重要性图显示它们?

您想在创建 xgb.DMatrix

时使用 feature_names 参数
dtrain = xgb.DMatrix(Xtrain, label=ytrain, feature_names=feature_names)

train_test_split 会将数据帧转换为不再包含列信息的 numpy 数组。

您可以按照@piRSquared 的建议将特征作为参数传递给 DMatrix 构造函数。或者,您可以将从 train_test_split 返回的 numpy 数组转换为 Dataframe,然后使用您的代码。

Xtrain, Xval, ytrain, yval = train_test_split(df[feature_names], y, \
                                    test_size=0.2, random_state=42)

# See below two lines
X_train = pd.DataFrame(data=Xtrain, columns=feature_names)
Xval = pd.DataFrame(data=Xval, columns=feature_names)

dtrain = xgb.DMatrix(Xtrain, label=ytrain)

我在玩 feature_names 时发现的另一种方法。在玩它的同时,我写了这个适用于我目前 运行.

的 XGBoost v0.80
## Saving the model to disk
model.save_model('foo.model')
with open('foo_fnames.txt', 'w') as f:
    f.write('\n'.join(model.feature_names))

## Later, when you want to retrieve the model...
model2 = xgb.Booster({"nthread": nThreads})
model2.load_model("foo.model")

with open("foo_fnames.txt", "r") as f:
    feature_names2 = f.read().split("\n")

model2.feature_names = feature_names2
model2.feature_types = None
fig, ax = plt.subplots(1,1,figsize=(10,10))
xgb.plot_importance(model2, max_num_features = 5, ax=ax)

所以这是单独保存 feature_names 并在以后添加回来。由于某些原因 feature_types 也需要初始化,即使值为 None.

如果您使用的是 scikit-learn 包装器,则需要访问底层 XGBoost Booster 并在其上设置功能名称,而不是 scikit 模型,如下所示:

model = joblib.load("your_saved.model")
model.get_booster().feature_names = ["your", "feature", "name", "list"]
xgboost.plot_importance(model.get_booster())

使用 Scikit-Learn 包装器接口 "XGBClassifier",plot_importance 返回 class "matplotlib Axes"。所以我们可以使用 axes.set_yticklabels.

plot_importance(model).set_yticklabels(['feature1','feature2'])

如果用

训练
model = XGBClassifier(
    max_depth = 8, 
    learning_rate = 0.25, 
    n_estimators = 50, 
    objective = "binary:logistic",
    n_jobs = 4
)

# x, y are pandas DataFrame
model.fit(train_data_x, train_data_y)

您可以 model.get_booster().get_fscore() 获取特征名称和特征重要性作为 python dict

您应该在实例化 XGBoost 分类器时指定 feature_names:

xgb = xgb.XGBClassifier(feature_names=feature_names)

请注意,如果将 xgb 分类器包装在对列执行任何选择(例如 VarianceThreshold)的 sklearn 管道中,则 xgb 分类器在尝试拟合或转换时将失败。

你也可以在没有 DMatrix 的情况下使代码更简单。列名用作标签:

from xgboost import XGBClassifier, plot_importance
model = XGBClassifier()
model.fit(Xtrain, ytrain)
plot_importance(model)