2012-05-19 39 views
-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); 
} 

但將始終爲每個指定類生成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

我做了一些更多的研究和跨這篇文章就來了: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(); 
    } 
} 

這代碼允許我檢查給定實例上正在預測哪個類以及所考慮的所有類的概率值。 我希望這會幫助別人:)

+1

它會幫助我! :) – Lorenzo

相關問題