如何解读决策树'图表结果并找到最丰富的功能?

时间:2016-01-19 07:51:10

标签: python-2.7 machine-learning scikit-learn decision-tree

我正在使用sk-learn python 27并输出一些决策树特征结果。虽然我不确定如何解释结果。起初,我认为这些功能是从信息最丰富到信息量最少的(从上到下)列出的,但是检查它的其他方面的\ n值。如何从输出或使用python线识别前5个最有用的功能?

from sklearn import tree

tree.export_graphviz(classifierUsed2, feature_names=dv.get_feature_names(), out_file=treeFileName)     

# Output below
digraph Tree {
node [shape=box] ;
0 [label="avg-length <= 3.5\ngini = 0.0063\nsamples = 250000\nvalue = [249210, 790]"] ;
1 [label="name-entity <= 2.5\ngini = 0.5\nsamples = 678\nvalue = [338, 340]"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="first-name=wm <= 0.5\ngini = 0.4537\nsamples = 483\nvalue = [168, 315]"] ;
1 -> 2 ;
3 [label="name-entity <= 1.5\ngini = 0.4016\nsamples = 435\nvalue = [121, 314]"] ;
2 -> 3 ;
4 [label="substring=ee <= 0.5\ngini = 0.4414\nsamples = 73\nvalue = [49, 24]"] ;
3 -> 4 ;
5 [label="substring=oy <= 0.5\ngini = 0.4027\nsamples = 68\nvalue = [49, 19]"] ;
4 -> 5 ;
6 [label="substring=im <= 0.5\ngini = 0.3589\nsamples = 64\nvalue = [49, 15]"] ;
5 -> 6 ;
7 [label="lastLetter-firstName=w <= 0.5\ngini = 0.316\nsamples = 61\nvalue = [49, 12]"] ;
6 -> 7 ;
8 [label="firstLetter-firstName=w <= 0.5\ngini = 0.2815\nsamples = 59\nvalue = [49, 10]"] ;
7 -> 8 ;
9 [label="substring=sa <= 0.5\ngini = 0.2221\nsamples = 55\nvalue = [48, 7]"] ;
... many many more lines below

2 个答案:

答案 0 :(得分:5)

  1. 在Python中,您可以使用DecisionTreeClassifier.feature_importances_,根据documentation包含

      

    功能重要性。功能越高,功能越重要。   特征的重要性计算为(标准化)总计   减少该特征带来的标准。它也是众所周知的   作为基尼的重要性[R66]。

    只需对要素重要性执行np.argsort即可获得要素排名(不考虑关系)。

  2. 您可以查看Gini impurity(graphviz输出中的\ngini)以获得第一个想法。越低越好。但是,请注意,如果在多个拆分中使用某个特征,则需要一种方法来组合杂质值。通常,这是通过对给定特征上的所有分割获取平均信息增益(或纯度增益&#39;)来完成的。如果您使用feature_importances_

  3. ,则可以使用此功能

    修改: 我看到问题比我想象的要深刻。 graphviz事物仅仅是树的图形表示。它详细显示了树和树的每个分割。这是树的表示,而不是功能的表示。特征的信息性(或重要性)并不真正适合这种表示,因为它在树的多个节点上累积信息。

    变量classifierUsed2.feature_importances_包含每个要素的重要性信息。如果得到例如[0,0.2,0,0.1,...],则第一个特征的重要性为0,第二个特征的重要性为0.2,第三个特征的重要性为0,第四个特征的重要性为0 0.1,等等。

    按重要性排序功能(最重要的是首先):

    rank = np.argsort(classifierUsed2.feature_importances_)[::-1]
    

    现在rank包含要素的索引,从最重要的一个开始:[1,3,0,1,...]

    想要查看五个最重要的功能吗?

    print(rank[:5])
    

    这会打印索引。什么索引对应什么功能?这是你应该知道的事情,因为你应该构建特征矩阵。有可能,这是有效的:

    print(dv.get_feature_names()[rank[:5]])
    

    或许这个:

    print('\n'.join(dv.get_feature_names()[i] for i in rank[:5]))
    

答案 1 :(得分:2)

正如kazemakase已经指出的那样,你可以使用classifier.feature_importances _获得最重要的功能:

print(sorted(list(zip(classifierUsed2.feature_importances_, dv.get_feature_names()))))

作为附录,我个人更喜欢以下打印结构(从this question/answer修改):

# Print Decision rules:
def print_decision_tree(tree, feature_names):
    left      = tree.tree_.children_left
    right     = tree.tree_.children_right
    threshold = tree.tree_.threshold
    features  = [feature_names[i] for i in tree.tree_.feature]
    value = tree.tree_.value

    def recurse(left, right, threshold, features, node, indent=""):
        if (threshold[node] != -2):
            print (indent+"if ( " + features[node] + " <= " + str(threshold[node]) + " ) {")
            if left[node] != -1:
                recurse (left, right, threshold, features,left[node],indent+"   ")
            print (indent+"} else {")
            if right[node] != -1:
                recurse (left, right, threshold, features,right[node],indent+"   ")
            print (indent+"}")
        else:
            print (indent+"return " + str(value[node]))

    recurse(left, right, threshold, features, 0)

# Use it like this:
print_decision_tree(classifierUsed2, dv.get_feature_names())