【问题标题】:using the saved model for predicting using weka (Eclipse+Java)使用保存的模型进行预测使用 weka (Eclipse+Java)
【发布时间】:2015-03-07 14:13:06
【问题描述】:

我对“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);
    }

}

【问题讨论】:

    标签: java machine-learning classification weka prediction


    【解决方案1】:

    为了完整起见,问题中的代码sn-p源自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 中的说明手动创建和添加实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-25
      • 2018-12-27
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 2016-03-08
      • 2018-09-28
      相关资源
      最近更新 更多