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") 
4
  • As the exception states, the serialized classifier is not of the type you're trying to cast it. Commented Feb 20, 2016 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 Commented Feb 20, 2016 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 ? Commented Feb 20, 2016 at 20:02
  • @qqilihq updated the question Commented Feb 20, 2016 at 21:36

1 Answer 1

1

try to

NaiveBayesMultinomial NBM = new NaiveBayesMultinomial();
NBM = (NaiveBayesMultinomial)weka.core.SerializationHelper.read("D:/MultiNomial.model");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.