@@ -0,0 +1,2336 @@
+package edu.neu.ccs.pyramid.eval;%0A%0Aimport edu.neu.ccs.pyramid.dataset.MultiLabel;%0Aimport edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet;%0Aimport edu.neu.ccs.pyramid.multilabel_classification.MultiLabelClassifier;%0Aimport org.apache.mahout.math.Vector;%0A%0Aimport java.util.Arrays;%0A%0A/**%0A * multi-label confusion matrix%0A * Created by chengli on 3/2/16.%0A */%0Apublic class MLConfusionMatrix %7B%0A    private int numClasses;%0A    private Entry%5B%5D%5B%5D entries;%0A%0A    public int getNumClasses() %7B%0A        return numClasses;%0A    %7D%0A%0A    public Entry%5B%5D%5B%5D getEntries() %7B%0A        return entries;%0A    %7D%0A%0A    public MLConfusionMatrix(int numClasses, MultiLabel%5B%5D trueLabels, MultiLabel%5B%5D predictions) %7B%0A        this.numClasses = numClasses;%0A        int numData = trueLabels.length;%0A        this.entries = new Entry%5BnumData%5D%5BnumClasses%5D;%0A        for (int i=0;i%3CnumData;i++)%7B%0A            MultiLabel label = trueLabels%5Bi%5D;%0A            MultiLabel prediction = predictions%5Bi%5D;%0A            Vector labelVector = label.toVector(numClasses);%0A            Vector predVector = prediction.toVector(numClasses);%0A            for (int l=0;l%3CnumClasses;l++)%7B%0A                double labelMatch = labelVector.get(l);%0A                double prediMatch = predVector.get(l);%0A                if (labelMatch==1&&prediMatch==1)%7B%0A                    entries%5Bi%5D%5Bl%5D=Entry.TP;%0A                %7D else if (labelMatch==1&&prediMatch==0)%7B%0A                    entries%5Bi%5D%5Bl%5D=Entry.FN;%0A                %7D else if (labelMatch==0&&prediMatch==0)%7B%0A                    entries%5Bi%5D%5Bl%5D=Entry.TN;%0A                %7D else %7B%0A                    entries%5Bi%5D%5Bl%5D=Entry.FP;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * assuming number of classes in dataset %3E= number of classes in classifier%0A     * @param classifier%0A     * @param dataSet%0A     */%0A    public MLConfusionMatrix(MultiLabelClassifier classifier, MultiLabelClfDataSet dataSet)%7B%0A        this(dataSet.getNumClasses(),dataSet.getMultiLabels(),classifier.predict(dataSet));%0A    %7D%0A%0A    public enum Entry%7B%0A        TP, TN, FP, FN%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        final StringBuilder sb = new StringBuilder(%22MLConfusionMatrix%7B%22);%0A        sb.append(%22numClasses=%22).append(numClasses);%0A        sb.append(%22, entries=%22).append(Arrays.deepToString(entries));%0A        sb.append('%7D');%0A        return sb.toString();%0A    %7D%0A%7D%0A

add MLConfusionMatrix