1
votes

If I am currently using a Weka decision tree (or other) classifier as follows in my Java code:

// Get training and testing data.
Instances train = new Instances ("from training file");
train.setClassIndex(train.numAttributes() - 1);
Instances test = new Instances ("from testing file");        
test.setClassIndex(test.numAttributes() - 1);

// Set classifier.
Object obj = Class.forName("weka.classifiers.trees.J48").newInstance();
Classifier cls = (Classifier) Class.forName("weka.classifiers.trees.J48").cast(obj);

// Set parameters for classifier.      
String options = ("-C 0.05 -M 2");        
String[] optionsArray = options.split(" ");        
cls.setOptions(optionsArray);        

// Train classifier.    
cls.buildClassifier(train);        
Evaluation eval = new Evaluation(train);

// Test trained classifier.
eval.evaluateModel(cls, test);

What happens if I want to use a meta classifier, e.g. bagging, to try to boost results? In Weka's Explorer, if I use bagging with my training and testing data, the parameter string for the classifier is:

weka.classifiers.meta.Bagging -P 100 -S 1 -num-slots 1 -I 10 -W weka.classifiers.trees.J48 -- -C 0.25 -M 2

Does anyone know what a code representation of this might be?

Ideally, I want to store the classes of the classifier and meta classifier in a database table, i.e. so line:

Object obj = Class.forName("weka.classifiers.trees.J48").newInstance(); 

becomes:

Object obj = Class.forName(classifier.getWekaClass()).newInstance();

And where the parameters could be listed in a database table as well to make them easy to change if I swap over classifiers from J48 to NB.

I believe that this is what I'm looking for but...

http://weka.wikispaces.com/Use+WEKA+in+your+Java+code#Attribute selection-Meta-Classifier

1

1 Answers

0
votes

The javadoc suggests that there is a method setClassifier() that you would use to set the classifier you want to use. Beyond that, it's simply a matter of instantiating the class and setting the options accordingly.

You can of course store the class names in a database and use them as an your example. Storing parameters would be a bit trickier as the number and type would vary with each classifier -- you would have to provide a wrapper that can serialise and deserialise them properly.