添加实例并在Weka 3.6.5+中对其进行分类

时间:2014-04-01 20:55:55

标签: java machine-learning weka

我有一个 Train.arff 文件,我想在其中添加新实例(例如“2,F,22222,1002-5,?”),然后对最后的属性进行分类。我的所有属性都是名义上的。

@attribute age {2,3,4,5,6}
@attribute gender {F,M}
@attribute zipcode {22222,33333,11111}
@attribute race {1002-5,2028-9,2054-5,2076-8,2106-3}
@attribute service {H0018,H2034,H0004,H0009,H0006}

@data
2,F,22222,1002-5,H0018
  1. 已加载 Train.arff
  2. 添加实例

            Instance inst = new Instance(10);
            inst.setValue(trainData.attribute(0), age);
            inst.setValue(trainData.attribute(1), administrativeGenderCode);
            inst.setValue(trainData.attribute(2), zipCode);
            inst.setValue(trainData.attribute(3), race);
            inst.setValue(trainData.attribute(4), "H2034");
    
            // inst.setDataset(trainData);
    
            // add
            trainData.add(inst);
    
  3. 构建分类器

    public String buildAndClassify() {
        //build model
        Logistic model = new Logistic();
        try {
            model.buildClassifier(trainData); <-- fails
    
            Instances labeled = new Instances(trainData);
            double clsLabel = model.classifyInstance(trainData.lastInstance());
            labeled.lastInstance().setClassValue(clsLabel);
    
            System.out.print(labeled.lastInstance().stringValue(7));
    
            return labeled.lastInstance().stringValue(7);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return "";
    }   
    
      

    java.lang.NullPointerException at   weka.filters.unsupervised.attribute.ReplaceMissingValues.batchFinished(ReplaceMissingValues.java:189)     在weka.filters.Filter.useFilter(Filter.java:663)at   weka.classifiers.functions.Logistic.buildClassifier(Logistic.java:546)     在   com.feisystems.bham.weka.LogisticRegression.buildAndClassify(LogisticRegression.java:70)     在   com.feisystems.bham.weka.LogisticRegression。(LogisticRegression.java:20)     在com.feisystems.bham.weka.AttTest.main(AttTest.java:22)

  4. 我正在使用Weka 3.7并且没有看到任何示例正确地将实例添加到现有文件并对实例的一个属性进行分类。

    作为一种解决方法,我正在创建另一个Test.arff文件,其中包含来自Train.arff的所有匹配属性,并将实例添加到这些属性。但是我想在同一个Train.arff文件中添加一个实例并对其进行分类。

1 个答案:

答案 0 :(得分:0)

这有效!

        Instance inst = new Instance(4); <-- Adjust number of instances you want to add.

        inst.setValue(trainData.attribute(0), age);
        inst.setValue(trainData.attribute(1), administrativeGenderCode);
        inst.setValue(trainData.attribute(2), zipCode);
        inst.setValue(trainData.attribute(3), race);
//      inst.setValue(trainData.attribute(4), "H2034"); <-- Do not add the instance you want to classify.