有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用保存的模型进行机器学习,以使用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);
    }

}

共 (1) 个答案

  1. # 1 楼答案

    为了完整起见,问题中的代码片段源自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中的描述手动创建和添加实例