machine-learning - 类别的 WEKA 分类可能性

标签 machine-learning weka

我想知道 WEKA 中是否有一种方法可以输出一些分类的“最佳猜测”。

我的场景是:例如,我通过交叉验证对数据进行分类,然后在 weka 的输出中我得到类似的结果:这些是该实例分类的 3 个最佳猜测。我想要的是,即使一个实例没有正确分类,我也会得到该实例的 3 或 5 个最佳猜测的输出。

示例:

类别:A、B、C、D、E 实例:1...10

输出将是: 实例 1 有 90% 的可能性属于 A 类,75% 的可能性属于 B 类,60% 的可能性属于 C 类..

谢谢。

最佳答案

Weka 的 API 有一个名为 Classifier.distributionForInstance() 的方法,可用于获取分类预测分布。然后,您可以通过降低概率对分布进行排序,以获得前 N 个预测。

下面是一个打印出的函数: (1) 测试实例的真实标签; (2)来自classifyInstance()的预测标签; (3) 来自 distributionForInstance() 的预测分布。我已将其与 J48 一起使用,但它应该与其他分类器一起使用。

输入参数是序列化模型文件(您可以在模型训练阶段创建并应用 -d 选项)和 ARFF 格式的测试文件。

public void test(String modelFileSerialized, String testFileARFF) 
    throws Exception
{
    // Deserialize the classifier.
    Classifier classifier = 
        (Classifier) weka.core.SerializationHelper.read(
            modelFileSerialized);

    // Load the test instances.
    Instances testInstances = DataSource.read(testFileARFF);

    // Mark the last attribute in each instance as the true class.
    testInstances.setClassIndex(testInstances.numAttributes()-1);

    int numTestInstances = testInstances.numInstances();
    System.out.printf("There are %d test instances\n", numTestInstances);

    // Loop over each test instance.
    for (int i = 0; i < numTestInstances; i++)
    {
        // Get the true class label from the instance's own classIndex.
        String trueClassLabel = 
            testInstances.instance(i).toString(testInstances.classIndex());

        // Make the prediction here.
        double predictionIndex = 
            classifier.classifyInstance(testInstances.instance(i)); 

        // Get the predicted class label from the predictionIndex.
        String predictedClassLabel =
            testInstances.classAttribute().value((int) predictionIndex);

        // Get the prediction probability distribution.
        double[] predictionDistribution = 
            classifier.distributionForInstance(testInstances.instance(i)); 

        // Print out the true label, predicted label, and the distribution.
        System.out.printf("%5d: true=%-10s, predicted=%-10s, distribution=", 
                          i, trueClassLabel, predictedClassLabel); 

        // Loop over all the prediction labels in the distribution.
        for (int predictionDistributionIndex = 0; 
             predictionDistributionIndex < predictionDistribution.length; 
             predictionDistributionIndex++)
        {
            // Get this distribution index's class label.
            String predictionDistributionIndexAsClassLabel = 
                testInstances.classAttribute().value(
                    predictionDistributionIndex);

            // Get the probability.
            double predictionProbability = 
                predictionDistribution[predictionDistributionIndex];

            System.out.printf("[%10s : %6.3f]", 
                              predictionDistributionIndexAsClassLabel, 
                              predictionProbability );
        }

        o.printf("\n");
    }
}

关于machine-learning - 类别的 WEKA 分类可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960580/

相关文章:

python - 在 2D 绘图 python 中可视化具有 100 个属性的 SVM 模型

python - 如何让 Pygame 渲染适用于我的神经网络?

python-3.x - 如何在多元时间序列数据中使用特征缩放?

java - WEKA:如何禁用标记数据的过滤器?

java - Weka 树,哪一棵支持 STRING 属性?

machine-learning - 如何在 Weka 构建的决策树中查找特征重要性

machine-learning - 求解器参数 'test_iter' 在测试阶段更改标签值

machine-learning - RNN/LSTM/GRU 最后一个时间步的状态与所有时间步的状态

java - WEKA - 将实例分配给 kmeans.buildClusterer 时出错

java - StringToWordVector过滤器weka