Question

I am using a string to vector filter to convert my arff to vector format.

But it throws an exception

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

I tried to use the same on weka explorer and it worked fine.

This is my code

  ArffLoader loader = new ArffLoader();
    loader.setFile(new File("valid file"));
    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);

I have tried another example but it still does not work

  ArffLoader loader = new ArffLoader();
    loader.setFile(new File("valid file"));

    Instances structure = loader.getStructure();


    // train NaiveBayes
    NaiveBayesMultinomialUpdateable n = new NaiveBayesMultinomialUpdateable();
    FilteredClassifier f = new FilteredClassifier();
    StringToWordVector s = new StringToWordVector();
    s.setInputFormat(structure);
    Instances struct = Filter.useFilter(structure, s);

    struct.setClassIndex(0);
    System.out.println(struct.numAttributes()); // only gives 2 or 1 attributes 




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

    // output generated model
    System.out.println(n);

The number of attributes printed is always 2 or 1.

It seems the string to word vector isn't working as expected

Original folder : https://www.dropbox.com/sh/cma4hbe2r96ul1c/GL2wNdeVUz

Converted to arff: https://www.dropbox.com/s/efle6ci4lb5riq7/test1.arff

Was it helpful?

Solution

According to your arff, the class seems to be the second in the two attributes, so the problem can be here:

struct.setClassIndex(0);

try

struct.setClassIndex(1);

UPDATE: I made this change to the first example, and it gives no exception, and prints out:

The independent probability of a class
--------------------------------------
oil spill   40.0
police  989.0

The probability of a word given the class
-----------------------------------------
        oil spill   police  
class   Infinity    Infinity    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top