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

using the saved model for predicting using weka (Eclipse+Java)

我对行的参数感到困惑 "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);
    }

}

为了完整起见,问题中的代码片段来自 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();<br>
      // 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;
    }
    </pre>

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