序列化weka模型不起作用

时间:2017-02-20 11:57:37

标签: c# weka xmlserializer

我编写的代码用于在c#中使用weka构建分类器。要为下一个预测存储此分类器,我使用了Serialize,如下所示:

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();
    }
}

当我运行此代码时,我在SMO.model文件中获得此输出:

<?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" />

我想存储分类器,但我的代码不起作用,问题是什么?我怎么解决呢?

0 个答案:

没有答案