List of usage examples for org.apache.commons.math3.linear RealMatrix getRow
double[] getRow(int row) throws OutOfRangeException;
From source file:net.myrrix.online.factorizer.als.AlternatingLeastSquaresTest.java
private static RealMatrix buildTestXYTProduct(boolean reconstructR) throws ExecutionException, InterruptedException { System.setProperty("model.reconstructRMatrix", Boolean.toString(reconstructR)); FastByIDMap<FastByIDFloatMap> byRow = new FastByIDMap<FastByIDFloatMap>(); FastByIDMap<FastByIDFloatMap> byCol = new FastByIDMap<FastByIDFloatMap>(); // Octave: R = [ 0 2 3 1 0 ; 0 0 4 5 0 ; 1 0 0 0 2 ; 3 0 1 0 5 ; 0 2 2 2 0 ] MatrixUtils.addTo(0, 1, 2.0f, byRow, byCol); MatrixUtils.addTo(0, 2, 3.0f, byRow, byCol); MatrixUtils.addTo(0, 3, 1.0f, byRow, byCol); MatrixUtils.addTo(1, 2, 4.0f, byRow, byCol); MatrixUtils.addTo(1, 3, 5.0f, byRow, byCol); MatrixUtils.addTo(2, 0, 1.0f, byRow, byCol); MatrixUtils.addTo(2, 4, 2.0f, byRow, byCol); MatrixUtils.addTo(3, 0, 3.0f, byRow, byCol); MatrixUtils.addTo(3, 2, 1.0f, byRow, byCol); MatrixUtils.addTo(3, 4, 5.0f, byRow, byCol); MatrixUtils.addTo(4, 1, 2.0f, byRow, byCol); MatrixUtils.addTo(4, 2, 2.0f, byRow, byCol); MatrixUtils.addTo(4, 3, 2.0f, byRow, byCol); // Octave: Y = [ 0.1 0.2 ; 0.2 0.5 ; 0.3 0.1 ; 0.2 0.2 ; 0.5 0.4 ]; 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 }); previousY.put(4L, new float[] { 0.5f, 0.4f }); MatrixFactorizer als = new AlternatingLeastSquares(byRow, byCol, 2, 0.0001, 40); als.setPreviousY(previousY);/*from w w w . j ava 2s . c om*/ 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); return product; }
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the standard deviations of rows. * * @param matrix The matrix of which the standard deviations of rows to be computed * @return A double array of row standard deviations. *///from ww w .j a va 2 s.com public static double[] rowStdDevs(RealMatrix matrix) { double[] retval = new double[matrix.getRowDimension()]; for (int i = 0; i < retval.length; i++) { retval[i] = new DescriptiveStatistics(matrix.getRow(i)).getStandardDeviation(); } return retval; }
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 ww w .j av a2 s . c o m*/ } return res; }
From source file:net.myrrix.common.math.MatrixUtilsTest.java
@Test public void testTransposeTimesSelf() { FastByIDMap<float[]> M = new FastByIDMap<float[]>(); M.put(1L, new float[] { 4.0f, -1.0f, -5.0f }); M.put(2L, new float[] { 2.0f, 0.0f, 3.0f }); RealMatrix MTM = MatrixUtils.transposeTimesSelf(M); assertArrayEquals(new double[] { 20.0, -4.0, -14.0 }, MTM.getRow(0)); assertArrayEquals(new double[] { -4.0, 1.0, 5.0 }, MTM.getRow(1)); assertArrayEquals(new double[] { -14.0, 5.0, 34.0 }, MTM.getRow(2)); }
From source file:com.clust4j.metrics.pairwise.HaversineTest.java
@Test public void test2() { final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(coordinates, false); StandardScaler scaler = new StandardScaler().fit(mat); RealMatrix X = scaler.transform(mat); AbstractCentroidClusterer km;/*from www . j a va 2 s .c om*/ CentroidClustererParameters<? extends AbstractCentroidClusterer> planner; planner = new KMedoidsParameters(2).setVerbose(true).setMetric(Distance.HAVERSINE.MI).setVerbose(true); km = planner.fitNewModel(X); int[] kmlabels = km.getLabels(); assertTrue(kmlabels[0] == kmlabels[1] && kmlabels[1] == kmlabels[2]); assertTrue(kmlabels[1] != kmlabels[3] && kmlabels[3] == kmlabels[4]); assertTrue(kmlabels[0] == 0 && kmlabels[1] == 0 && kmlabels[2] == 0); // Inverse transform the centroid: double[][] c = new double[][] { km.getCentroids().get(0) }; Array2DRowRealMatrix cm = new Array2DRowRealMatrix(c, false); RealMatrix inverse = scaler.inverseTransform(cm); assertTrue(VecUtils.equalsExactly(inverse.getRow(0), coordinates[0])); // First one should be Austin }
From source file:com.yahoo.egads.utilities.TestSpectralMethods.java
@Test public void f() { double[][] mat = { { 1, 2 }, { 3, 2 }, { 2, 5 }, { 7, 8 }, { 3, 4 }, { 8, 9 }, { 3, 3 } }; RealMatrix data = MatrixUtils.createRealMatrix(mat); RealMatrix res = SpectralMethods.createHankelMatrix(data, 3); for (int i = 0; i < res.getRowDimension(); ++i) { System.out.println(Arrays.toString(res.getRow(i))); }/* w w w. j ava 2 s . com*/ RealMatrix data2 = SpectralMethods.averageHankelMatrix(res, 3); for (int i = 0; i < data2.getRowDimension(); ++i) { System.out.println(Arrays.toString(data2.getRow(i))); } }
From source file:com.cloudera.oryx.common.math.MatrixUtilsTest.java
@Test public void testTransposeTimesSelf() { LongObjectMap<float[]> M = new LongObjectMap<float[]>(); M.put(1L, new float[] { 4.0f, -1.0f, -5.0f }); M.put(2L, new float[] { 2.0f, 0.0f, 3.0f }); RealMatrix MTM = MatrixUtils.transposeTimesSelf(M); assertArrayEquals(new double[] { 20.0, -4.0, -14.0 }, MTM.getRow(0)); assertArrayEquals(new double[] { -4.0, 1.0, 5.0 }, MTM.getRow(1)); assertArrayEquals(new double[] { -14.0, 5.0, 34.0 }, MTM.getRow(2)); }
From source file:com.cloudera.oryx.als.common.factorizer.als.AlternatingLeastSquaresTest.java
@Test public void testALS() throws Exception { RealMatrix product = buildTestXYTProduct(); 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 w w . j av a 2s . c om*/ 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:com.cloudera.oryx.als.common.factorizer.als.AlternatingLeastSquaresPredictingRTest.java
@Test public void testALSPredictingR() throws Exception { ConfigUtils.overlayConfigOnDefault(getResourceAsFile("AlternatingLeastSquaresPredictingRTest.conf")); RealMatrix product = AlternatingLeastSquaresTest.buildTestXYTProduct(); 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));//w w w . ja v a 2 s . c o m 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:com.cloudera.oryx.als.common.factorizer.als.NegativeInputTest.java
@Test public void testALS() throws Exception { LongObjectMap<LongFloatMap> byRow = new LongObjectMap<LongFloatMap>(); LongObjectMap<LongFloatMap> byCol = new LongObjectMap<LongFloatMap>(); // 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 ]; LongObjectMap<float[]> previousY = new LongObjectMap<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);/* w ww. ja va 2 s . c om*/ als.call(); RealMatrix product = MatrixUtilsTest.multiplyXYT(als.getX(), als.getY()); 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)); }