Example usage for weka.core.matrix Matrix getMatrix

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

Introduction

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

Prototype

public Matrix getMatrix(int i0, int i1, int j0, int j1) 

Source Link

Document

Get a submatrix.

Usage

From source file:adams.data.instancesanalysis.pls.SIMPLS.java

License:Open Source License

/**
 * Zeroes the coefficients of the W matrix beyond the specified number of
 * coefficients./*from   w ww .j a v  a2  s  . c o  m*/
 *
 * @param in      the matrix to process in-place
 */
protected void slim(Matrix in) {
    double[][] B = in.getArray();

    for (int i = 0; i < in.getColumnDimension(); i++) {
        Matrix l = in.getMatrix(0, in.getRowDimension() - 1, i, i);
        double[] ld = l.getRowPackedCopy();
        for (int t = 0; t < ld.length; t++) {
            ld[t] = Math.abs(ld[t]);
        }
        int[] srt = weka.core.Utils.sort(ld);
        //int index = srt.length - 1 - srt[Math.min(getNumCoefficients(),srt.length-1)]; //nonono
        int index = srt[Math.max(srt.length - 1 - getNumCoefficients(), 0)];

        double val = ld[index];
        for (int c = 0; c < in.getRowDimension(); c++) {
            if (Math.abs(B[c][i]) < val) {
                B[c][i] = 0;
            }
        }
    }
}

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

License:Open Source License

public void run() throws Exception {
    /*//from   www  .ja v  a2 s.  c o  m
     * 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);
        }
    }

}