0
votes

I had use weka to classifier the data and get the result via

System.out.println(classifier.toString())

and get the classifier tree like this:

petalwidth <= 0.6: Iris-setosa (50.0)
petalwidth > 0.6
|   petalwidth <= 1.7
|   |   petallength <= 4.9: Iris-versicolor (48.0/1.0)
|   |   petallength > 4.9
|   |   |   petalwidth <= 1.5: Iris-virginica (3.0)
|   |   |   petalwidth > 1.5: Iris-versicolor (3.0/1.0)
|   petalwidth > 1.7: Iris-virginica (46.0/1.0)

My question is how to parse the tree structure to the rule? Or is there any method can get the result's components?

Thanks!

1
What does classifier.toString() return? - Smutje
J48 pruned tree ------------------ petalwidth <= 0.6: Iris-setosa (50.0) petalwidth > 0.6 | petalwidth <= 1.7 | | petallength <= 4.9: Iris-versicolor (48.0/1.0) | | petallength > 4.9 | | | petalwidth <= 1.5: Iris-virginica (3.0) | | | petalwidth > 1.5: Iris-versicolor (3.0/1.0) | petalwidth > 1.7: Iris-virginica (46.0/1.0) Number of Leaves : 5 Size of the tree : 9 - user2458433
And what do you want to do with this (kind of) "tree"? - Smutje
I want to get the "rule" of this tree. For example, I want to get a array [(petalwidth),(<=),(0.6)] or other structure of the data that I can use this result to create the decision tree's rule via programming. Thanks. - user2458433

1 Answers

0
votes

Answering exactly the question, You can use the embedded method toSource() to parse the tree structure to the rule. Answering your subquestion, if I understood it, there is two methods that can get the result's components: prefix() and graph().

Another possibility is to extend the class or change the sources to access the internal data structures.

Yet another option is to use a third party library like this: J48Parser

Output from each method:

graph --------------
digraph J48Tree {
N0 [label="V2" ]
N0->N1 [label="<= 0.561"]
N1 [label="V2" ]
N1->N2 [label="<= -0.857"]
N2 [label="1 (1426.0/427.0)" shape=box style=filled ]
N1->N3 [label="> -0.857"]
N3 [label="V1" ]
N3->N4 [label="<= 0.155"]
N4 [label="2 (1149.0/180.0)" shape=box style=filled ]
N3->N5 [label="> 0.155"]
N5 [label="1 (1000.0/490.0)" shape=box style=filled ]
N0->N6 [label="> 0.561"]
N6 [label="1 (1716.0/487.0)" shape=box style=filled ]
}


prefix ---------------
[V2: <= 0.561,
 > 0.561[V2: <= -0.857,
 > -0.857[1 (1426.0/427.0)][V1: <= 0.155,
 > 0.155[2 (1149.0/180.0)][1 (1000.0/490.0)]]][1 (1716.0/487.0)]]

toSource ---------------
class bla {

  public static double classify(Object[] i)
    throws Exception {

    double p = Double.NaN;
    p = bla.N37374a5e0(i);
    return p;
  }
  static double N37374a5e0(Object []i) {
    double p = Double.NaN;
    if (i[1] == null) {
      p = 1;
    } else if (((Double) i[1]).doubleValue() <= 0.561) {
    p = bla.N4671e53b1(i);
    } else if (((Double) i[1]).doubleValue() > 0.561) {
      p = 0;
    } 
    return p;
  }
  static double N4671e53b1(Object []i) {
    double p = Double.NaN;
    if (i[1] == null) {
      p = 0;
    } else if (((Double) i[1]).doubleValue() <= -0.857) {
      p = 0;
    } else if (((Double) i[1]).doubleValue() > -0.857) {
    p = bla.N2db7a79b2(i);
    } 
    return p;
  }
  static double N2db7a79b2(Object []i) {
    double p = Double.NaN;
    if (i[0] == null) {
      p = 1;
    } else if (((Double) i[0]).doubleValue() <= 0.155) {
      p = 1;
    } else if (((Double) i[0]).doubleValue() > 0.155) {
      p = 0;
    } 
    return p;
  }
}