Search in sources :

Example 1 with Matrix

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

the class F1PredictorTest method test2.

private static void test2() {
    int numLabels = 5;
    Matrix matrix = LossMatrixGenerator.matrix(numLabels, "f1");
    List<MultiLabel> multiLabels = Enumerator.enumerate(numLabels);
    List<Double> dis = LossMatrixGenerator.sampleDistribution(numLabels);
    GeneralF1Predictor generalF1Predictor = new GeneralF1Predictor();
    generalF1Predictor.setMaxSize(3);
    MultiLabel pred = generalF1Predictor.predict(numLabels, multiLabels, dis);
    MultiLabel search = GeneralF1Predictor.exhaustiveSearch(numLabels, matrix, dis);
    System.out.println("pred = " + pred);
    System.out.println("search = " + search);
}
Also used : Matrix(org.apache.mahout.math.Matrix)

Example 2 with Matrix

use of org.apache.mahout.math.Matrix 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

Matrix (org.apache.mahout.math.Matrix)2 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