Example usage for org.apache.mahout.classifier ConfusionMatrix ConfusionMatrix

List of usage examples for org.apache.mahout.classifier ConfusionMatrix ConfusionMatrix

Introduction

In this page you can find the example usage for org.apache.mahout.classifier ConfusionMatrix ConfusionMatrix.

Prototype

public ConfusionMatrix(Collection<String> labels, String defaultLabel) 

Source Link

Usage

From source file:de.tu_berlin.dima.aim3.naivebayes.classifier.NBayesClassifierPlanAssembler.java

License:Open Source License

public static void main(String... args) throws NumberFormatException, IOException {
    if (args == null)
        args = new String[0];

    String resultData = (args.length > 0 ? args[0] : "file:///home/mkaufmann/datasets/result");

    Map<String, Map<String, Integer>> confusionMatrix = new HashMap<String, Map<String, Integer>>();

    InputStreamReader stream = new InputStreamReader(new FileInputStream(new File(resultData)));
    BufferedReader in = new BufferedReader(stream);

    String line = null;//ww w . j  a  v  a 2 s .com
    while ((line = in.readLine()) != null) {
        String[] triple = line.split("::");
        if (triple.length == 3) {
            String correct = triple[0];
            String label = triple[1];
            int count = Integer.parseInt(triple[2]);

            Map<String, Integer> rowMatrix = confusionMatrix.get(correct);
            if (rowMatrix == null) {
                rowMatrix = new HashMap<String, Integer>();
            }

            rowMatrix.put(label, count);
            confusionMatrix.put(correct, rowMatrix);
        }
    }

    ConfusionMatrix matrix = new ConfusionMatrix(confusionMatrix.keySet(), "default");
    for (Map.Entry<String, Map<String, Integer>> correctLabelSet : confusionMatrix.entrySet()) {
        Map<String, Integer> rowMatrix = correctLabelSet.getValue();
        for (Map.Entry<String, Integer> classifiedLabelSet : rowMatrix.entrySet()) {
            matrix.addInstance(correctLabelSet.getKey(), classifiedLabelSet.getKey());
            matrix.putCount(correctLabelSet.getKey(), classifiedLabelSet.getKey(),
                    classifiedLabelSet.getValue());
        }
    }

    System.out.println(matrix.toString());
}