use of edu.neu.ccs.pyramid.eval.MLConfusionMatrix 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;
}
Aggregations