@Test public void testBuild(){ function.build(IDEAL_LABELS, LABEL_CONFIDENCES); double[] parameters = function.getFunctionParameters(); assertNotNull("Parameters learned by the function should not be null.", function.getFunctionParameters()); assertTrue("Parameters learned by the function are not as expected.", parameters.length == NUM_LABELS + 1); }
public void build(final double[][] idealLabels, final double[][] modelOutLabels) { if (idealLabels == null || modelOutLabels == null) { throw new IllegalArgumentException("Non of the input parameters can be null."); } int numExamples = idealLabels.length; int numLabels = idealLabels[0].length; if (modelOutLabels.length != numExamples || modelOutLabels[0].length != numLabels) { throw new IllegalArgumentException("Matrix dimensions of input parameters does not agree."); } double[] thresholds = new double[numExamples]; double[] isLabelModelOuts = new double[numLabels]; double[] isNotLabelModelOuts = new double[numLabels]; for (int example = 0; example < numExamples; example++) { Arrays.fill(isLabelModelOuts, Double.MAX_VALUE); Arrays.fill(isNotLabelModelOuts, -Double.MAX_VALUE); for (int label = 0; label < numLabels; label++) { if (idealLabels[example][label] == 1) { isLabelModelOuts[label] = modelOutLabels[example][label]; } else { isNotLabelModelOuts[label] = modelOutLabels[example][label]; } } double isLabelMin = isLabelModelOuts[Utils.minIndex(isLabelModelOuts)]; double isNotLabelMax = isNotLabelModelOuts[Utils.maxIndex(isNotLabelModelOuts)]; if (isLabelMin != isNotLabelMax) { if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { thresholds[example] = isLabelMin; } } Matrix modelMatrix = new Matrix(numExamples, numLabels + 1, 1.0); modelMatrix.setMatrix(0, numExamples - 1, 0, numLabels - 1, new Matrix(modelOutLabels)); Matrix weights = modelMatrix.solve(new Matrix(thresholds, thresholds.length)); double[][] weightsArray = weights.transpose().getArray(); parameters = Arrays.copyOf(weightsArray[0], weightsArray[0].length); }
ThresholdFunction implements Serializable { public void build(final double[][] idealLabels, final double[][] modelOutLabels) { if (idealLabels == null || modelOutLabels == null) { throw new IllegalArgumentException("Non of the input parameters can be null."); } int numExamples = idealLabels.length; int numLabels = idealLabels[0].length; if (modelOutLabels.length != numExamples || modelOutLabels[0].length != numLabels) { throw new IllegalArgumentException("Matrix dimensions of input parameters does not agree."); } double[] thresholds = new double[numExamples]; double[] isLabelModelOuts = new double[numLabels]; double[] isNotLabelModelOuts = new double[numLabels]; for (int example = 0; example < numExamples; example++) { Arrays.fill(isLabelModelOuts, Double.MAX_VALUE); Arrays.fill(isNotLabelModelOuts, -Double.MAX_VALUE); for (int label = 0; label < numLabels; label++) { if (idealLabels[example][label] == 1) { isLabelModelOuts[label] = modelOutLabels[example][label]; } else { isNotLabelModelOuts[label] = modelOutLabels[example][label]; } } double isLabelMin = isLabelModelOuts[Utils.minIndex(isLabelModelOuts)]; double isNotLabelMax = isNotLabelModelOuts[Utils.maxIndex(isNotLabelModelOuts)]; if (isLabelMin != isNotLabelMax) { if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { thresholds[example] = isLabelMin; } } Matrix modelMatrix = new Matrix(numExamples, numLabels + 1, 1.0); modelMatrix.setMatrix(0, numExamples - 1, 0, numLabels - 1, new Matrix(modelOutLabels)); Matrix weights = modelMatrix.solve(new Matrix(thresholds, thresholds.length)); double[][] weightsArray = weights.transpose().getArray(); parameters = Arrays.copyOf(weightsArray[0], weightsArray[0].length); } }
ThresholdFunction implements Serializable { public void build(final double[][] idealLabels, final double[][] modelOutLabels) { if (idealLabels == null || modelOutLabels == null) { throw new IllegalArgumentException("Non of the input parameters can be null."); } int numExamples = idealLabels.length; int numLabels = idealLabels[0].length; if (modelOutLabels.length != numExamples || modelOutLabels[0].length != numLabels) { throw new IllegalArgumentException("Matrix dimensions of input parameters does not agree."); } double[] thresholds = new double[numExamples]; double[] isLabelModelOuts = new double[numLabels]; double[] isNotLabelModelOuts = new double[numLabels]; for (int example = 0; example < numExamples; example++) { Arrays.fill(isLabelModelOuts, Double.MAX_VALUE); Arrays.fill(isNotLabelModelOuts, -Double.MAX_VALUE); for (int label = 0; label < numLabels; label++) { if (idealLabels[example][label] == 1) { isLabelModelOuts[label] = modelOutLabels[example][label]; } else { isNotLabelModelOuts[label] = modelOutLabels[example][label]; } } double isLabelMin = isLabelModelOuts[Utils.minIndex(isLabelModelOuts)]; double isNotLabelMax = isNotLabelModelOuts[Utils.maxIndex(isNotLabelModelOuts)]; if (isLabelMin != isNotLabelMax) { if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { thresholds[example] = isLabelMin; } } Matrix modelMatrix = new Matrix(numExamples, numLabels + 1, 1.0); modelMatrix.setMatrix(0, numExamples - 1, 0, numLabels - 1, new Matrix(modelOutLabels)); Matrix weights = modelMatrix.solve(new Matrix(thresholds, thresholds.length)); double[][] weightsArray = weights.transpose().getArray(); parameters = Arrays.copyOf(weightsArray[0], weightsArray[0].length); } ThresholdFunction(final double[][] idealLabels, final double[][] modelOutLabels); }
ThresholdFunction implements Serializable { public void build(final double[][] idealLabels, final double[][] modelOutLabels) { if (idealLabels == null || modelOutLabels == null) { throw new IllegalArgumentException("Non of the input parameters can be null."); } int numExamples = idealLabels.length; int numLabels = idealLabels[0].length; if (modelOutLabels.length != numExamples || modelOutLabels[0].length != numLabels) { throw new IllegalArgumentException("Matrix dimensions of input parameters does not agree."); } double[] thresholds = new double[numExamples]; double[] isLabelModelOuts = new double[numLabels]; double[] isNotLabelModelOuts = new double[numLabels]; for (int example = 0; example < numExamples; example++) { Arrays.fill(isLabelModelOuts, Double.MAX_VALUE); Arrays.fill(isNotLabelModelOuts, -Double.MAX_VALUE); for (int label = 0; label < numLabels; label++) { if (idealLabels[example][label] == 1) { isLabelModelOuts[label] = modelOutLabels[example][label]; } else { isNotLabelModelOuts[label] = modelOutLabels[example][label]; } } double isLabelMin = isLabelModelOuts[Utils.minIndex(isLabelModelOuts)]; double isNotLabelMax = isNotLabelModelOuts[Utils.maxIndex(isNotLabelModelOuts)]; if (isLabelMin != isNotLabelMax) { if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { thresholds[example] = isLabelMin; } } Matrix modelMatrix = new Matrix(numExamples, numLabels + 1, 1.0); modelMatrix.setMatrix(0, numExamples - 1, 0, numLabels - 1, new Matrix(modelOutLabels)); Matrix weights = modelMatrix.solve(new Matrix(thresholds, thresholds.length)); double[][] weightsArray = weights.transpose().getArray(); parameters = Arrays.copyOf(weightsArray[0], weightsArray[0].length); } ThresholdFunction(final double[][] idealLabels, final double[][] modelOutLabels); double computeThreshold(final double[] labelsConfidences); void build(final double[][] idealLabels, final double[][] modelOutLabels); }
ThresholdFunction implements Serializable { public void build(final double[][] idealLabels, final double[][] modelOutLabels) { if (idealLabels == null || modelOutLabels == null) { throw new IllegalArgumentException("Non of the input parameters can be null."); } int numExamples = idealLabels.length; int numLabels = idealLabels[0].length; if (modelOutLabels.length != numExamples || modelOutLabels[0].length != numLabels) { throw new IllegalArgumentException("Matrix dimensions of input parameters does not agree."); } double[] thresholds = new double[numExamples]; double[] isLabelModelOuts = new double[numLabels]; double[] isNotLabelModelOuts = new double[numLabels]; for (int example = 0; example < numExamples; example++) { Arrays.fill(isLabelModelOuts, Double.MAX_VALUE); Arrays.fill(isNotLabelModelOuts, -Double.MAX_VALUE); for (int label = 0; label < numLabels; label++) { if (idealLabels[example][label] == 1) { isLabelModelOuts[label] = modelOutLabels[example][label]; } else { isNotLabelModelOuts[label] = modelOutLabels[example][label]; } } double isLabelMin = isLabelModelOuts[Utils.minIndex(isLabelModelOuts)]; double isNotLabelMax = isNotLabelModelOuts[Utils.maxIndex(isNotLabelModelOuts)]; if (isLabelMin != isNotLabelMax) { if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { thresholds[example] = isLabelMin; } } Matrix modelMatrix = new Matrix(numExamples, numLabels + 1, 1.0); modelMatrix.setMatrix(0, numExamples - 1, 0, numLabels - 1, new Matrix(modelOutLabels)); Matrix weights = modelMatrix.solve(new Matrix(thresholds, thresholds.length)); double[][] weightsArray = weights.transpose().getArray(); parameters = Arrays.copyOf(weightsArray[0], weightsArray[0].length); } ThresholdFunction(final double[][] idealLabels, final double[][] modelOutLabels); double computeThreshold(final double[] labelsConfidences); void build(final double[][] idealLabels, final double[][] modelOutLabels);  }