Question

I have followed example code on weka websites for example but i still keep on getting an error of unable to find the class label

weka.core.WekaException: weka.classifiers.bayes.NaiveBayesMultinomialUpdateable: Not enough training instances with class labels (required: 1, provided: 0)!

I have tried this file with the weka explorer and it works fine.

  ArffLoader loader = new ArffLoader();
    loader.setFile(new File(""));//file is valid
    Instances structure = loader.getStructure();
    structure.setClassIndex(structure.numAttributes() - 1);

    // train NaiveBayes
    NaiveBayesMultinomialUpdateable n = new NaiveBayesMultinomialUpdateable();
    FilteredClassifier f = new FilteredClassifier();
    StringToWordVector s = new StringToWordVector();

    f.setFilter(s);
    f.setClassifier(n);

    f.buildClassifier(structure);
    Instance current;
    while ((current = loader.getNextInstance(structure)) != null)
      n.updateClassifier(current);

    // output generated model
    System.out.println(n);
Was it helpful?

Solution

The problem lies in where the class index is at position 0

ArffLoader loader = new ArffLoader();
    loader.setFile(new File(""));//file is valid
    Instances structure = loader.getStructure();
    structure.setClassIndex(0);

    // train NaiveBayes
    NaiveBayesMultinomialUpdateable n = new NaiveBayesMultinomialUpdateable();
    FilteredClassifier f = new FilteredClassifier();
    StringToWordVector s = new StringToWordVector();

    f.setFilter(s);
    f.setClassifier(n);

    f.buildClassifier(structure);
    Instance current;
    while ((current = loader.getNextInstance(structure)) != null)
      n.updateClassifier(current);

    // output generated model
    System.out.println(n);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top