weka GUI和Java代码给出了不同的结果

时间:2013-12-08 22:33:36

标签: java weka

我正在使用weka java API进行网格搜索,以便为MultilayerPerceptron找到最佳参数。但是,我的java代码给出的RMSE(我在这里做回归)与weka GUI给出的不同。这是代码:

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;
            }
        }
            }
}

我甚至在每次迭代中打印出选项,结果与weka GUI中的选项相同。要预测的属性是第一个,所以setClassIndex(0);并使用火车测试集拆分进行评估。有人可以帮忙吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

在java代码中的weka安装文件夹中使用weka.jar。