【问题标题】:How to assign a class to an instance using saved model in weka如何使用 weka 中保存的模型将类分配给实例
【发布时间】:2012-05-26 20:05:01
【问题描述】:

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

使用 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

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

【问题讨论】:

    标签: c# java data-mining classification weka


    【解决方案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();
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2014-05-26
      • 2012-03-25
      • 2014-03-02
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 2015-01-28
      相关资源
      最近更新 更多