Java Weka: How to specify split percentage?
In the UI class ClassifierPanel
's method startClassifier()
, I found the following code:
// Percent split
int trainSize = (int) Math.round(inst.numInstances() * percent
/ 100);
int testSize = inst.numInstances() - trainSize;
Instances train = new Instances(inst, 0, trainSize);
Instances test = new Instances(inst, trainSize, testSize);
so after randomizing your dataset...
trainingSet.randomize(new java.util.Random(0));
... I suggest you split your trainingSet
in the same way:
int trainSize = (int) Math.round(trainingSet.numInstances() * 0.8);
int testSize = trainingSet.numInstances() - trainSize;
Instances train = new Instances(trainingSet, 0, trainSize);
Instances test = new Instances(trainingSet, trainSize, testSize);
then use Classifier#buildClassifier(Instances data)
to train the classifier with 80% of your set instances:
model.buildClassifier(train);
UPDATE: thanks to @ChengkunWu's answer, I added the randomizing step above.
You might also want to randomize the split as well.
data.randomize(new java.util.Random(0));