scikit learn - 在决策树中进行特征重要性计算

时间:2018-03-08 10:00:22

标签: python scikit-learn decision-tree feature-selection

我试图了解如何计算sci-kit学习中的决策树的特征重要性。之前已经问过这个问题,但我无法重现算法提供的结果。

例如:

from StringIO import StringIO

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree.export import export_graphviz
from sklearn.feature_selection import mutual_info_classif

X = [[1,0,0], [0,0,0], [0,0,1], [0,1,0]]

y = [1,0,1,1]

clf = DecisionTreeClassifier()
clf.fit(X, y)

feat_importance = clf.tree_.compute_feature_importances(normalize=False)
print("feat importance = " + str(feat_importance))

out = StringIO()
out = export_graphviz(clf, out_file='test/tree.dot')

导致特征重要性:

feat importance = [0.25       0.08333333 0.04166667]

并给出以下决策树:

decision tree

现在,answer对类似的问题表明重要性计算为

formula_a

其中G是节点杂质,在这种情况下是基尼杂质。据我所知,这是杂质减少。但是,对于功能1,这应该是:

formula_b

answer表明重要性由到达节点的概率加权(通过到达该节点的样本比例近似)。同样,对于特征1,这应该是:

formula_c

两个公式都提供了错误的结果。如何正确计算要素重要性?

2 个答案:

答案 0 :(得分:12)

我认为功能重要性取决于实现,所以我们需要查看scikit-learn的文档。

  

功能重要性。功能越高,功能越重要。特征的重要性计算为该特征带来的标准的(标准化的)总减少量。它也被称为基尼重要性

减少或加权信息增益定义为:

  

加权杂质减少方程式如下:

     

N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity)

     

其中N是样本总数,N_t是当前节点的样本数,N_t_L是左子项中的样本数,N_t_R是右子项中的样本数。

http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier

由于每种特征在您的情况下使用一次,因此特征信息必须等于上面的等式。

对于X [2]:

feature_importance = (4 / 4) * (0.375 - (0.75 * 0.444)) = 0.042

对于X [1]:

feature_importance = (3 / 4) * (0.444 - (2/3 * 0.5)) = 0.083

对于X [0]:

feature_importance = (2 / 4) * (0.5) = 0.25

答案 1 :(得分:2)

单个特征可以在树的不同分支中使用,那么特征的重要性在于它对减少杂质的总贡献。

feature_importance += number_of_samples_at_parent_where_feature_is_used\*impurity_at_parent-left_child_samples\*impurity_left-right_child_samples\*impurity_right

杂质是基尼/熵值

normalized_importance = feature_importance/number_of_samples_root_node(total num of samples)

在上面,例如:

feature_2_importance = 0.375*4-0.444*3-0*1 = 0.16799 , 
normalized = 0.16799/4(total_num_of_samples) = 0.04199

如果在其他分支中使用了feature_2,请在每个这样的父节点上计算其重要性并汇总这些值。

由于我们使用的是图表中的截断值,因此计算出的要素重要性与库返回的要素重要性有所不同。

相反,我们可以使用分类器的'tree_'属性访问所有必需的数据,该属性可用于探查所使用的特征,阈值,杂质,每个节点上没有样本等。

例如:clf.tree_.feature给出使用的功能列表。负值表示它是叶节点。

类似地,clf.tree_.children_left/right将左右孩子的索引分配到clf.tree_.feature

使用上面的方法遍历树,并在clf.tree_.impurity & clf.tree_.weighted_n_node_samples中使用相同的索引来获取基尼/熵值以及每个节点及其子节点处的样本数。

def dt_feature_importance(model,normalize=True):

    left_c = model.tree_.children_left
    right_c = model.tree_.children_right

    impurity = model.tree_.impurity    
    node_samples = model.tree_.weighted_n_node_samples 

    # Initialize the feature importance, those not used remain zero
    feature_importance = np.zeros((model.tree_.n_features,))

    for idx,node in enumerate(model.tree_.feature):
        if node >= 0:
            # Accumulate the feature importance over all the nodes where it's used
            feature_importance[node]+=impurity[idx]*node_samples[idx]- \
                                   impurity[left_c[idx]]*node_samples[left_c[idx]]-\
                                   impurity[right_c[idx]]*node_samples[right_c[idx]]

    # Number of samples at the root node
    feature_importance/=node_samples[0]

    if normalize:
        normalizer = feature_importance.sum()
        if normalizer > 0:
            feature_importance/=normalizer

    return feature_importance

此函数将返回与clf.tree_.compute_feature_importances(normalize=...)返回的值完全相同的值

根据功能的重要性对其进行排序

features = clf.tree_.feature[clf.tree_.feature>=0] # Feature number should not be negative, indicates a leaf node
sorted(zip(features,dt_feature_importance(clf,False)[features]),key=lambda x:x[1],reverse=True)
相关问题