문제

I have a Train.arff file to which I want to add new instance (say "2,F,22222,1002-5,?") and then classify the last attributes. All my attributes are nominal.

@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. Loaded Train.arff
  2. Adding Instance

            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. Building Classifier

    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) at weka.filters.Filter.useFilter(Filter.java:663) at weka.classifiers.functions.Logistic.buildClassifier(Logistic.java:546) at com.feisystems.bham.weka.LogisticRegression.buildAndClassify(LogisticRegression.java:70) at com.feisystems.bham.weka.LogisticRegression.(LogisticRegression.java:20) at com.feisystems.bham.weka.AttTest.main(AttTest.java:22)

I am using Weka 3.7 and don't see any example to correctly add instance to an existing file and classify one attribute of the instance.

As a work around I am currently creating another Test.arff file with all the matching attributes from Train.arff and adding instance to those attributes. I however want to add an instance to the same Train.arff file and classify it.

도움이 되었습니까?

해결책

This works!

        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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top