无法在weka中获取类标签* WEKA * DUMMY * STRING * FOR * STRING * ATTRIBUTES *

时间:2014-05-27 06:02:30

标签: java model weka

我正在尝试使用weka库和在线教程对java中的实例进行分类。

我已经在我的设备中构建了一个模型,并使用此代码从磁盘加载了该模型。

public void makeModel() throws Exception
    {
        ArffLoader loader = new ArffLoader();
    loader.setFile(new File("data.arff"));

   Instances structure = loader.getDataSet();
    structure.setClassIndex(1);

// train NaiveBayes

NaiveBayesMultinomial n = new NaiveBayesMultinomial();
FilteredClassifier f = new FilteredClassifier();
StringToWordVector s = new StringToWordVector();

s.setUseStoplist(true);
s.setWordsToKeep(100);

f.setFilter(s);

f.setClassifier(n);
structure.numAttributes();
 f.buildClassifier(structure);
Instance current;


Evaluation eval = new Evaluation(structure);
 eval.crossValidateModel(f, structure, 10, new Random(1));
 System.out.println(eval.toSummaryString("\nResults\n======\n", false));



// output generated model
//System.out.println(f);
 ObjectOutputStream oos = new ObjectOutputStream(
                            new FileOutputStream("classifier.model"));
 oos.writeObject(f);
 oos.flush();
 oos.close();
    }

------------------------输出-------------

结果

正确分类实例20158 79.6948% 错误分类的实例5136 20.3052% Kappa统计0.6737 平均绝对误差0.0726 均方根误差0.2025 相对绝对误差38.7564% 根相对平方误差66.1815% 案件覆盖率(0.95水平)96.4142% 平均值。区域面积(0.95水平)27.7531% 实例总数25294


然后我使用相同的模型对未标记的实例进行分类。

public void classify() throws Exception
    {
        FilteredClassifier cls = (FilteredClassifier) weka.core.SerializationHelper.read("classifier.model");


Instances unlabeled = new Instances(
                         new BufferedReader(
                           new FileReader("test.arff")));

 // set class attribute
 unlabeled.setClassIndex(0);

 // create copy
 Instances labeled = new Instances(unlabeled);

 // label instances
 for (int i = 0; i < unlabeled.numInstances(); i++) {
     System.out.println(labeled.instance(i).classValue());
     System.out.print(", actual: " + labeled.classAttribute().value((int)labeled.instance(i).classValue()));
   double clsLabel = cls.classifyInstance(unlabeled.instance(i));
   labeled.instance(i).setClassValue(clsLabel);
   System.out.println(", predicted: " + labeled.classAttribute().value((int) clsLabel));
 }
 // save labeled data
System.out.println("ended");


    }

------------------------输出---------------------- -----

1.0 ,实际:Bud1?这是一个新的新字符串.txtIlocblobR(?????? @?@?@?@E?DSDB` @?@?@,预测:* WEKA * DUMMY * STRING * FOR * STRING * ATTRIBUTES * 2.0 ,actual:这是一个新的新字符串 ,预测:* WEKA * DUMMY * STRING * for * STRING *属性* 结束


但是,我的错误是预测实际上是 * WEKA * DUMMY * STRING * FOR * STRING * ATTRIBUTES * ,而它应该给我一个类标签。

2 个答案:

答案 0 :(得分:1)

保存分类器时还会保存实例(只是标题,不需要数据):

Instances instancesSample = new Instances(structure, 0);
instancesSample.setClassIndex(1);
...
ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream("classifier.model"));
oos.writeObject(f);
oos.writeObject(instancesSample);
oos.flush();
oos.close();

加载模型后,将保存的实例加载为instancesSample。 分类时:

ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream("classifier.model")));
FilteredClassifier cls = (FilteredClassifier)= (Classifier) objectInputStream.readObject();
Instances instancesSample = (Instances) objectInputStream.readObject();
objectInputStream.close();

int classIndex = 1;
Instances ins = unlabeled[i];
double clsLabel = cls.classifyInstance(ins);
String prediction = instancesSample.attribute(classIndex).value((int) clsLabel));
System.out.println(", predicted: " + prediction);

答案 1 :(得分:0)

我已将这些行添加到我的分类方法中。

ArffLoader loader = new ArffLoader();
    loader.setFile(new File("data.arff"));

   Instances structure = loader.getDataSet();
    structure.setClassIndex(1);

要获得类标签,我将其更改为此

System.out.println(", predicted: " + structure.classAttribute().value((int) clsLabel));