Mark Needham

Thoughts on Software Development

Weka: Saving and loading classifiers

with 3 comments

In our continued machine learning travels Jen and I have been building some classifiers using Weka and one thing we wanted to do was save the classifier and then reuse it later.

There is documentation for how to do this from the command line but we’re doing everything programatically and wanted to be able to save our classifiers from Java code.

As it turns out it’s not too tricky when you know which classes to call and saving a classifier to a file is as simple as this:

MultilayerPerceptron classifier = new MultilayerPerceptron();
classifier.buildClassifier(instances); // instances gets passed in from elsewhere
 
Debug.saveToFile("/path/to/weka-neural-network", classifier);

If we want to load that classifier up we can make use of the SerializedClassifier class like so:

SerializedClassifier classifier = new SerializedClassifier();
classifier.setModelFile(new File("/path/to/weka-neural-network"));

Simples!

Be Sociable, Share!

Written by Mark Needham

December 12th, 2012 at 12:04 am

Posted in Machine Learning

Tagged with ,

  • Pingback: Geek Reading December 12, 2012 | Regular Geek

  • Chris Mills

    Thanks so much, this is exactly what I have been looking for. Incredibly helpful. Cheers!

  • Julio

    Hi there… this is great.
    I am trying to do the same with the WekaForecaster class.

    Do you have any idea if this is possible?

    Thanks!