Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java应用中如何使用序列化分类器对新实例进行分类_Java_Weka - Fatal编程技术网

java应用中如何使用序列化分类器对新实例进行分类

java应用中如何使用序列化分类器对新实例进行分类,java,weka,Java,Weka,我想使用序列化分类器对新实例进行分类。我找到了这门课,但我不懂 arg[2]=类属性名和arg[3]=从原始数据集预测的实例的基于1的索引 以下是该类的代码: import weka.core.*; import weka.classifiers.*; import java.io.*; /** * A little class for testing deserialization and prediction. * * @author FracPete (fracpet at w

我想使用序列化分类器对新实例进行分类。我找到了这门课,但我不懂

arg[2]
=类属性名和
arg[3]
=从原始数据集预测的实例的基于1的索引

以下是该类的代码:

import weka.core.*;
import weka.classifiers.*;

import java.io.*;

/**
 * A little class for testing deserialization and prediction.
 * 
 * @author FracPete (fracpet at waikat dot ac dot nz)
 */
public class Blah {

   /**
    * Takes 4 arguments:
    * <ol>
    *   <li>serialized model</li>
    *   <li>ARFF file</li>
    *   <li>class attribute name</li>
    *   <li>1-based index of an instance to predict from original dataset</li>
    * </ol>
    */
   public static void main(String[] args) throws Exception {
      // read the arff training file
      BufferedReader reader = new BufferedReader(new FileReader(args[1]));
      Instances in = new Instances(reader);
      in.setClass(in.attribute(args[2]));

      // instance to classify
      int index = Integer.parseInt(args[3]) - 1;
      Instance toClassifyInstance = (Instance) in.instance(index).copy();
      toClassifyInstance.setClassValue(Instance.missingValue());

      // deserialize model
      Classifier cls = null;
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(args[0]));
      cls = (Classifier) ois.readObject();
      ois.close();

      // PREDICTION
      double clsLabel = cls.classifyInstance(toClassifyInstance);
      String classLabel = in.classAttribute().value((int) clsLabel);

      System.out.println(classLabel + " =?= " + in.instance(index).stringValue(in.classIndex()));
   }
}
导入weka.core.*;
导入weka。分类器。*;
导入java.io.*;
/**
*一个用于测试反序列化和预测的小类。
* 
*@作者fracpet(新西兰waikat点ac点的fracpet)
*/
公开课之类的{
/**
*接受4个参数:
* 
*
  • 序列化模型
  • *
  • ARFF文件
  • *
  • 类属性名称
  • *从原始数据集预测实例的基于
  • 1的索引
  • * */ 公共静态void main(字符串[]args)引发异常{ //阅读arff培训文件 BufferedReader=new BufferedReader(new FileReader(args[1]); 中的实例=新实例(读取器); in.setClass(in.attribute(args[2]); //实例分类 int index=Integer.parseInt(args[3])-1; Instance toClassifyInstance=(Instance)in.Instance(index.copy(); toClassifyInstance.setClassValue(Instance.missingValue()); //反序列化模型 分类器cls=null; ObjectInputStream ois=新ObjectInputStream(新文件输入流(args[0]); cls=(分类器)ois.readObject(); ois.close(); //预测 double clsLabel=cls.classifyInstance(toClassifyInstance); 字符串classLabel=in.classAttribute().value((int)clsLabel); System.out.println(classLabel+“=?=”+in.instance(index.stringValue)(in.classIndex()); } }

    提前感谢。

    大多数分类器在预测之前需要一组训练数据。此训练数据用于创建模型,然后可用于进行分类。看起来这里发生的一切是,他们正在读取已序列化的模型,然后对其进行预测。也就是说,它可能使用了ObjectOutputStream(http://download.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html)在通过一些训练数据创建分类器之后


    如果这没有回答您的困惑,请进一步澄清您要查找的内容。

    第一个参数args[0]是将用于分类的序列化分类器的路径名。下一个参数是实例构造函数希望位于arff文件中的数据集的路径。此集合必须具有与创建序列化分类器时使用的训练数据中的特征兼容的特征(因此,以相同的顺序使用完全相同的特征)。args[2]是属性的名称,该属性是来自arff的数据集中的类属性,args[3]是索引加上实例的名称,该实例将在类标签的值设置为“缺少”后对其自身的副本进行分类


    如果您试图对一个“外部”实例进行分类,例如,在您构建了一些代码之后,该实例在分类之前仍然必须有一个到一些兼容数据集的链接。这可以在实例上使用方法setDataset(Instances)来完成。没有进行兼容性检查,因此您可能希望在实例上使用checkInstance(Instance)进行检查。

    谢谢您的回复,是的,我有一个模型,我想在此模型上公开一个实例,以影响类我应该如何对该实例进行分类,我不理解包含的参数如args[2]是项的类,它查找索引args[3]的项。感谢您,这正是我要查找的:)