title: Load and save Weka Model using Java API?
body: <p>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</p>
  
  <pre><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);
  
      }
  
  }
  </code></pre>
  
  <p>but I had the following error when I tried to run it from Eclipse:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: weka.classifiers.meta.FilteredClassifier cannot be cast to weka.classifiers.bayes.NaiveBayesMultinomial
  </code></pre>
  
  <p>what is the issue?</p>
  
  <p>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.</p>
  
  <p>I think there must be a way to tell the code how to reverse StringToVector then use :</p>
  
  <pre><code>NaiveBayesMultinomial  NBM =(NaiveBayesMultinomial) weka.core.SerializationHelper.read("D:/MultiNomial.model") 
  </code></pre>