Example usage for org.apache.commons.math3.linear RealMatrix getSubMatrix

List of usage examples for org.apache.commons.math3.linear RealMatrix getSubMatrix

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getSubMatrix.

Prototype

RealMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn)
        throws OutOfRangeException, NumberIsTooSmallException;

Source Link

Document

Gets a submatrix.

Usage

From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java

public static RealMatrix nullspace(final RealMatrix matrix) {
    final SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
    final int rank = svd.getRank();
    final RealMatrix V = svd.getV();
    RealMatrix nullspace = null;//w ww.ja va  2 s . c o  m
    if (rank < matrix.getColumnDimension()) {
        nullspace = V.getSubMatrix(0, V.getRowDimension() - 1, rank, V.getColumnDimension() - 1);
    }

    return nullspace;
}

From source file:org.knime.al.util.noveltydetection.knfst.MultiClassKNFST.java

public MultiClassKNFST(final KernelCalculator kernel, final String[] labels, final ExecutionMonitor progMon)
        throws Exception {
    super(kernel);
    m_labels = labels;/*  w w  w.  j a  va2  s  .  c om*/

    final ExecutionMonitor kernelProgMon = progMon.createSubProgress(0.3);
    final ExecutionMonitor nullspaceProgMon = progMon.createSubProgress(0.7);
    final RealMatrix kernelMatrix = kernel.kernelize(kernelProgMon);

    // obtain unique class labels
    final ClassWrapper[] classes = ClassWrapper.classes(labels);

    // calculate projection of KNFST
    nullspaceProgMon.setMessage("Calculating nullspace projection");
    m_projection = projection(kernelMatrix, labels);

    nullspaceProgMon.setProgress(1.0, "Finished calculating nullspace projection");

    // calculate target points ( = projections of training data into the
    // null space)
    m_targetPoints = MatrixUtils.createRealMatrix(classes.length, m_projection.getColumnDimension());
    int n = 0;
    int nOld = 0;
    for (int c = 0; c < classes.length; c++) {
        n += classes[c].getCount();
        m_targetPoints.setRowVector(c, MatrixFunctions.columnMeans(kernelMatrix
                .getSubMatrix(nOld, n - 1, 0, kernelMatrix.getColumnDimension() - 1).multiply(m_projection)));
        nOld = n;
    }

    // set betweenClassDistances
    m_betweenClassDistances = MatrixFunctions.calculateRowVectorDistances(m_targetPoints);

}

From source file:org.knime.al.util.noveltydetection.knfst.MultiClassKNFST.java

public MultiClassKNFST(final RealMatrix kernelMatrix, final String[] labels) throws KNFSTException {
    m_labels = labels;//from   www.j  a va 2s  .c om
    // obtain unique class labels
    final ClassWrapper[] classes = ClassWrapper.classes(labels);

    // calculate projection of KNFST
    m_projection = projection(kernelMatrix, labels);

    // calculate target points ( = projections of training data into the
    // null space)
    m_targetPoints = MatrixUtils.createRealMatrix(classes.length, m_projection.getColumnDimension());
    int n = 0;
    int nOld = 0;
    for (int c = 0; c < classes.length; c++) {
        n += classes[c].getCount();
        m_targetPoints.setRowVector(c, MatrixFunctions.columnMeans(kernelMatrix
                .getSubMatrix(nOld, n - 1, 0, kernelMatrix.getColumnDimension() - 1).multiply(m_projection)));
        nOld = n;
    }

    // set betweenClassDistances
    m_betweenClassDistances = MatrixFunctions.calculateRowVectorDistances(m_targetPoints);
}

From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java

public OneClassKNFST(final KernelCalculator kernel, final ExecutionMonitor progMon) throws Exception {
    super(kernel);

    final ExecutionMonitor kernelProgMon = progMon.createSubProgress(0.3);
    final ExecutionMonitor nullspaceProgMon = progMon.createSubProgress(0.7);

    // get number of training samples
    final RealMatrix kernelMatrix = m_kernel.kernelize(kernelProgMon);
    final int n = kernelMatrix.getRowDimension();

    // include dot products of training samples and the origin in feature
    // space (these dot products are always zero!)
    final RealMatrix k = MatrixFunctions.concatVertically(
            MatrixFunctions.concatHorizontally(kernelMatrix,
                    MatrixUtils.createRealMatrix(kernelMatrix.getRowDimension(), 1)),
            MatrixUtils.createRealMatrix(1, kernelMatrix.getColumnDimension() + 1));

    // create one-class labels + a different label for the origin
    final String[] labels = new String[n + 1];
    for (int l = 0; l <= n; l++) {
        labels[l] = (l == n) ? "0" : "1";
    }//  w  w  w.j  a v a  2  s  .  c  om

    // get model parameters
    nullspaceProgMon.setMessage("Calculating nullspace projection");
    final RealMatrix projection = projection(k, labels);
    nullspaceProgMon.setProgress(1.0, "Finished calculating nullspace projection");
    final int[] indices = new int[n];
    for (int i = 0; i < n; i++) {
        indices[i] = i;
    }
    m_targetPoints = MatrixUtils.createRowRealMatrix(MatrixFunctions
            .columnMeans(k.getSubMatrix(0, n - 1, 0, k.getColumnDimension() - 1).multiply(projection))
            .toArray());
    m_projection = projection.getSubMatrix(0, n - 1, 0, projection.getColumnDimension() - 1);
    m_betweenClassDistances = new double[] { Math.abs(m_targetPoints.getEntry(0, 0)) };
}

From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java

public OneClassKNFST(final RealMatrix kernelMatrix) throws KNFSTException {
    final int n = kernelMatrix.getRowDimension();

    // include dot products of training samples and the origin in feature
    // space (these dot products are always zero!)
    final RealMatrix k = MatrixFunctions.concatVertically(
            MatrixFunctions.concatHorizontally(kernelMatrix,
                    MatrixUtils.createRealMatrix(kernelMatrix.getRowDimension(), 1)),
            MatrixUtils.createRealMatrix(1, kernelMatrix.getColumnDimension() + 1));

    // create one-class labels + a different label for the origin
    final String[] labels = new String[n + 1];
    for (int l = 0; l <= n; l++) {
        labels[l] = (l == n) ? "0" : "1";
    }//w  w w. ja  v a 2 s . com

    // get model parameters
    final RealMatrix projection = projection(k, labels);
    final int[] indices = new int[n];
    for (int i = 0; i < n; i++) {
        indices[i] = i;
    }
    m_targetPoints = MatrixUtils.createRowRealMatrix(MatrixFunctions
            .columnMeans(k.getSubMatrix(0, n - 1, 0, k.getColumnDimension() - 1).multiply(projection))
            .toArray());
    m_projection = projection.getSubMatrix(0, n - 1, 0, projection.getColumnDimension() - 1);
    m_betweenClassDistances = new double[] { Math.abs(m_targetPoints.getEntry(0, 0)) };
}