2016-09-23 36 views
0

我需要在java中創建混淆矩陣。數據可用單位文本文件。我有兩個文本文件。一個文件的實際數據看起來像這樣如何在Java中創建混淆矩陣

<PersonName>Amit</PersonName> <Address>XYZ</Address> 
<PersonName>Sam</PersonName> <Address>St 123 UK </Address 

在第二個文件中的預測的數據是這樣的。

Amit: PersonName 
Sam St: Address 

沒有人有任何想法如何產生的混淆矩陣,計算所有的假陽性,假陰性,真陰性和真這是需要做積極的。

+0

的混淆矩陣往往與二進制數據,其數據並不能很好的工作。 – erip

回答

1

所以基本上一個混淆​​矩陣是大小爲n*n2D matrix。其中n代表要預測的可能類別的數量。

現在我們需要維護一個n類別的索引數組。例如:

{Cat, Dog, Lion, Tiger}

假設你有預測值和實際值的列表:

Act  Pred 
Cat  Cat 
Cat  Dog 
Dog  Lion 
Lion  Lion 
etc  etc 

現在只是假設,這個陣列可以被轉換成座標對應於先前數組列表:

A P 
0 0 
0 1 
etc etc 

現在在2D array中要更新的條目屬於上述索引。

的代碼可能是這個樣子:

String[] a = new String[] {"airplanes", "butterfly", "flower", "grand_piano", "starfish", "watch"}; 
    Category = Arrays.asList(a); 
    int [][] confMatrix = new int[6][6]; 
    for (Instance inst : predictedValues) { 
      String outLabel = inst.getPredictedLabel(); 
      String actualLabel = inst.getLabel(); 
      int outLabelIndex = Category.indexOf(outLabel); 
      int actualLabelIndex = Category.indexOf(actualLabel); 
      confMatrix[actualLabelIndex][outLabelIndex] += 1; 
    }