0

I wrote code for building a classifier with weka in c#. To store this classifier for the next predictions I used Serialize as seen below:

int percentSplit = 66;
try
{
    Instances insts = new Instances(new java.io.FileReader(@"C:\Users\...\Desktop\iris.arff"));
    insts.setClassIndex(insts.numAttributes() - 1);

    var cl = new weka.classifiers.functions.SMO();

    weka.filters.Filter myRandom = new weka.filters.unsupervised.instance.Randomize();
    myRandom.setInputFormat(insts);
    insts = weka.filters.Filter.useFilter(insts, myRandom);

    int trainSize = insts.numInstances() * percentSplit / 100;
    int testSize = insts.numInstances() - trainSize;
    Instances train = new Instances(insts, 0, trainSize);

    cl.buildClassifier(train);
    int numCorrect = 0;
    for (int i = trainSize; i < insts.numInstances(); i++)
    {
        Instance currentInst = insts.instance(i);
        double predictedClass = cl.classifyInstance(currentInst);
        if (Math.Abs(predictedClass - insts.instance(i).classValue()) < 0.001)
            numCorrect++;
        }
        XmlSerializer x = new XmlSerializer(typeof(weka.classifiers.functions.SMO));
        TextWriter writer = new StreamWriter(@"C:\Users\...\Desktop\SMO.model");
        x.Serialize(writer, cl);
        writer.Flush();
        writer.Close();
    }
}

when I ran this code I get this output in SMO.model file:

<?xml version="1.0" encoding="utf-8"?><SMO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

I want to store the classifier but my code doesn't work, what is the problem? how can I solve it?

Sentry
  • 4,102
  • 2
  • 30
  • 38
Shirin
  • 11
  • 3
  • How does the SMO class look like? Maybe it's not ment to be serialized with the XML Serializer? You could look into BinaryFormatter though – Icepickle Feb 20 '17 at 12:01
  • I used this tutorial https://weka.wikispaces.com/Serialization , and based on this page, it's serializable – Shirin Feb 20 '17 at 12:08
  • Alright, but here they also rather use a binary form of storing the data, and not through an xmlserializer – Icepickle Feb 20 '17 at 13:39
  • so what is your solution for my code? how I can store my Classifier model? – Shirin Feb 20 '17 at 13:54
  • try something like this http://stackoverflow.com/a/6179516/3923800 – xro7 Feb 22 '17 at 09:30

0 Answers0