The following steps may help you:
Download Weka from http://www.cs.waikato.ac.nz/ml/weka/downloading.html .
From the package, find "Weka.jar" and add it to the project.
Java code snippet
Building a neural classifier
public void simpleWekaTrain(String filepath)
{
try{
FileReader trainreader = new FileReader(filepath);
Instances train = new Instances(trainreader);
train.setClassIndex(train.numAttributes() – 1);
MultilayerPerceptron mlp = new MultilayerPerceptron();
mlp.setLearningRate(0.1);
mlp.setMomentum(0.2);
mlp.setTrainingTime(2000);
mlp.setHiddenLayers("3?);
mlp.buildClassifier(train);
}
catch(Exception ex){
ex.printStackTrace();
}
}
Another way to set parameters,
mlp.setOptions(Utils.splitOptions("-L 0.1 -M 0.2 -N 2000 -V 0 -S 0 -E 20 -H 3?));
Where
L = Learning Rate
M = Momentum
N = Training Time or Epochs
H = Hidden Layers
etc.
Evaluation eval = new Evaluation(train);
eval.evaluateModel(mlp, train);
System.out.println(eval.errorRate());
System.out.println(eval.toSummaryString());
K-Fold
eval.crossValidateModel(mlp, train, kfolds, new Random(1));
/
Instances datapredict = new Instances(
new BufferedReader(
new FileReader(<Predictdatapath>)));
datapredict.setClassIndex(datapredict.numAttributes() – 1);
Instances predicteddata = new Instances(datapredict);
for (int i = 0; i < datapredict.numInstances(); i++) {
double clsLabel = mlp.classifyInstance(datapredict.instance(i));
predicteddata.instance(i).setClassValue(clsLabel);
}
BufferedWriter writer = new BufferedWriter(
new FileWriter(<Output File Path>));
writer.write(predicteddata.toString());
writer.newLine();
writer.flush();
writer.close();