Weka错误消息 - 没有足够的带有类标签的训练实例(必需:1,提供:0)!

时间:2014-08-22 16:13:48

标签: java machine-learning weka

我的Weka代码无效。
我不知道如何解决错误。
请给我一些建议 (我使用了weka.jar(版本3.6.11))

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.RBFNetwork;
import weka.clusterers.FarthestFirst;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;


public class WEKATutorial {

    public static void main(String[] args) throws Exception {
        WEKATutorial wekaTut = new WEKATutorial();
        wekaTut.executeWekaTutorial();
    }

    private void executeWekaTutorial() throws Exception {
        FastVector allAttributes = createAttributes();  
        Instances learningDataset = createLearningDataSet(allAttributes);   
        Classifier predictiveModel = learnPredictiveModel(learningDataset); 
        Evaluation evaluation = evaluatePredictiveModel(predictiveModel, learningDataset);  

        System.out.println(evaluation.toSummaryString());   
        predictUnknownCases(learningDataset, predictiveModel);  
    }

    private FastVector createAttributes() {
        Attribute ageAttribute = new Attribute("age");  

        FastVector genderAttributeValues = new FastVector(2);
        genderAttributeValues.addElement("male");
        genderAttributeValues.addElement("female");

        Attribute genderAttribute = new Attribute("gender", genderAttributeValues); 
        Attribute numLoginsAttribute = new Attribute("numLogins");

        FastVector allAttributes = new FastVector(3);   
        allAttributes.addElement(ageAttribute);
        allAttributes.addElement(genderAttribute);
        allAttributes.addElement(numLoginsAttribute);
        return allAttributes;
    }


    private Instances createLearningDataSet(FastVector allAttributes) {
        Instances trainingDataSet = new Instances("wekaTutorial", allAttributes, 4);    
        trainingDataSet.setClassIndex(2);   
        addInstance(trainingDataSet, 20., "male", 5);
        addInstance(trainingDataSet, 30., "female", 2);
        addInstance(trainingDataSet, 40., "male", 3);
        addInstance(trainingDataSet, 35., "female", 4);
        return trainingDataSet;
    }

    private void addInstance(Instances trainingDataSet, double age, String gender, int numLogins) {
        Instance instance = createInstance(trainingDataSet, age, gender, numLogins);
    }

    private Instance createInstance(Instances associatedDataSet, double age, String gender, int numLogins) {
        Instance instance = new Instance(3);
        instance.setDataset(associatedDataSet);
        instance.setValue(0, age);
        instance.setValue(1, gender);
        instance.setValue(2, numLogins);
        return instance;
    }

    private Classifier learnPredictiveModel(Instances learningDataset) throws Exception {
        Classifier classifier = getClassifier();    
        classifier.buildClassifier(learningDataset);    
        return classifier;
    }


    private Classifier getClassifier() {
        RBFNetwork rbfLearner = new RBFNetwork();   
        FarthestFirst EM_Learner = new FarthestFirst();
        rbfLearner.setNumClusters(2);
        return  rbfLearner;
    }


    private Evaluation evaluatePredictiveModel(Classifier classifier, Instances learningDataset) throws Exception {
        Evaluation learningSetEvaluation = new Evaluation(learningDataset); 
        learningSetEvaluation.evaluateModel(classifier, learningDataset);   
        return learningSetEvaluation;
    }


    private void predictUnknownCases(Instances learningDataset, Classifier predictiveModel) throws Exception {
        Instance testMaleInstance = createInstance(learningDataset, 32., "male", 0);
        Instance testFemaleInstance = createInstance(learningDataset, 32., "female", 0);
        double malePrediction = predictiveModel.classifyInstance(testMaleInstance);
        double femalePrediction = predictiveModel.classifyInstance(testFemaleInstance);

        System.out.println("Predicted number of logins [age=32]: ");
        System.out.println("\tMale = " + malePrediction);
        System.out.println("\tFemale = " + femalePrediction);
    }

}


以下是错误消息。

Exception in thread "main" weka.core.WekaException: weka.classifiers.functions.Logistic: Not enough training instances with class labels (required: 1, provided: 0)!
    at weka.core.Capabilities.test(Capabilities.java:1138)
    at weka.core.Capabilities.test(Capabilities.java:1023)
    at weka.core.Capabilities.testWithFail(Capabilities.java:1302)
    at weka.classifiers.functions.RBFNetwork.buildClassifier(RBFNetwork.java:153)
    at WEKATutorial.learnPredictiveModel(WEKATutorial.java:81)
    at WEKATutorial.executeWekaTutorial(WEKATutorial.java:24)
    at WEKATutorial.main(WEKATutorial.java:18)

我搜索了互联网,但我没有解决方案。 我非常沮丧。 :(

2 个答案:

答案 0 :(得分:1)

尝试在createInstance方法中将新实例添加到associatedDataSet:

associatedDataSet.add(instance); 

答案 1 :(得分:0)

以下列方式更改addInstance代码。

private void addInstance(Instances trainingDataSet, double age, String gender, int numLogins) {
    Instance instance = createInstance(trainingDataSet, age, gender, numLogins);
    trainingDataSet.add(instance);
}