使用保存的模型来预测使用weka(Eclipse + Java)

时间:2015-01-09 09:31:41

标签: java machine-learning classification weka prediction

我对“Instances originalTrain =”这一行的论点感到困惑,任何人都可以帮助我纠正这个错误,因为我刚接触这个weka。我们正在使用java中的weka创建一个疾病预测系统。

import weka.classifiers.Classifier;
import weka.core.Instances;

public class Main {

    public static void main(String[] args) throws Exception
    {
        String rootPath="/some/where/"; 
        Instances originalTrain= //instances here (don't know to complete this statement)

        //load model
        Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model");

        //predict instance class values
        Instances originalTrain= //load or create Instances to predict (This statement too)

        //which instance to predict class value
        int s1=0;

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

        //get the prediction percentage or distribution
        double[] percentage=cls.distributionForInstance(originalTrain.instance(s1));

        //get the name of the class value
        String prediction=originalTrain.classAttribute().value((int)value); 

        System.out.println("The predicted value of instance "+
                            Integer.toString(s1)+
                            ": "+prediction); 

        //Format the distribution
        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);
    }

}

1 个答案:

答案 0 :(得分:0)

为了完整起见,问题中的代码段来自Get prediction percentage in WEKA using own Java code and a model

originalTrain应该是您的训练实例。我知道有两种方法可以将实例添加到originalTrain。

  1. 此方法从.arff文件加载数据,并基于找到的here指令

    // rootPath should be where the .arff file is held
    // filename should hold the complete name of the .arff file
    public static Instances instanceData(String rootPath, String filename) throws Exception
    { 
      // initialize source 
      DataSource source = null;
      Instances data = null;
      source = new DataSource(rootPath + filename);
      data = source.getDataSet();
    // set the class to the last attribute of the data (may need to tweak) if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() -1 ); return data; }

  2. 您可以按照此答案Define input data for clustering using WEKA API中的说明手动创建和添加实例。