println不使用第二种方法打印我的值

时间:2019-03-03 13:59:28

标签: api regression weka

在这里,我正在做一些与weka api有关的工作,并使用回归进行预测。我有一些代码,可以在main方法中将数据集转换为数值,然后在下一个方法中进行预测。不幸的是,当我运行代码时,这些预测不会打印到控制台上。

在这里非常感谢您的帮助,谢谢。

package weka.api;

import weka.core.Instances;
import weka.core.converters.ArffSaver;
import java.io.File;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NominalToBinary;
import weka.classifiers.functions.SMOreg;
import weka.core.Instance;
import weka.classifiers.bayes.NaiveBayes;
import weka.filters.unsupervised.attribute.Discretize;

public class DiscretizeAttribute {

    public static void main(String[] args) throws Exception {
        //load dataset
        DataSource source = new DataSource("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes.arff");
        Instances dataset = source.getDataSet();
        //set options

        String[] options1 = new String[2];
        options1[0] = "-R";
        options1[1] = "first-last";

        NominalToBinary n2b = new NominalToBinary();
        n2b.setOptions(options1);
        n2b.setInputFormat(dataset);


        Instances newData = Filter.useFilter(dataset, n2b);

        ArffSaver saver = new ArffSaver();
        saver.setInstances(newData);
        saver.setFile(new File("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes1.arff"));
        saver.writeBatch();

        //convert diabetes-test to numeric
        DataSource source1 = new DataSource("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes_test.arff");
        Instances dataset1 = source1.getDataSet();
        //set options

        String[] options2 = new String[2];
        options2[0] = "-R";
        options2[1] = "first-last";
        //options1[2] = "-A";

        NominalToBinary n2b1 = new NominalToBinary();
        n2b1.setOptions(options2);
        n2b1.setInputFormat(dataset1);

        Instances newData1 = Filter.useFilter(dataset1, n2b1);

        ArffSaver saver1 = new ArffSaver();
        saver1.setInstances(newData1);
        saver1.setFile(new File("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes_test1.arff"));
        saver1.writeBatch();

    }

    public void ClassifyInstance() throws Exception {
        DataSource source = new DataSource("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes1.arff");
        Instances trainDataset = source.getDataSet();
        //set class index to the last attribute
        trainDataset.setClassIndex(trainDataset.numAttributes()-1);

        //build model
        SMOreg smo = new SMOreg();
        smo.buildClassifier(trainDataset);
        //output model
        System.out.println(smo);

        //load new dataset
        DataSource source1 = new DataSource("D:/Uni Work/COS5022-B _Coursework 1/Diabetes/diabetes_test1.arff");
        Instances testDataset = source1.getDataSet();

        //set class index to the last attribute
        testDataset.setClassIndex(testDataset.numAttributes()-1);

        //loop through the new dataset and make predictions
        System.out.println("======================");
        System.out.println("Actual Class, SMO Predicted");
        for (int i = 0; i < testDataset.numInstances(); i++) {
            //get class double value for current instance
            double actualValue = testDataset.instance(i).classValue();

            //get instance object of current instances
            Instance newInst = testDataset.instance(i);

            //call classifyInstance, which returns a double value for the class
            double predSMO = smo.classifyInstance(newInst);
            System.out.println(actualValue+", "+predSMO);

        }
    }
}

0 个答案:

没有答案