Search in sources :

Example 1 with DenseMatrix

use of org.apache.mahout.math.DenseMatrix in project pyramid by cheng-li.

the class LossMatrixGenerator method matrix.

public static Matrix matrix(int n, String lossName) {
    int size = (int) Math.pow(2, n);
    double[][] matrixBuilder = new double[size][size];
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            String ib = toBinary(i, n);
            String jb = toBinary(j, n);
            MultiLabel multiLabel1 = toML(ib);
            MultiLabel multiLabel2 = toML(jb);
            MultiLabel[] trueLabels = { multiLabel1 };
            MultiLabel[] predicted = { multiLabel2 };
            MLConfusionMatrix mlConfusionMatrix = new MLConfusionMatrix(n, trueLabels, predicted);
            InstanceAverage instanceAverage = new InstanceAverage(mlConfusionMatrix);
            double loss;
            switch(lossName.toLowerCase()) {
                case "hamming":
                    loss = instanceAverage.getHammingLoss() * n;
                    break;
                case "overlap":
                    loss = 1 - instanceAverage.getOverlap();
                    break;
                case "accuracy":
                    loss = 1 - instanceAverage.getAccuracy();
                    break;
                case "precision":
                    loss = 1 - instanceAverage.getPrecision();
                    break;
                case "recall":
                    loss = 1 - instanceAverage.getRecall();
                    break;
                case "f1":
                    loss = 1 - instanceAverage.getF1();
                    break;
                default:
                    throw new IllegalArgumentException("unknown loss");
            }
            matrixBuilder[i][j] = loss;
        }
    }
    Matrix matrix = new DenseMatrix(matrixBuilder);
    return matrix;
}
Also used : MLConfusionMatrix(edu.neu.ccs.pyramid.eval.MLConfusionMatrix) MultiLabel(edu.neu.ccs.pyramid.dataset.MultiLabel) DenseMatrix(org.apache.mahout.math.DenseMatrix) MLConfusionMatrix(edu.neu.ccs.pyramid.eval.MLConfusionMatrix) Matrix(org.apache.mahout.math.Matrix) InstanceAverage(edu.neu.ccs.pyramid.eval.InstanceAverage) DenseMatrix(org.apache.mahout.math.DenseMatrix)

Aggregations

MultiLabel (edu.neu.ccs.pyramid.dataset.MultiLabel)1 InstanceAverage (edu.neu.ccs.pyramid.eval.InstanceAverage)1 MLConfusionMatrix (edu.neu.ccs.pyramid.eval.MLConfusionMatrix)1 DenseMatrix (org.apache.mahout.math.DenseMatrix)1 Matrix (org.apache.mahout.math.Matrix)1