所以基本上一個混淆矩陣是大小爲n*n
的2D 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;
}
的混淆矩陣往往與二進制數據,其數據並不能很好的工作。 – erip