Re: Level Order Tree Traversal of C4.5 (J48) Tree

Eibe Frank <[email protected]>
Newsgroups gmane.comp.ai.weka
Message-ID <CADehzLXjuVO-VNdj-jpwm9swJVie_yBOC6U4L38L++3mfv748A@mail.gmail.com>
Here is some code (again, written in the Groovy console) that outputs the
tree in breadth-first order.

Cheers,
Eibe

package weka.classifiers.trees.j48;

import weka.core.Instances;
import weka.classifiers.trees.J48;

class MyJ48 extends J48 {

 public static String breadthFirst(ClassifierTree tree){
  StringBuffer buffer = new StringBuffer();
  Queue<ClassifierTree> fifoQueue = new LinkedList<>();
  fifoQueue.add(tree);
  while (!fifoQueue.isEmpty()) {
   ClassifierTree firstElement = fifoQueue.pop();
   if (!firstElement.isLeaf()) {
    for (int i = 0; i < firstElement.m_sons.length; i++) {
     buffer.append(firstElement.m_localModel.leftSide(firstElement.m_train)
+ firstElement.m_localModel.rightSide(i, firstElement.m_train) + "\n");
     ClassifierTree currentSuccessor = firstElement.m_sons[i];
     fifoQueue.add(currentSuccessor);
    }
   } else {
    buffer.append(": " + firstElement.m_localModel.dumpLabel(0,
firstElement.m_train) + "\n");
   }
  }
  return buffer.toString();
 }

 public static void main(String[] args) {
  Instances data = (new
weka.core.converters.ConverterUtils.DataSource("C:/Users/eibe/datasets/UCI/anneal.arff")).getDataSet();
  data.setClassIndex(data.numAttributes() - 1);
  J48 j48 = new J48();
  j48.buildClassifier(data);
  System.out.println(j48);
  System.out.println("Breadth-first traversal: \n\n" +
MyJ48.breadthFirst(j48.m_root));
 }
}

On Tue, Jun 29, 2021 at 8:56 PM <[email protected]> wrote:

> I include the code together with some changes. First the method:
>
> private void dumpTreeLevelOrder(Instances root, StringBuffer text){
>
>     if (root==null)
>       return;
>
>     Queue<Instances> q =new LinkedList<>();
>     q.add(root);
>     int height=1;
>     while(!q.isEmpty()){
>       for (int k=0; k<height; k++)
>         text.append("|  ");
>       int n = q.size();
>       while (n>0){
>         Instances p = q.peek();
>         q.remove();
>         text.append(m_localModel.leftSide(p));
>         text.append(m_localModel.rightSide(1, p)+" ");
>         for (int i=0; i<m_sons.length; i++)
>           q.add(m_sons[i].m_train);
>         n--;
>       }
>       height++;
>       text.append("\n");
>     }
>
>   }
>
> And next the call from the toString() method:
>
> dumpTreeLevelOrder(m_train, text);
>
> Any help would be valuable!
>
> Thank you in advance,
>
> Spyros Halkidis
> _______________________________________________
> Wekalist mailing list -- [email protected]
> Send posts to [email protected]
> To unsubscribe send an email to [email protected]
> To subscribe, unsubscribe, etc., visit
> https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
> List etiquette:
> http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
>

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.