使用WEKA(Java API)获取预测错误

问题描述 投票:-1回答:1

实际上,我已经开发了一个使用RandomTree分类器进行预测的项目。您可以输入不同的参数,算法将告诉您可以是“是”或“否”的响应。

我希望这个答案是“是”或“否”和概率(百分比预测或预测错误)。例如:

是的 - 0.754%

不 - 0.64%

我怎样才能用WEKA做到这一点?有没有教程或参考看到它?

这是我的RandomTree分类器的代码:

RandomTree cls = new RandomTree();
        cls = (RandomTree) weka.core.SerializationHelper.read("randomTreeSerializadoEnfermedad.model");
        System.out.println("Muestro la profundidad al cargar el modelo: " + cls);

        BufferedReader breader = new BufferedReader(new FileReader(patharff));
        Instances originalTrain = null;
        originalTrain = new Instances(breader);
        originalTrain.setClassIndex(originalTrain.numAttributes() - 1);
        //originalTrain.set
        System.out.println("Muestro lo de numAttributes para ver qué es:" + originalTrain.attribute(13));
                int s1 = 0;

        // perform your prediction
        double value = cls.classifyInstance(originalTrain.instance(s1));

        // get the prediction percentage or distribution
        System.out.println("La instancia sobre la que se van a predecir los datos es: " + originalTrain.instance(s1));
        double[] percentage = cls.distributionForInstance(originalTrain.instance(s1));
        System.out.println("Percentage: "+percentage);

        // get the name of the class value
        String prediction = originalTrain.classAttribute().value((int) value);
        //originalTrain.classAttribute().value((int) value).
        System.out.println("xxx: " + originalTrain.classAttribute());

        System.out.println("The predicted value of instance " + Integer.toString(s1) + ": " + prediction);
        String distribution = "";
        for (int i = 0; i < percentage.length; i = i + 1) {
            if (i == value) {
                distribution = distribution + "*" + Double.toString(percentage[i]) + ",";
            } else {
                distribution = distribution + Double.toString(percentage[i]) + ",";
            }
        }
        distribution = distribution.substring(0, distribution.length() - 1);

        System.out.println("Distribution:" + distribution);

        String finalpred = "The predicted value of instance " + Integer.toString(s1) + ": " + prediction +"\n"
                + "Distribution:" + distribution;
        return finalpred;

这是我用这段代码得到的输出:

Output

如何获得预测错误?

先感谢您!

java machine-learning deep-learning weka prediction
1个回答
0
投票

以下是使用虹膜数据集的工作示例。请注意,设置numFolds> 0(cls.setNumFolds(2);)以获得0和1之外的概率。

package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.StringJoiner;

import weka.classifiers.trees.RandomTree;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

public class WekaTest {

    public static void main(String[] args) throws Exception {
        DataSource dataSource = new DataSource("C:\\Program Files\\Weka-3-8\\data\\iris.arff");
        Instances instances = dataSource.getDataSet();

        if (instances.classIndex() == -1) {
            instances.setClassIndex(instances.numAttributes() - 1);
        }

        instances.randomize(new Random(1));

        int trainSize = (int) Math.round(instances.numInstances() * 66 / 100);
        int testSize = instances.numInstances() - trainSize;
        Instances train = new Instances(instances, 0, trainSize);
        Instances test = new Instances(instances, trainSize, testSize);

        RandomTree cls = new RandomTree();
        cls.setNumFolds(2);
        cls.buildClassifier(train);

        Attribute classAttribute = train.classAttribute();
        ArrayList<Object> classNames = Collections.list(classAttribute.enumerateValues());

        for (int i = 0; i < testSize; i++) {
            Instance instance = test.get(i);
            // perform your prediction
            double value = cls.classifyInstance(instance);
            double[] percentage = cls.distributionForInstance(instance);
            int predictedIndex = (int) value;

            StringJoiner sj = new StringJoiner(", ");
            for (int j = 0; j < percentage.length; j++) {
                sj.add(String.format("%s%s %.2f", classNames.get(j), j == predictedIndex ? "*" : "", percentage[j]));
            }

            System.out.println("Distribution for index " + i + ": " + sj.toString());
        }
    }
}

这输出:

Distribution for index 0: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 1: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 2: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 3: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 4: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 5: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 6: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 7: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 8: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 9: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 10: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 11: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 12: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 13: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 14: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 15: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 16: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 17: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 18: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 19: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 20: Iris-setosa 0.00, Iris-versicolor 0.00, Iris-virginica* 1.00
Distribution for index 21: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 22: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 23: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 24: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 25: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 26: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 27: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 28: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 29: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 30: Iris-setosa 0.00, Iris-versicolor 0.00, Iris-virginica* 1.00
Distribution for index 31: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 32: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 33: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 34: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 35: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 36: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 37: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 38: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 39: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 40: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 41: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 42: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 43: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 44: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 45: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 46: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 47: Iris-setosa* 1.00, Iris-versicolor 0.00, Iris-virginica 0.00
Distribution for index 48: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
Distribution for index 49: Iris-setosa 0.00, Iris-versicolor* 1.00, Iris-virginica 0.00
Distribution for index 50: Iris-setosa 0.00, Iris-versicolor 0.13, Iris-virginica* 0.88
© www.soinside.com 2019 - 2024. All rights reserved.