如何在Weka中使用.arff文件预测未标记的数据?

时间:2019-05-25 00:01:53

标签: java c# classification weka

我接受了培训并测试了“ labeled.arff”文件。然后,我建立一个分类器并写入“ modelFile.model”文件。

我有一个“ unlabeled.arff”文件,每行中的最后一个属性是“?”。

如何使用Java或C#进行预测? 我有一些代码,但这是不对的,总是给我相同的预测。

谢谢

// Write to Model
public static void Classify()
{
                Instances train = new Instances(new java.io.FileReader(dirTrain + "labeled.arff"));
                Instances test = new Instances(new java.io.FileReader(dirTest + "labeled.arff"));

                train.setClassIndex(train.numAttributes() - 1);
                test.setClassIndex(test.numAttributes() - 1);

                // train Classifier
                Classifier cl = new J48();

                // Randomize the order of the instances in the dataset
                weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
                myRandom.setInputFormat(train);
                train = weka.filters.Filter.useFilter(train, myRandom);

                // Build the classifier
                cl.buildClassifier(train);

                // evaluate classifier and print some statistics
                Evaluation eval = new Evaluation(train);
                eval.evaluateModel(cl, test);
                Console.WriteLine(eval.toSummaryString("\nResults Decision Tree\n======\n", false));

                SerializationHelper.write(dirModel + "modelFile.model", cl);

}

// Make predictions
public void Predictions()
{
            Classifier cl = (Classifier)SerializationHelper.read(dirModel + "modelFile.model");

            // load unlabeled data
            Instances unlabeled = new Instances(new java.io.FileReader(pathFeatures + "unlabeled.arff"));

            // set class attribute
            unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

            // create copy
            Instances labeled = new Instances(unlabeled);

            // label instances
            for (int i = 0; i < unlabeled.numInstances(); i++)
            {
                double clsLabel = cl.classifyInstance(unlabeled.instance(i));
                labeled.instance(i).setClassValue(clsLabel);
            }

            int numCorrect = 0;
            for (int i = 0; i < unlabeled.numInstances(); i++)
            {
                double pred = cl.classifyInstance(unlabeled.instance(i));
                Console.Write("ID: " + unlabeled.instance(i).value(i));
                //Console.Write(", actual: " + unlabeled.classAttribute().value((int)unlabeled.instance(i).classValue()));
                Console.WriteLine(", predicted: " + unlabeled.classAttribute().value((int)pred));
            }

            Console.WriteLine("Correct predictions: " + numCorrect);

}

0 个答案:

没有答案