python特征重要性_特征重要性--feature_importance

feature_importance的特征重要性

There are indeed several ways to get feature "importances". As often, there is no strict consensus about what this word means.

In scikit-learn, we implement the importance as described in [1] (often cited, but unfortunately rarely read...). It is sometimes called "gini importance" or "mean decrease impurity" and is defined as the total decrease in node impurity (weighted by the probability of reaching that node (which is approximated by the proportion of samples reaching that node)) averaged over all trees of the ensemble.

In the literature or in some other packages, you can also find feature importances implemented as the "mean decrease accuracy".

Basically, the idea is to measure the decrease in accuracy on OOB data when you randomly permute the values for that feature. If the decrease is low, then the feature is not important, and vice-versa.

(Note that both algorithms are available in the randomForest R package.)

[1]: Breiman, Friedman, "Classification and regression trees", 1984.

其实sklearn的作者说并没有给出feature importance的具体定义。

feature importance有两种常用实现思路:

(1) mean decrease in node impurity:

feature importance is calculated by looking at the splits of each tree.

The importance of the splitting variable is proportional to the improvement to the gini index given by thatsplit and it is accumulated (for each variable) over all the trees in the forest.

就是计算每棵树的每个划分特征在划分准则(gini或者entropy)上的提升,然后对聚合所有树得到特征权重

(2) mean decrease in accuracy:

This method, proposed in the original paper, passes the OOB samples down the tree and records prediction accuracy.

A variable is then selected and its values in the OOB samples are randomly permuted. OOB samples are passed down the tree and accuracy is computed again.

A decrease in accuracy obtained by this permutation is averaged over all trees for each variable and it provides the importance of that variable (the higher the decreas the higher the importance).

简单来说,如果该特征非常的重要,那么稍微改变一点它的值,就会对模型造成很大的影响。

自己造数据太麻烦,可以直接在OOB数据集对该维度的特征数据进行打乱,重新训练测试,打乱前的准确率减去打乱后的准确率就是该特征的重要度。该方法又叫permute。

以random forest为例

以random forest为例,feature importance特性有助于模型的可解释性。简单考虑下,就算在解释性很强的决策树模型中,如果树过于庞大,人类也很难解释它做出的结果。

随机森林通常会有上百棵树组成,更加难以解释。好在我们可以找到那些特征是更加重要的,从而辅助我们解释模型。更加重要的是可以剔除一些不重要的特征,降低杂讯。比起pca降维后的结果,更具有人类的可理解性。

sklearn中实现的是第一种方法【mean decrease in node impurity】实现的。使用iris数据集我们来看下效果。

首先,对于单棵决策树,权重是怎么计算的呢?

iris = load_iris()

X = iris.data

y = iris.target

dt = DecisionTreeClassifier(criterion='entropy', max_leaf_nodes=3)

dt.fit(X, y)

使用Graphviz画出生成的决策树

sklearn中tree的代码是用Cython编写的,具体这部分的源码在compute_feature_importances方法中

根据上面生成的树,我们计算特征2和特征3的权重

特征2: 1.585*150 - 0*50 - 1.0*100 = 137.35

特征3: 1.0*100 - 0.445*54 - 0.151*46 = 69.024

归一化之后得到[0, 0, 0.665, 0.335] 我们算的结果和sklearn输出的结果相同。

得到每课树的特征重要度向量之后,就加和平均得到结果,具体代码如下:

def feature_importances_(self):"""Return the feature importances (the higher, the more important the

feature).

Returns-------feature_importances_ : array, shape=[n_features]""" check_is_fitted(self, 'n_outputs_')if self.estimators_ is None or len(self.estimators_) == 0:

raise ValueError("Estimator not fitted,"

"call `fit` before `feature_importances_`.")

all_importances= Parallel(n_jobs=self.n_jobs, backend="threading")(

delayed(getattr)(tree,'feature_importances_')for tree inself.estimators_)

returnsum(all_importances) / len(self.estimators_)

View Code

利用permutation importance挑选变量

为什么引入置换重要性:

决策树适合于寻找也可以解释的非线性预测规则,尽管它们的不稳定性和缺乏平滑性引起了人们的关注(Hastie等,2001)。RandomForest(RF; Breiman,2001)分类器旨在克服这些问题,并且由于将决策树的可解释性与现代学习算法(例如人工神经网络和SVM)的性能相结合而受到广泛欢迎。RF的作者提出了两种用于特征排名的度量,即变量重要性(VI)和基尼重要性(GI)。最近的一项研究表明,如果预测变量是分类的,则这两种方法都偏向于采用更多类别的变量(Strobl等,2007)。这篇文章的作者将偏爱归因于使用bootstrap采样和吉尼分裂准则来训练分类树和回归树(CART; Breiman等,1984)。在文献中,多年来已经报道了由基尼系数引起的偏差(Bourguignon,1979; Pyatt等,1980)。),并且通常不仅会影响分类变量,还会影响分组变量(即,将变量聚类的值分成多个独立的组,例如多峰高斯分布)。在生物学中,预测因子通常具有分类或分组的值(例如,微阵列和序列突变)。Strobl等。(2007)提出了一种新的算法(cforest),用于基于条件推理树构建RF模型(Hothorn等,2006),并计算可校正偏差的VI值。

关于生物学数据的学习通常具有大量特征和少量可用样本的特征。通常的做法是在模型拟合之前滤除不重要的特征,例如,通过拒绝与结果最少相关的特征。互信息(MI)是在这种情况下经常使用的一种关联度量(Guyon和Elisseeff,2003年)。它与基尼指数密切相关,并且事实证明,它也偏向于具有更多类别的变量(Achard等,2005)。

们在构建树类模型(XGBoost、LightGBM等)时,如果想要知道哪些变量比较重要的话。可以通过模型的feature_importances_方法来获取特征重要性。

例如LightGBM的feature_importances_可以通过特征的分裂次数或利用该特征分裂后的增益来衡量。一般情况下,不同的衡量准则得到的特征重要性顺序会有差异。

一般是通过多种评价标准来交叉选择特征,当一个特征在不同的评价标准下都是比较重要的,那么该特征对label有较好的预测能力。

为大家介绍一种评价特征重要性的方法:PermutationImportance。

文档对该方法介绍如下:

eli5 provides a way to compute feature importances for any black-box estimator by measuring how score decreases when a feature is not available;

the method is also known as “permutation importance” or “Mean Decrease Accuracy (MDA)”.

若将一个特征置为随机数,模型效果下降很多,说明该特征比较重要;反之则不是。

下面为大家举一个简单的例子,我们利用不同模型来挑选变量(RF、LightGBM、LR)。并挑选出来重要性排在前30的变量(总变量200+)进行建模。

参考博客:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值