如何在weka中获取实例的预测值? [英] How to get predication value for an instance in weka?

查看:666
本文介绍了如何在weka中获取实例的预测值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Weka,需要为每个测试实例输出每个标签的预测值(概率)。

I am working on Weka and need to output the predication values (probabilities) of each labels for each test instance.

在GUI中,分类标签中有一个选项(分类 - >选项 - >输出预测值),它通过输出每个标签的预测概率来完成这项工作但是如何在java代码中执行此操作。我想在分类后获得每个标签的概率分数吗?

In GUI there is an option in classify tab as (classify -> options -> Output predicted value) which does this work by outputting the prediction probabilities for each label but how to do this in java code. I want to receive probability scores for each label after classifying it ?

推荐答案

以下代码包含一组培训实例,以及输出特定实例的预测概率。

The following code takes in a set of training instances, and outputs the predicted probability for a specific instance.


import weka.classifiers.trees.J48;
import weka.core.Instances;

public class Main {

    public static void main(String[] args) throws Exception
    {
        //load training instances
        Instances test=...

        //build a J48 decision tree
        J48 model=new J48(); 
        model.buildClassifier(test);

        //decide which instance you want to predict
        int s1=2;

        //get the predicted probabilities 
        double[] prediction=model.distributionForInstance(test.get(s1));

        //output predictions
        for(int i=0; i<prediction.length; i=i+1)
        {
            System.out.println("Probability of class "+
                                test.classAttribute().value(i)+
                               " : "+Double.toString(prediction[i]));
        }

    }

}

方法distributionForInstance仅适用于能够输出分布预测的分类器。您可以阅读此处

The method "distributionForInstance" only works for classifiers capable of outputting distribution predictions. You can read up on it here.

这篇关于如何在weka中获取实例的预测值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆