List of usage examples for weka.core.matrix Matrix solve
public Matrix solve(Matrix B)
From source file:mulan.classifier.neural.ThresholdFunction.java
License:Open Source License
/** * Build a threshold function for based on input data. * The threshold function is build for a particular model. * * @param idealLabels the ideal output for each input patterns, which a model should output. * First index is expected to be number of examples and second is the label index. * @param modelOutLabels the real output of a model for each input pattern. * First index is expected to be number of examples and second is the label index. * @throws IllegalArgumentException if dimensions of input arrays does not match */// ww w. j a v a2 s .c o m 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)]; // check if we have unique minimum ... // if not take center of the segment ... if it is a segment if (isLabelMin != isNotLabelMax) { // check marginal cases -> all labels are in or none of them if (isLabelMin == Double.MAX_VALUE) { thresholds[example] = isNotLabelMax + 0.1; } else if (isNotLabelMax == -Double.MAX_VALUE) { thresholds[example] = isLabelMin - 0.1; } else { // center of a segment thresholds[example] = (isLabelMin + isNotLabelMax) / 2; } } else { // when minimum is unique 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); }