Вот рабочий образец с использованием набора данных радужной оболочки.Обратите внимание, что установка 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