Search code examples
javaclasspathweka

Weka RandomForest setter for -M option


The Weka RandomForest class provides various setters, like setNumIterations(), setNumFeatures(), setMaxDepth(), etc. Is there also a setter to set the minimum number of instances per leaf option of a RandomForest?

I am currently setting it indirectly via setOptions() using -M, but that triggers the "Can't find a permissible class" error (see Weka - Can't find a permissible class) when running the program from the command-line java -jar target/MyApp.jar. However, running from it within the Apache Netbeans IDE does work without any errors, so perhaps some classpath/Maven change might also help?


Solution

  • weka.classifiers.trees.RandomForest is derived from weka.classifiers.meta.Bagging, with the base classifier being accessible via the getClassifier() method. RandomForest uses weka.classifiers.trees.RandomTree as the base classifier which has the -M option that you mentioned.

    So you could probably do something like this to change the minimum number of instances per leaf:

    import weka.classifiers.trees.RandomForest;
    import weka.classifiers.trees.RandomTree;
    import weka.core.Utils;
    
    RandomForest rf = new RandomForest();
    RandomTree rt = (RandomTree) rf.getClassifier();
    rt.setMinNum(5);
    System.out.println(Utils.toCommandLine(rf));
    

    This will output something like this:

    weka.classifiers.trees.RandomForest -P 100 -I 100 -num-slots 1 -K 0 -M 5.0 -V 0.001 -S 1