Example usage for weka.core.matrix Matrix setMatrix

List of usage examples for weka.core.matrix Matrix setMatrix

Introduction

In this page you can find the example usage for weka.core.matrix Matrix setMatrix.

Prototype

public void setMatrix(int i0, int i1, int j0, int j1, Matrix X) 

Source Link

Document

Set a submatrix.

Usage

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
 *//*from ww  w  . j a v  a  2  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);
}

From source file:org.mitre.clustering.AffinityPropagation.java

License:Open Source License

public void run() throws Exception {
    /*/*from w  ww  .  j a  va  2  s  . c om*/
     * Allocate space for messages
     */
    Matrix ds;
    try {
        ds = getDiag(s);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    A = new Matrix(count, count, 0.0);
    R = new Matrix(count, count, 0.0);
    int t = 1;

    /*
     * Run parralel affinity prop. updates
     */
    Matrix e = new Matrix(count, convits, 0);
    Boolean dn = false;
    int i = -1; // adjusted due to difference in MATLAB indexing
    Matrix st;
    if (!symmetric) {
        st = s;
    } else {
        st = s.transpose();
    }

    while (!dn) {
        i = i + 1;

        A = A.transpose();
        R = R.transpose();
        /*
         * Compute responsibilities
         */
        for (int ii = 0; ii < count; ii++) {
            Matrix old = R.getMatrix(0, count - 1, ii, ii);
            Matrix AS = A.getMatrix(0, count - 1, ii, ii).plus(st.getMatrix(0, count - 1, ii, ii));

            int I = getMaxIndex(AS);
            double Y = AS.get(I, 0);
            /*
             * Need to use -Double.MAX_VALUE here, as this will return
             * the smallest NEGATIVE number, as opposed to smallest absolute
             * floating point number
             */
            AS.set(I, 0, -Double.MAX_VALUE);

            int I2 = getMaxIndex(AS);
            double Y2 = AS.get(I2, 0);

            /*
             * Set R
             */
            Matrix yMat = new Matrix(count, 1, Y);
            Matrix stVect = st.getMatrix(0, count - 1, ii, ii);
            R.setMatrix(0, count - 1, ii, ii, stVect.minus(yMat));
            R.set(I, ii, st.get(I, ii) - Y2);
            Matrix rVect = R.getMatrix(0, count - 1, ii, ii);
            rVect = rVect.times(1 - lam).plus(old.times(lam));
            R.setMatrix(0, count - 1, ii, ii, rVect);
        }
        A = A.transpose();
        R = R.transpose();

        /*
         * Compute availabilities
         */
        for (int jj = 0; jj < count; jj++) {
            Matrix old = A.getMatrix(0, count - 1, jj, jj);
            Matrix Rp = new Matrix(count, 1, 0);
            for (int c = 0; c < count; c++) {
                if (R.get(c, jj) < 0.0) {
                    Rp.set(c, 0, 0.0);
                } else {
                    Rp.set(c, 0, R.get(c, jj));
                }
            }

            Rp.set(jj, 0, R.get(jj, jj));
            Matrix sumM = new Matrix(count, 1, getSum(Rp));
            A.setMatrix(0, count - 1, jj, jj, sumM.minus(Rp));

            double dA = A.get(jj, jj);

            Matrix aVect = A.getMatrix(0, count - 1, jj, jj);
            for (int c = 0; c < count; c++) {
                if (aVect.get(c, 0) > 0) {
                    aVect.set(c, 0, 0.0);
                }
            }
            A.setMatrix(0, count - 1, jj, jj, aVect);
            A.set(jj, jj, dA);

            aVect = A.getMatrix(0, count - 1, jj, jj);
            aVect = aVect.times(1 - lam).plus(old.times(lam));
            A.setMatrix(0, count - 1, jj, jj, aVect);

        }

        /*
         * Check for convergence
         */
        Matrix E = null;
        try {
            E = getDiag(A).plus(getDiag(R));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        for (int c = 0; c < count; c++) {
            if (E.get(c, 0) > 0) {
                E.set(c, 0, 1.0);
            } else {
                E.set(c, 0, 0.0);
            }
        }

        int index = i % convits;

        e.setMatrix(0, count - 1, index, index, E);

        K = getSum(E);

        if (i >= convits || i >= maxits) {
            Matrix se = new Matrix(count, 1);
            for (int c = 0; c < count; c++) {
                double sum = 0;
                for (int c2 = 0; c2 < convits; c2++) {
                    sum += e.get(c, c2);
                }
                se.set(c, 0, sum);
            }

            Matrix seC = matrixIsEqual(se, convits);
            Matrix seZ = matrixIsEqual(se, 0.0);

            double value = getSum(seC.plus(seZ));

            if (value == count) {
                unconverged = false;
            } else {
                unconverged = true;
            }

            if ((!unconverged && K > 0) || i == maxits) {
                dn = true;
            }

        }

    }

    /*
     * Identify exemplars
     */
    Matrix diag = null;
    try {
        diag = getDiag(A).plus(getDiag(R));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    exemplars = new Vector<Integer>();

    for (int c = 0; c < count; c++) {
        if (diag.get(c, 0) > 0) {
            exemplars.add(c);
        }
    }

}