00001 package edu.tum.cs.classification; 00002 00003 import java.io.PrintStream; 00004 import java.util.HashMap; 00005 import java.util.Map.Entry; 00006 00007 public class ConfusionMatrix<TClass> { 00008 HashMap<TClass, HashMap<TClass,Integer>> matrix; 00009 int instances = 0; 00010 int correct = 0; 00011 00012 public ConfusionMatrix() { 00013 matrix = new HashMap<TClass, HashMap<TClass,Integer>>(); 00014 } 00015 00016 public void addCase(TClass classification, TClass groundTruth) { 00017 HashMap<TClass,Integer> list = matrix.get(classification); 00018 if(list == null) { 00019 list = new HashMap<TClass,Integer>(); 00020 matrix.put(classification, list); 00021 } 00022 Integer cnt = list.get(groundTruth); 00023 if(cnt == null) 00024 cnt = 1; 00025 else 00026 cnt++; 00027 list.put(groundTruth, cnt); 00028 instances++; 00029 if(classification == groundTruth) 00030 correct++; 00031 } 00032 00033 public void print() { 00034 PrintStream out = System.out; 00035 for(Entry<TClass, HashMap<TClass,Integer>> e : matrix.entrySet()) { 00036 out.printf("%s: %s\n", e.getKey().toString(), e.getValue().toString()); 00037 } 00038 out.printf("correct: %.2f%% (%d/%d)", (double)correct/instances*100, correct, instances); 00039 } 00040 }