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

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

Introduction

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

Prototype

double[] getRow(int row) throws OutOfRangeException;

Source Link

Document

Get the entries at the given row index.

Usage

From source file:net.myrrix.online.factorizer.als.AlternatingLeastSquaresTest.java

@Test
public void testALS() throws Exception {
    RealMatrix product = buildTestXYTProduct(false);

    assertArrayEquals(new float[] { -0.030258f, 0.852781f, 1.004839f, 1.024087f, -0.036206f },
            product.getRow(0));
    assertArrayEquals(new float[] { 0.077046f, 0.751232f, 0.949796f, 0.910322f, 0.073047f }, product.getRow(1));
    assertArrayEquals(new float[] { 0.916777f, -0.196005f, 0.335926f, -0.163591f, 0.929028f },
            product.getRow(2));//w ww  .  j ava 2s. co m
    assertArrayEquals(new float[] { 0.987400f, 0.130943f, 0.772403f, 0.235522f, 0.998354f }, product.getRow(3));
    assertArrayEquals(new float[] { -0.028683f, 0.850540f, 1.003130f, 1.021514f, -0.034598f },
            product.getRow(4));
}

From source file:net.myrrix.online.factorizer.als.AlternatingLeastSquaresTest.java

@Test
public void testALSPredictingR() throws Exception {
    RealMatrix product = buildTestXYTProduct(true);

    assertArrayEquals(new float[] { 0.0678369f, 0.6574759f, 2.1020291f, 2.0976211f, 0.1115919f },
            product.getRow(0));
    assertArrayEquals(new float[] { -0.0176293f, 1.3062225f, 4.1365933f, 4.1739127f, -0.0380586f },
            product.getRow(1));/*from  w  w w . j  av a  2 s.  c om*/
    assertArrayEquals(new float[] { 1.0854513f, -0.0344434f, 0.1725342f, -0.1564803f, 1.8502977f },
            product.getRow(2));
    assertArrayEquals(new float[] { 2.8377915f, 0.0528524f, 0.9041158f, 0.0474437f, 4.8365208f },
            product.getRow(3));
    assertArrayEquals(new float[] { -0.0057799f, 0.6608552f, 2.0936351f, 2.1115670f, -0.0139042f, },
            product.getRow(4));
}

From source file:net.myrrix.online.factorizer.als.NegativeInputTest.java

@Test
public void testALS() throws Exception {

    FastByIDMap<FastByIDFloatMap> byRow = new FastByIDMap<FastByIDFloatMap>();
    FastByIDMap<FastByIDFloatMap> byCol = new FastByIDMap<FastByIDFloatMap>();

    // Octave: R = [ 1 1 1 0 ; 0 -1 1 1 ; -1 0 0 1 ]
    MatrixUtils.addTo(0, 0, 1.0f, byRow, byCol);
    MatrixUtils.addTo(0, 1, 1.0f, byRow, byCol);
    MatrixUtils.addTo(0, 2, 1.0f, byRow, byCol);
    MatrixUtils.addTo(1, 1, -1.0f, byRow, byCol);
    MatrixUtils.addTo(1, 2, 1.0f, byRow, byCol);
    MatrixUtils.addTo(1, 3, 1.0f, byRow, byCol);
    MatrixUtils.addTo(2, 0, -1.0f, byRow, byCol);
    MatrixUtils.addTo(2, 3, 1.0f, byRow, byCol);

    // Octave: Y = [ 0.1 0.2 ; 0.2 0.5 ; 0.3 0.1 ; 0.2 0.2 ];
    FastByIDMap<float[]> previousY = new FastByIDMap<float[]>();
    previousY.put(0L, new float[] { 0.1f, 0.2f });
    previousY.put(1L, new float[] { 0.2f, 0.5f });
    previousY.put(2L, new float[] { 0.3f, 0.1f });
    previousY.put(3L, new float[] { 0.2f, 0.2f });

    MatrixFactorizer als = new AlternatingLeastSquares(byRow, byCol, 2, 0.0001, 40);
    als.setPreviousY(previousY);/*from   ww w . ja v a2s. c  o m*/
    als.call();

    RealMatrix product = MatrixUtils.multiplyXYT(als.getX(), als.getY());

    StringBuilder productString = new StringBuilder(100);
    for (int row = 0; row < product.getRowDimension(); row++) {
        productString.append('\n').append(Arrays.toString(doubleToFloatArray(product.getRow(row))));
    }
    log.info("{}", productString);

    assertArrayEquals(new float[] { 0.899032f, 0.900162f, 0.990150f, -0.026642f }, product.getRow(0));
    assertArrayEquals(new float[] { 0.181214f, 0.089988f, 0.787198f, 1.012226f }, product.getRow(1));
    assertArrayEquals(new float[] { -0.104165f, -0.178240f, 0.360391f, 0.825856f }, product.getRow(2));
}

From source file:gr.iti.mklab.reveal.forensics.maps.dwnoisevar.DWNoiseVarExtractor.java

public void getNoiseMap() throws IOException {

    BufferedImage img = inputImage;

    int imWidth, imHeight;
    if (img.getWidth() % 2 == 0) {
        imWidth = img.getWidth();//from www. j  ava  2 s.c  om
    } else {
        imWidth = (img.getWidth() - 1);
    }
    if (img.getHeight() % 2 == 0) {
        imHeight = img.getHeight();
    } else {
        imHeight = (img.getHeight() - 1);
    }

    int columnFilterScale = (int) (Math.log(imHeight) / Math.log(2)) - 1;
    int rowFilterScale = (int) (Math.log(imWidth) / Math.log(2)) - 1;

    double[][] imgYAsArray = new double[imWidth][imHeight];
    double[][] filteredImgYAsArray = new double[imWidth][imHeight / 2];
    double[][] doubleFilteredImgYAsArray = new double[imWidth / 2][imHeight / 2];
    double[] imgColumn, imgRow;
    Color tmpcolor;
    double R, G, B;

    for (int ii = 0; ii < imWidth; ii++) {
        for (int jj = 0; jj < imHeight; jj++) {
            tmpcolor = new Color(img.getRGB(ii, jj));
            R = tmpcolor.getRed();
            G = tmpcolor.getGreen();
            B = tmpcolor.getBlue();
            imgYAsArray[ii][jj] = 0.2989 * R + 0.5870 * G + 0.1140 * B;
        }
    }

    double[] waveletColumn;
    RealMatrix rm = new Array2DRowRealMatrix(imgYAsArray);
    for (int ii = 0; ii < imWidth; ii++) {
        try {
            imgColumn = new double[imHeight];
            imgColumn = rm.getRow(ii);
            //Long startTime1 = System.currentTimeMillis();  
            waveletColumn = DWT.transform(imgColumn, Wavelet.Daubechies, 8, columnFilterScale,
                    DWT.Direction.forward);
            System.arraycopy(waveletColumn, imHeight / 2, filteredImgYAsArray[ii], 0, imHeight / 2);
        } catch (Exception ex) {
            Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    double[] waveletRow;
    RealMatrix rm2 = new Array2DRowRealMatrix(filteredImgYAsArray);
    for (int jj = 0; jj < imHeight / 2; jj++) {
        try {
            imgRow = new double[imWidth];
            imgRow = rm2.getColumn(jj);
            waveletRow = DWT.transform(imgRow, Wavelet.Daubechies, 8, rowFilterScale, DWT.Direction.forward);
            for (int ii = 0; ii < imWidth / 2; ii++) {
                doubleFilteredImgYAsArray[ii][jj] = waveletRow[ii + imWidth / 2];
            }
        } catch (Exception ex) {
            Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    int blockSize = 8;
    double[][] blockNoiseVar = Util.blockNoiseVar(doubleFilteredImgYAsArray, blockSize);

    int medianFilterSize = 7;
    if (medianFilterSize > blockNoiseVar.length) {
        medianFilterSize = blockNoiseVar.length - 1;
    }
    if (medianFilterSize > blockNoiseVar[0].length) {
        medianFilterSize = blockNoiseVar[0].length - 1;
    }
    if (medianFilterSize < 5) {
        minNoiseValue = 0;
        maxNoiseValue = 0;
        noiseMap = new float[1][1];
        noiseMap[0][0] = 0;
        double[][] artificialOutput = new double[1][1];
        artificialOutput[0][0] = 0;
        BufferedImage outputImage = Util.visualizeWithJet(artificialOutput);
        displaySurface = outputImage;
        return;
    }

    float[][] outBlockMap = Util.medianFilterSingleChannelImage(blockNoiseVar, medianFilterSize);

    minNoiseValue = Util.minDouble2DArray(outBlockMap);
    maxNoiseValue = Util.maxDouble2DArray(outBlockMap);
    noiseMap = outBlockMap;
    double[][] normalizedMap = Util.normalizeIm(outBlockMap);
    BufferedImage outputImage = Util.visualizeWithJet(normalizedMap);
    // output
    displaySurface = outputImage;
}

From source file:com.itemanalysis.psychometrics.mixture.MvNormalMixtureModel.java

public void estimateMean(RealMatrix x) {
    VectorialMean m = new VectorialMean(dimensions);
    for (int i = 0; i < x.getRowDimension(); i++) {
        m.increment(x.getRow(i));
    }/*from   w  ww  . ja  va  2  s.  co m*/
    initialMean = new Array2DRowRealMatrix(m.getResult());

}

From source file:com.datumbox.framework.core.machinelearning.featureselection.PCA.java

/** {@inheritDoc} */
@Override/*  www. j av a 2  s  .com*/
protected void _transform(Dataframe newData) {
    ModelParameters modelParameters = knowledgeBase.getModelParameters();

    //convert data into matrix
    Map<Object, Integer> featureIds = modelParameters.getFeatureIds();

    Map<Integer, Integer> recordIdsReference = new HashMap<>();
    DataframeMatrix matrixDataset = DataframeMatrix.parseDataset(newData, recordIdsReference, featureIds);

    RealMatrix components = modelParameters.getComponents();

    //multiplying the data with components
    final RealMatrix X = matrixDataset.getX().multiply(components);

    streamExecutor.forEach(StreamMethods.stream(newData.entries(), isParallelized()), e -> {
        Integer rId = e.getKey();
        Record r = e.getValue();
        int rowId = recordIdsReference.get(rId);

        AssociativeArray xData = new AssociativeArray();
        int componentId = 0;
        for (double value : X.getRow(rowId)) {
            xData.put(componentId++, value);
        }

        Record newR = new Record(xData, r.getY(), r.getYPredicted(), r.getYPredictedProbabilities());

        //we call below the recalculateMeta()
        newData._unsafe_set(rId, newR);
    });

    //recordIdsReference = null;
    //matrixDataset = null;

    newData.recalculateMeta();
}

From source file:eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java

private RealMatrix computeMaxDistanceOnPCs(int index) {

    RealMatrix pc = new Array2DRowRealMatrix(principalComponents[index].toArray());
    RealMatrix transposePC1 = pc.transpose();
    RealMatrix finalDataTranspose = finalMatrixWithoutLowVariantCmds.transpose();

    RealMatrix trainingData = pc.multiply(transposePC1).multiply(finalDataTranspose);
    RealMatrix trainingDataTranspose = trainingData.transpose();

    double maxDistance = 0.0;
    double minDistance = 0.0;
    RealVector p1 = null, p2 = null;//from w ww .  ja  va 2  s  .c  o  m
    if (LOG.isDebugEnabled())
        LOG.debug("Training data transpose size: " + trainingDataTranspose.getRowDimension() + "x"
                + trainingDataTranspose.getColumnDimension());
    for (int i = 0; i < trainingDataTranspose.getRowDimension(); i++) {
        RealVector iRowVector = new ArrayRealVector(trainingDataTranspose.getRow(i));
        RealVector transposePC1Vect = transposePC1.getRowVector(0);
        double distance = iRowVector.getDistance(transposePC1Vect);
        if (distance > maxDistance) {
            maxDistance = distance;
            p1 = iRowVector;
            p2 = transposePC1Vect;
        }
        if (distance < minDistance)
            minDistance = distance;
        //}
    }
    maximumL2Norm.setEntry(index, maxDistance);
    minimumL2Norm.setEntry(index, minDistance);
    minVector = p1;
    maxVector = p2;
    return trainingDataTranspose;
}

From source file:feature.lowLevel.audio.FeatureExtractor.java

private RealMatrixExt computeStatisticalSpectrumDescriptor(RealMatrix sonogram, int bandLimit) {
    double[][] matrixSSD = new double[bandLimit][N_STATISTICAL_DESCRIPTORS];
    int nRows = sonogram.getRowDimension();

    if (nRows < bandLimit) { // requested number of bands is > than actual # of bands -> fill remainder with 0
        for (int r = nRows; r < bandLimit; r++) {
            Arrays.fill(matrixSSD[r], 0);
        }/*from w  ww  .j av a  2 s  . c o m*/
        bandLimit = nRows;
    }

    for (int r = 0; r < bandLimit; r++) {
        matrixSSD[r] = getStatisticalSpectrumDescriptor(sonogram.getRow(r));
    }

    RealMatrixExt result = new RealMatrixExt(matrixSSD);
    result.setType(RealMatrixExt.TYPE_SSD);
    return result;
}

From source file:com.datumbox.framework.core.machinelearning.featureselection.continuous.PCA.java

/** {@inheritDoc} */
@Override//from   w  ww  . ja va 2 s  .c om
protected void filterFeatures(Dataframe dataset) {
    ModelParameters modelParameters = kb().getModelParameters();

    //convert data into matrix
    Map<Object, Integer> featureIds = modelParameters.getFeatureIds();

    Map<Integer, Integer> recordIdsReference = new HashMap<>();
    MatrixDataframe matrixDataset = MatrixDataframe.parseDataset(dataset, recordIdsReference, featureIds);

    RealMatrix components = new BlockRealMatrix(modelParameters.getComponents());

    //multiplying the data with components
    final RealMatrix X = matrixDataset.getX().multiply(components);

    streamExecutor.forEach(StreamMethods.stream(dataset.entries(), isParallelized()), e -> {
        Integer rId = e.getKey();
        Record r = e.getValue();
        int rowId = recordIdsReference.get(rId);

        AssociativeArray xData = new AssociativeArray();
        int componentId = 0;
        for (double value : X.getRow(rowId)) {
            xData.put(componentId++, value);
        }

        Record newR = new Record(xData, r.getY(), r.getYPredicted(), r.getYPredictedProbabilities());

        //we call below the recalculateMeta()
        dataset._unsafe_set(rId, newR);
    });

    //recordIdsReference = null;
    //matrixDataset = null;

    dataset.recalculateMeta();
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java

private RotationResults GPFoblq(RealMatrix A, boolean normalize, int maxIter, double eps)
        throws ConvergenceException {
    int ncol = A.getColumnDimension();

    RealMatrix Tinner = null;//w ww.j  a  v  a  2  s .c om
    RealMatrix TinnerInv = null;
    RealMatrix Tmat = new IdentityMatrix(ncol);

    if (normalize) {
        //elementwise division by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value / W.getEntry(row, column);
            }
        });
    }

    RealMatrix TmatInv = new LUDecomposition(Tmat).getSolver().getInverse();
    RealMatrix L = A.multiply(TmatInv.transpose());

    //compute gradientAt and function value
    gpFunction.computeValues(L);
    RealMatrix VgQ = gpFunction.getGradient();
    RealMatrix VgQt = VgQ;
    double f = gpFunction.getValue();
    double ft = f;
    RealMatrix G = ((L.transpose().multiply(VgQ).multiply(TmatInv)).transpose()).scalarMultiply(-1.0);

    int iter = 0;
    double alpha = 1.0;
    double s = eps + 0.5;
    double s2 = Math.pow(s, 2);
    int innerMaxIter = 10;
    int innerCount = 0;

    IdentityMatrix I = new IdentityMatrix(G.getRowDimension());
    RealMatrix V1 = MatrixUtils.getVector(ncol, 1.0);

    while (iter < maxIter) {
        RealMatrix M = MatrixUtils.multiplyElements(Tmat, G);
        RealMatrix diagP = new DiagonalMatrix(V1.multiply(M).getRow(0));
        RealMatrix Gp = G.subtract(Tmat.multiply(diagP));
        s = Math.sqrt(Gp.transpose().multiply(Gp).getTrace());
        s2 = Math.pow(s, 2);

        if (s < eps) {
            break;
        }
        alpha = 2.0 * alpha;

        innerCount = 0;
        for (int i = 0; i < innerMaxIter; i++) {
            RealMatrix X = Tmat.subtract(Gp.scalarMultiply(alpha));
            RealMatrix X2 = MatrixUtils.multiplyElements(X, X);
            RealMatrix V = V1.multiply(X2);
            V.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
                @Override
                public double visit(int row, int column, double value) {
                    return 1.0 / Math.sqrt(value);
                }
            });

            //compute new value of T, its inverse, and the rotated loadings
            RealMatrix diagV = new DiagonalMatrix(V.getRow(0));
            Tinner = X.multiply(diagV);
            TinnerInv = new LUDecomposition(Tinner).getSolver().getInverse();
            L = A.multiply(TinnerInv.transpose());

            //compute new values of the gradientAt and the rotation criteria
            gpFunction.computeValues(L);
            VgQt = gpFunction.getGradient();
            ft = gpFunction.getValue();

            innerCount++;
            if (ft < f - 0.5 * s2 * alpha) {
                break;
            }
            alpha = alpha / 2.0;
        }

        //            System.out.println(iter + "  " + f + "  " + s + "  " + Math.log10(s) + "  " + alpha + "  " + innerCount);

        Tmat = Tinner;
        f = ft;
        G = (L.transpose().multiply(VgQt).multiply(TinnerInv)).transpose().scalarMultiply(-1.0);
        iter++;
    }

    boolean convergence = s < eps;
    if (!convergence) {
        throw new ConvergenceException();
    }

    if (normalize) {
        //elementwise multiplication by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value * W.getEntry(row, column);
            }
        });
    }

    RealMatrix Phi = Tmat.transpose().multiply(Tmat);
    RotationResults result = new RotationResults(gpFunction.getValue(), L, Phi, Tmat, rotationMethod);
    return result;

}