用特征名称绘制特征重要性

Posted

技术标签:

【中文标题】用特征名称绘制特征重要性【英文标题】:Plot Feature Importance with feature names 【发布时间】:2017-11-14 15:35:51 【问题描述】:

在 R 中有预先构建的函数来绘制随机森林模型的特征重要性。但在 python 中,似乎缺少这种方法。我在matplotlib 中搜索一个方法。

model.feature_importances 给了我以下信息:

array([  2.32421835e-03,   7.21472336e-04,   2.70491223e-03,
         3.34521084e-03,   4.19443238e-03,   1.50108737e-03,
         3.29160540e-03,   4.82320256e-01,   3.14117333e-03])

然后使用以下绘图功能:

>> pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_)
>> pyplot.show()

我得到了一个条形图,但我想得到带有标签的条形图,同时重要性以排序的方式水平显示。我也在探索seaborn,但找不到方法。

【问题讨论】:

您正在寻找barh(水平条形图)。将功能名称传递为tick_labels。 【参考方案1】:

为没有时间浪费的数据科学家提供快速解答:

将特征重要性加载到按列名索引的 pandas 系列中,然后使用它的 plot 方法。对于使用X 训练的分类器model

feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(20).plot(kind='barh')

稍微详细一点的答案,附上一个完整的例子:

假设您使用 pandas 数据框中包含的数据训练模型,如果您将特征重要性加载到 pandas 系列中,这将相当轻松,然后您可以利用它的索引来轻松显示变量名称。 plot 参数kind='barh' 为我们提供了一个水平条形图,但如果您愿意,您可以很容易地用这个参数替换kind='bar' 以获得沿x 轴的特征名称的传统条形图。

nlargest(n) 是 pandas Series 方法,它将返回具有最大 n 值的系列子集。如果您的模型中有很多特征并且您只想绘制最重要的特征,这将非常有用。

使用经典 Kaggle Titanic 数据集的快速完整示例...

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
%matplotlib inline            # don't forget this if you're using jupyter!

X = pd.read_csv("titanic_train.csv")
X = X[['Pclass', 'Age', 'Fare', 'Parch', 'SibSp', 'Survived']].dropna()
y = X.pop('Survived')

model = RandomForestClassifier()
model.fit(X, y)

(pd.Series(model.feature_importances_, index=X.columns)
   .nlargest(4)
   .plot(kind='barh'))        # some method chaining, because it's sexy!

这会给你这个:

【讨论】:

如果我想在 y 轴上翻转这个图怎么办?如,先显示年龄,然后显示票价等 @ArtTatum Call invert_yaxis() - 示例用法pd.Series(model.feature_importances_, index=X.columns).nlargest(4).plot(kind='barh').invert_yaxis() 快速而神奇!【参考方案2】:

不完全确定您在寻找什么。从here 派生一个示例。如评论中所述:如果要自定义功能标签,可以将indices 更改为plt.yticks(range(X.shape[1]), indices) 行的标签列表。

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesClassifier

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
                           n_features=10,
                           n_informative=3,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

# Build a forest and compute the feature importances
forest = ExtraTreesClassifier(n_estimators=250,
                              random_state=0)

forest.fit(X, y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
             axis=0)
indices = np.argsort(importances)

# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.barh(range(X.shape[1]), importances[indices],
       color="r", xerr=std[indices], align="center")
# If you want to define your own labels,
# change indices to a list of labels on the following line.
plt.yticks(range(X.shape[1]), indices)
plt.ylim([-1, X.shape[1]])
plt.show()

【讨论】:

通过将这一行 plt.yticks(range(X.shape[1]), indices) 替换为 plt.yticks(range(X.shape[1]), [feature_names[i] for i in indices]),我得到了出现在 y 轴上的功能名称而不是索引号【参考方案3】:

可以只传递df.columns 作为plt.xticks() 的参数:

plt.bar( range(len(model.feature_importances_)), model.feature_importances_)
plt.xticks(range(len(model.feature_importances_)), train_features.columns)
plt.show()

【讨论】:

以上是关于用特征名称绘制特征重要性的主要内容,如果未能解决你的问题,请参考以下文章

如何在python中绘制随机森林的特征重要性

使用排列重要性绘制前 n 个特征

XGBoost三种特征重要性计算方法对比

从 Sklearn 管道中提取具有特征名称的特征重要性

“特征重要性”的“单热编码”变量的显示名称

获取转换后用于分类的最重要特征的名称