Example usage for org.apache.commons.math3.linear MatrixUtils createRowRealMatrix

List of usage examples for org.apache.commons.math3.linear MatrixUtils createRowRealMatrix

Introduction

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

Prototype

public static RealMatrix createRowRealMatrix(double[] rowData) throws NoDataException, NullArgumentException 

Source Link

Document

Create a row RealMatrix using the data from the input array.

Usage

From source file:com.yahoo.egads.utilities.SpectralMethods.java

public static RealMatrix createHankelMatrix(RealMatrix data, int windowSize) {

    int n = data.getRowDimension();
    int m = data.getColumnDimension();
    int k = n - windowSize + 1;

    RealMatrix res = MatrixUtils.createRealMatrix(k, m * windowSize);
    double[] buffer = {};

    for (int i = 0; i < n; ++i) {
        double[] row = data.getRow(i);
        buffer = ArrayUtils.addAll(buffer, row);

        if (i >= windowSize - 1) {
            RealMatrix mat = MatrixUtils.createRowRealMatrix(buffer);
            res.setRowMatrix(i - windowSize + 1, mat);
            buffer = ArrayUtils.subarray(buffer, m, buffer.length);
        }/*from w  w  w . j a  v a 2  s.  c o  m*/
    }

    return res;
}

From source file:com.github.tteofili.looseen.yay.SGM.java

private RealMatrix[] initBiases() {

    RealMatrix[] initialBiases = new RealMatrix[weights.length];

    for (int i = 0; i < initialBiases.length; i++) {
        double[] data = new double[weights[i].getRowDimension()];
        Arrays.fill(data, 0.01d);
        RealMatrix matrix = MatrixUtils.createRowRealMatrix(data);

        initialBiases[i] = matrix;//from www .  j a v a  2  s  .c  o m
    }
    return initialBiases;
}

From source file:com.github.tteofili.looseen.yay.SGM.java

/**
 * predict network output given an input
 *
 * @param input the input/*from w ww  .j a v  a 2s. c om*/
 * @return the output
 * @throws Exception
 */
private double[] predictOutput(double[] input) throws Exception {

    RealMatrix hidden = rectifierFunction.applyMatrix(
            MatrixUtils.createRowRealMatrix(input).multiply(weights[0].transpose()).add(biases[0]));
    RealMatrix scores = hidden.multiply(weights[1].transpose()).add(biases[1]);

    RealMatrix probs = scores.copy();
    int len = scores.getColumnDimension() - 1;
    for (int d = 0; d < configuration.window - 1; d++) {
        int startColumn = d * len / (configuration.window - 1);
        RealMatrix subMatrix = scores.getSubMatrix(0, scores.getRowDimension() - 1, startColumn,
                startColumn + input.length);
        for (int sm = 0; sm < subMatrix.getRowDimension(); sm++) {
            probs.setSubMatrix(softmaxActivationFunction.applyMatrix(subMatrix.getRowMatrix(sm)).getData(), sm,
                    startColumn);
        }
    }

    RealVector d = probs.getRowVector(0);
    return d.toArray();
}

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";
    }/*from   w ww  .  java 2s  .c o  m*/

    // 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";
    }/*from  w w w  .j  ava  2s . c o  m*/

    // 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)) };
}