2015-06-19 26 views
0

這裏是我的代碼,我使用的是weka API。我想打印出錯誤分類的實例和準確分類的實例。請幫助我,或者告訴我有關能夠做我想做的任何其他文本分類的Java API。如何識別在weka中錯誤分類的確切實例

public void evaluation() throws Exception{ 
    BufferedReader reader=null; 
    reader= new BufferedReader(new FileReader("SparseDTM.arff")); 

    Instances train= new Instances(reader); 
    train.setClassIndex(0); 
    train.toSummaryString(); 
    reader.close(); 
    SMO svm=new SMO(); 
    svm.buildClassifier(train); 

    NaiveBayes nB = new NaiveBayes(); 
    nB.buildClassifier(train); 

    weka.classifiers.Evaluation eval= new weka.classifiers.Evaluation(train); 
    eval.crossValidateModel(nB, train,10,new Random(1)); 
    //eval.crossValidateModel(nB, train,10,new Random(1), new Object[] { }); 

    System.out.println("\n\t************Results by Naive Bayes Classifier************\n"); 
    System.out.println(eval.toSummaryString("", true)); 
    System.out.println(eval.toClassDetailsString()); 
// System.out.println("F Measure: "+eval.fMeasure(1) + " " + "Precision: "+eval.precision(1) + " " + "Precision: "+eval.recall(1)); 
// System.out.println("Correct :" + eval.correct()); 
// System.out.println("Weighted True Negative Rate: " + eval.weightedTrueNegativeRate()); 
// System.out.println("Weighted False Positive Rate:" + eval.weightedFalsePositiveRate()); 
// System.out.println("Weighted False Negative Rate:" + eval.weightedFalseNegativeRate()); 
// System.out.println("Weighted True Positive Rate:" + eval.weightedTruePositiveRate()); 
    System.out.println(eval.toMatrixString()); 
    } 

回答

0

下面是一個方法,可以幫助你解決你的問題。所以,你可以編輯它來達到你的目標。

public void showPredictions(){  

    BufferedReader reader=null; 
    reader= new BufferedReader(new FileReader("SparseDTM.arff")); 

    Instances data = new Instances(reader); 

    double[] predictions; 
    try { 

     NaiveBayes classifier = new NaiveBayes(); 
     classifier.buildClassifier(data); 

     predictions = eval.evaluateModel(classifier, data); 

     int classIndex = data.numAttributes()-1; 
     // getting the array of predictions for each instance 
     System.out.println("predictions: "); 
     for (int i=0; i < data.numInstances(); i++) { 
      double realValue = testData.instance(i).classValue(); // test or train data. 
      System.out.print("Real Value: " + testData.instance(i).stringValue(classIndex)); 
      System.out.println("\tClassification predicted value: " + predictions[i]); 

      if(realValue != predictions[i]) { 
       System.out.println("misclassified instance: " + testData.instance(i).toString()); 
      } 
     }  
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

通過「數據」替換「TESTDATA」如果你能遵守相關題庫組誤分類情況。否則,你必須提供一個測試集。

+0

此解決方案適用於測試數據集,在我們構建模型之後,但交叉驗證又如何?並感謝您的回答 –

+0

您的解決方案向我展示了一種這樣做的方式,非常感謝。 我已經完成了...... :) –

+0

對於交叉驗證,您必須在每次摺疊中應用解決方案才能觀察錯誤分類的實例。在這種情況下,請查看此源代碼[link](https://weka.wikispaces.com/Generating+cross-validation+folds+(Java+approach) –