private int getNoOfClasses(MLModel mlModel) { if (mlModel.getEncodings() == null) { return -1; } int responseIndex = mlModel.getEncodings().size() - 1; return mlModel.getEncodings().get(responseIndex) != null ? mlModel.getEncodings().get(responseIndex).size() : -1; }
/** * This method returns multiclass confusion matrix for a given multiclass metric object * * @param multiclassMetrics Multiclass metric object */ private MulticlassConfusionMatrix getMulticlassConfusionMatrix( MulticlassMetrics multiclassMetrics, MLModel mlModel) { MulticlassConfusionMatrix multiclassConfusionMatrix = new MulticlassConfusionMatrix(); if (multiclassMetrics != null) { int size = multiclassMetrics.confusionMatrix().numCols(); double[] matrixArray = multiclassMetrics.confusionMatrix().toArray(); double[][] matrix = new double[size][size]; // set values of matrix into a 2D array for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { matrix[i][j] = matrixArray[(j * size) + i]; } } multiclassConfusionMatrix.setMatrix(matrix); List<Map<String, Integer>> encodings = mlModel.getEncodings(); // decode only if encodings are available if (encodings != null) { // last index is response variable encoding Map<String, Integer> encodingMap = encodings.get(encodings.size() - 1); List<String> decodedLabels = new ArrayList<String>(); for (double label : multiclassMetrics.labels()) { Integer labelInt = (int) label; String decodedLabel = MLUtils.getKeyByValue(encodingMap, labelInt); if (decodedLabel != null) { decodedLabels.add(decodedLabel); } else { continue; } } multiclassConfusionMatrix.setLabels(decodedLabels); } else { List<String> labelList = toStringList(multiclassMetrics.labels()); multiclassConfusionMatrix.setLabels(labelList); } multiclassConfusionMatrix.setSize(size); } return multiclassConfusionMatrix; }