Search code examples
javawekadecision-tree

Weka Decision tree Java to lists


I want to make a decision tree and break it to lists (name , sign , val). I made the tree with this code :

        //Get File
        BufferedReader reader = new BufferedReader(new FileReader(PATH + "TempArffFile.arff"));

        //Get the data
        Instances data = new Instances(reader);
        reader.close();

        //Setting class attribute 
        data.setClassIndex(data.numAttributes() - 1);

        //Make tree
        J48 tree = new J48();
        String[] options = new String[1];
        options[0] = "-U"; 
        tree.setOptions(options);
        tree.buildClassifier(data);

        //Print tree
        System.out.println(tree);

now I need to break it to arrays how can i do that ?

Example : i get this tree :

title <= 1: bad (4.0)
title > 1
|   positionMatch <= 1
|   |   countryCode <= 1: good (3.0/1.0)
|   |   countryCode > 1: bad (8.0/3.0)
|   positionMatch > 1: good (4.0/1.0)

so i want to get 4 lists from that tree:

  • title(<= 1) -> bad
  • title(> 1) -> position(<= 1) -> countryCode(<= 1) -> good
  • title(> 1) -> position(<= 1) -> countryCode(> 1) -> bad
  • title(> 1) -> position(> 1) -> good

How can i do that ?


Solution

  • Not that nice but maybe better then nothing... Maybe it will give u an idea.

        public static void split(String tree){
    
        String[] lines = tree.split("\n");
        List<List<String>> lists = new ArrayList<List<String>>(); 
        for(String line : lines){
            List<String> temp = new ArrayList<String>();
            while(line.indexOf("|") != -1){
                temp.add("|");
                line = line.replaceFirst("\\|", "");
            }
            temp.add(line.trim());
            lists.add(temp);
        }
    
        for(int i = 0; i < 3; i++){
            lists.remove(0);
        }
        for(int i = 0; i < 4; i++){
            lists.remove(lists.size()-1);
        }
        List<String> substitutes = new ArrayList<String>();
    
        for(List<String> list : lists){
            for(int i = 0; i < list.size(); i++){
                if(!list.get(i).contains(":") && !list.get(i).equals("|") && !substitutes.contains(list.get(i))){
                    substitutes.add(list.get(i));
                }
            }
        }
        for(List<String> list : lists){
            for(int i = 0; i < list.size(); i++){
                if(list.get(i).equals("|")){
                    list.set(i, substitutes.get(i));
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for(List<String> list : lists){
            String line = "";
            for(String s : list){
                line = line+" "+s;
            }
            if(line.endsWith(")")){
                sb.append(line+"\n");
            }
        }
        System.out.println(sb.toString());
    }
    

    Input

    J48 unpruned 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

    Output:

    petalwidth <= 0.6: Iris-setosa (50.0)

    petalwidth > 0.6 petalwidth <= 1.7 petallength <= 4.9: Iris-versicolor (48.0/1.0)

    petalwidth > 0.6 petalwidth <= 1.7 petallength > 4.9 petalwidth <= 1.5: Iris-virginica (3.0)

    petalwidth > 0.6 petalwidth <= 1.7 petallength > 4.9 petalwidth > 1.5: Iris-versicolor (3.0/1.0)

    petalwidth > 0.6 petalwidth > 1.7: Iris-virginica (46.0/1.0)