-1

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

使用 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 行,我没有分配类(动作属性)。基本上我希望模型回答这个问题:)

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

-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

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

4

1 回答 1

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

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

于 2012-05-20T07:46:43.090 回答