Search code examples
javaweka

weka GUI and Java code give different results


I'm using weka java API to do a grid search in order to find the optimal parameters for MultilayerPerceptron. However, the RMSE (I'm doing regression here) given by my java code is different from that given by weka GUI. Here is the code:

public class ANN {
/**
 * @param args
 */
public static void main(String[] args) throws Exception{
    DataSource source = new DataSource("/home/yongfeng/ML/Project/choose_openning_price/holdout.arff");
    Instances raw = source.getDataSet();
    int trainSize = (int) Math.round(raw.numInstances()*0.666666666);
    int testSize = raw.numInstances() - trainSize;
    Instances train = new Instances(raw, 0, trainSize);
    Instances test = new Instances(raw, trainSize, testSize);
    train.setClassIndex(0);
    test.setClassIndex(0);
    final int sizeOfSearch = 15;
    double[][] resultsArray = new double[sizeOfSearch][sizeOfSearch];

    for (int i=0;i < sizeOfSearch;i++){
        for (int j=0;j < sizeOfSearch;j++){
            double m = i;
            double k = j;
            double learningRate = (m+1)/1000;
            double momentum = (k+1)/100;
            MultilayerPerceptron ann = new MultilayerPerceptron();
            String options = String.format("-L %f -M %f -N 500 -V 0 -S 0 -E 20 -H a", learningRate, momentum);
            ann.setOptions(weka.core.Utils.splitOptions(options));
            ann.buildClassifier(train);
            Evaluation eval = new Evaluation(train);
            eval.evaluateModel(ann, test);
            double error = eval.rootMeanSquaredError();
            System.out.println("learningRate: " + learningRate + "\tMomentum: " + momentum + "\tError: " + error);
            printOptions(ann.getOptions());
            resultsArray[i][j] = error;
            ann = null;
            eval = null;
            }
        }
            }
}

I even printed out the options in each iteration and they turned out to be the same as those in weka GUI. The attribute to be predicted is the first one, so setClassIndex(0); And used train-test set split to do the evaluation. Can anybody help? Many thanks!


Solution

  • Use weka.jar in weka installation folder in your java code.