0

I have my model on my hard drive at d:\MultiNomial.model. That model can be run correctly from weka. The model was built to classify a text using StringToVector as a filter. I am using java to load that model using Weka API. This is my source code

import weka.core.*;
import weka.classifiers.bayes.NaiveBayesMultinomial;
import weka.core.converters.ConverterUtils.DataSource;
public class Classifier {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        NaiveBayesMultinomial    NBM =(NaiveBayesMultinomial) weka.core.SerializationHelper.read("D:/MultiNomial.model");
        DataSource source= new DataSource(   "D:/test.arff");
        Instances Testset=source.getDataSet() ;
        Testset.setClassIndex(Testset.numAttributes()-1);

        Instance newInstance=Testset.instance(0);
        double PredictVal=NBM.classifyInstance(newInstance);
        System.out.println(PredictVal);

    }

}

but I had the following error when I tried to run it from Eclipse:

Exception in thread "main" java.lang.ClassCastException: weka.classifiers.meta.FilteredClassifier cannot be cast to weka.classifiers.bayes.NaiveBayesMultinomial

what is the issue?

In Weka I used FilteredClassifier -> Unsupervised-> Attribute->StringToVector. Then I chose NaiveBayesMultinomial and I saved my model in my drive. Now When I used Java, I got the error : weka.classifiers.meta.FilteredClassifier cannot be cast to weka.classifiers.bayes.NaiveBayesMultinomial.

I think there must be a way to tell the code how to reverse StringToVector then use :

NaiveBayesMultinomial  NBM =(NaiveBayesMultinomial) weka.core.SerializationHelper.read("D:/MultiNomial.model") 
Lylia John
  • 51
  • 8
  • As the exception states, the serialized classifier is not of the type you're trying to cast it. – qqilihq Feb 20 '16 at 18:58
  • I am sure they are the same model classifier algorithm. I also created another model J48 and casted to J48 and have the same error !! weka.classifiers.meta.FilteredClassifier cannot be cast to weka.classifiers.trees.J48 – Lylia John Feb 20 '16 at 19:05
  • Is the problem because I use FilteredClassifier "StringToVector" and the code does not support how can be done in reverse mode, before applying the Classifier algorithm ? – Lylia John Feb 20 '16 at 20:02
  • @qqilihq updated the question – Lylia John Feb 20 '16 at 21:36

1 Answers1

1

try to

NaiveBayesMultinomial NBM = new NaiveBayesMultinomial();
NBM = (NaiveBayesMultinomial)weka.core.SerializationHelper.read("D:/MultiNomial.model");
김진헌
  • 51
  • 7