Search code examples
pythonscikit-learnclassificationgraphvizdecision-tree

How to read a graphviz decision tree?


I have a decision tree graphviz file that I obtained by using ScikitLearn's export_graphviz function. For sake of simplicity I limited the depth to 3 so I got this output:

digraph Tree {
node [shape=box] ;
0 [label="userAcceleration-magnitude-mean <= 0.973\ngini = 0.875\nsamples = 3878\nvalue = [471, 467, 485, 484, 486, 486, 513, 486]\nclass = Walking"] ;
1 [label="userAcceleration-x-IQR <= 0.073\ngini = 0.834\nsamples = 2881\nvalue = [471, 443, 476, 484, 9, 486, 512, 0]\nclass = Walking"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="rotationRate-z-IQR <= 0.396\ngini = 0.606\nsamples = 1020\nvalue = [466, 80, 43, 2, 0, 429, 0, 0]\nclass = Push-ups"] ;
1 -> 2 ;
3 [label="gini = 0.355\nsamples = 515\nvalue = [5, 74, 28, 2, 0, 406, 0, 0]\nclass = Resting"] ;
2 -> 3 ;
4 [label="gini = 0.164\nsamples = 505\nvalue = [461, 6, 15, 0, 0, 23, 0, 0]\nclass = Push-ups"] ;
2 -> 4 ;
5 [label="rotationRate-magnitude-median <= 0.844\ngini = 0.764\nsamples = 1861\nvalue = [5, 363, 433, 482, 9, 57, 512, 0]\nclass = Walking"] ;
1 -> 5 ;
6 [label="gini = 0.596\nsamples = 974\nvalue = [2, 73, 388, 476, 0, 23, 12, 0]\nclass = Lunges"] ;
5 -> 6 ;
7 [label="gini = 0.571\nsamples = 887\nvalue = [3, 290, 45, 6, 9, 34, 500, 0]\nclass = Walking"] ;
5 -> 7 ;
8 [label="userAcceleration-y-max <= 2.702\ngini = 0.533\nsamples = 997\nvalue = [0, 24, 9, 0, 477, 0, 1, 486]\nclass = Running"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
9 [label="rotationRate-z-IQR <= 2.4\ngini = 0.236\nsamples = 536\nvalue = [0, 22, 6, 0, 466, 0, 1, 41]\nclass = Jump Rope"] ;
8 -> 9 ;
10 [label="gini = 0.553\nsamples = 53\nvalue = [0, 11, 6, 0, 3, 0, 0, 33]\nclass = Running"] ;
9 -> 10 ;
11 [label="gini = 0.08\nsamples = 483\nvalue = [0, 11, 0, 0, 463, 0, 1, 8]\nclass = Jump Rope"] ;
9 -> 11 ;
12 [label="altitude-median <= 5.0\ngini = 0.068\nsamples = 461\nvalue = [0, 2, 3, 0, 11, 0, 0, 445]\nclass = Running"] ;
8 -> 12 ;
13 [label="gini = 0.0\nsamples = 445\nvalue = [0, 0, 0, 0, 0, 0, 0, 445]\nclass = Running"] ;
12 -> 13 ;
14 [label="gini = 0.477\nsamples = 16\nvalue = [0, 2, 3, 0, 11, 0, 0, 0]\nclass = Jump Rope"] ;
12 -> 14 ;
}

Let's concentrate on the first 2 nodes:

0 [label="userAcceleration-magnitude-mean <= 0.973\ngini = 0.875\nsamples = 3878\nvalue = [471, 467, 485, 484, 486, 486, 513, 486]\nclass = Walking"] ;
1 [label="userAcceleration-x-IQR <= 0.073\ngini = 0.834\nsamples = 2881\nvalue = [471, 443, 476, 484, 9, 486, 512, 0]\nclass = Walking"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="rotationRate-z-IQR <= 0.396\ngini = 0.606\nsamples = 1020\nvalue = [466, 80, 43, 2, 0, 429, 0, 0]\nclass = Push-ups"] ;
1 -> 2 ;

This is what I don't understand:

  1. If user-acceleration-magnitude-mean is less or equal than 0.973, then the class is "Walking", otherwise I jump to the node 1, right? or it's the other way around?
  2. How do I read a label which starts with "gini = 0.596"? gini is not a feature of my decision tree, what does it mean?
  3. What about the other values such as nsamples and nvalue? what do they represent?

Solution

  • 1: If user-acceleration-magnitude-mean is less or equal than 0.973, follow True. (This is continued downwards into the tree.)

    2: I googled a bit, and found the "gini coefficient: a statistical measure of the degree of variation represented in a set of values, used especially in analysing income inequality". I think it has been taken out of the economic context, but I don't know for sure if this is the case.

    3: There is a underlying structure in this. samples is the amount of samples that apply for that node. There are 3878 samples for the root, 2881 for the left child and 997 for the right child. Since 2881 + 997 = 3878 I believe that for 2881 samples user-acceleration-magnitude-mean <= 0.973 was True. And respectively False for the other 997 samples.

    The values also have some sort of underlying structure going on. The sum of every value in the value list equals to the amount of samples in that node.