如何使用weka中的已保存模型将类分配给实例

时间:2012-05-19 15:05:40

标签: c# java data-mining classification weka

在我决定发布问题之前,我已经阅读了很多帖子,但仍然无法得到明确的答案。所以这就是:

使用weka我已经使用我的训练数据训练了一个NaiveBayesTree:

(the values are simplified, and there's 20000 rows in the training set)
AF3,F7,F3,FC5,T7,T8,FC6,F4,F8,AF4,Action
-1,2,0,1,0,0,-1,-0,-0,-0,NEUTRAL
-2,1,0,2,-0,0,-0,0,-1,-0,RIGHT
-1,1,0,2,-0,0,-1,0,-1,-0,LEFT

现在我想在我的程序中使用保存的模型来确定给定128行测试数据中的类分布是什么。对于这128行,我没有分配类(Action属性)。基本上我希望模型回答:)

所以测试行看起来像这样:

-1,1,0,2,-0,0,-1,0,-1,-0,?

到目前为止,我已经想出了这段代码:

Classifier nbTree = (Classifier)SerializationHelper.read(Model) as NBTree;
Instances testInstances = TestSet();
testInstances.setClassIndex(10);

for (int i = 0; i < testInstances.numInstances(); i++)
{
    Instance instance = testInstances.instance(i);
    double assignedClass = nbTree.classifyInstance(instance);
    double[] distributionForInstance = nbTree.distributionForInstance(instance);
}

但它总是为每个assignedClass生成0,而distributionForInstance将始终只有一个具有不同值的元素:

0,9412543332996
0,9412543332996
0,9412543332996
0,9412543332996
0,0577106296809467
0,315216251505317
0,9412543332996
0,9412543332996
0,315216251505317
0,315216251505317
0,863366140658458
0,9412543332996
0,9412543332996
0,9412543332996
0,9412543332996
0,783615619462732

我现在走了两天,并且非常感谢一些帮助:)

1 个答案:

答案 0 :(得分:1)

我做了一些更多的研究并发现了这篇文章:http://weka.wikispaces.com/Making+predictions帮助我编写了以下代码:

Classifier nbTree = (Classifier)SerializationHelper.read(Model) as NBTree;
Instances testDataSet = new Instances(new BufferedReader(new FileReader(arff)));
testDataSet.setClassIndex(10);
Evaluation evaluation = new Evaluation(testDataSet);

for (int i = 0; i < testDataSet.numInstances(); i++)
{
    Instance instance = testDataSet.instance(i);
    evaluation.evaluateModelOnceAndRecordPrediction(nbTree, instance);
}

foreach (object o in evaluation.predictions().toArray())
{
    NominalPrediction prediction = o as NominalPrediction;
    if (prediction != null)
    {
        double[] distribution = prediction.distribution();
        double predicted = prediction.predicted();
    }
}

这段代码允许我检查给定实例上预测的类以及所考虑的所有类的概率值。 我希望这会有所帮助:)