org.apache.mahout.math.Matrix.viewPart()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中org.apache.mahout.math.Matrix.viewPart()方法的一些代码示例,展示了Matrix.viewPart()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matrix.viewPart()方法的具体详情如下:
包路径:org.apache.mahout.math.Matrix
类名称:Matrix
方法名:viewPart

Matrix.viewPart介绍

[英]Return a view into part of a matrix. Changes to the view will change the original matrix.
[中]将视图返回到矩阵的一部分。对视图的更改将更改原始矩阵。

代码示例

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartIndexOver() {
  3. int[] offset = {1, 1};
  4. int[] size = {2, 2};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartCardinality() {
  3. int[] offset = {1, 1};
  4. int[] size = {3, 3};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartIndexUnder() {
  3. int[] offset = {-1, -1};
  4. int[] size = {2, 2};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartIndexUnder() {
  3. int[] offset = {-1, -1};
  4. int[] size = {2, 2};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartCardinality() {
  3. int[] offset = {1, 1};
  4. int[] size = {3, 3};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. @Test(expected = IndexException.class)
  2. public void testViewPartIndexOver() {
  3. int[] offset = {1, 1};
  4. int[] size = {2, 2};
  5. test.viewPart(offset, size);
  6. }

代码示例来源:origin: apache/mahout

  1. /** Tests MAHOUT-1046 */
  2. @Test
  3. public void testMatrixViewBug() {
  4. Matrix m = test.viewPart(0, 3, 0, 2);
  5. // old bug would blow cookies with an index exception here.
  6. m = m.viewPart(2, 1, 0, 1);
  7. assertEquals(5.5, m.zSum(), 0);
  8. }

代码示例来源:origin: apache/mahout

  1. q = qTmp.viewPart(0, rows, 0, min).clone();
  2. } else {
  3. q = qTmp;

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testRightVectors() {
  3. Matrix A = lowRankMatrix();
  4. SequentialBigSvd s = new SequentialBigSvd(A, 6);
  5. SingularValueDecomposition svd = new SingularValueDecomposition(A);
  6. Matrix v1 = svd.getV().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  7. Matrix v2 = s.getV().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  8. assertEquals(v1, v2);
  9. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testViewPart() {
  3. int[] offset = {1, 1};
  4. int[] size = {2, 1};
  5. Matrix view = test.viewPart(offset, size);
  6. for (int row = 0; row < view.rowSize(); row++) {
  7. for (int col = 0; col < view.columnSize(); col++) {
  8. assertEquals("value[" + row + "][" + col + ']',
  9. values[row + 2][col + 2], view.getQuick(row, col), EPSILON);
  10. }
  11. }
  12. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testLeftVectors() {
  3. Matrix A = lowRankMatrix();
  4. SequentialBigSvd s = new SequentialBigSvd(A, 8);
  5. SingularValueDecomposition svd = new SingularValueDecomposition(A);
  6. // can only check first few singular vectors because once the singular values
  7. // go to zero, the singular vectors are not uniquely determined
  8. Matrix u1 = svd.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS);
  9. Matrix u2 = s.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS);
  10. assertEquals(0, u1.minus(u2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9);
  11. }

代码示例来源:origin: apache/mahout

  1. /**
  2. * Predict the power law growth in number of unique samples from the first few data points.
  3. * Also check that the fitted growth coefficient is about right.
  4. *
  5. * @param m
  6. * @param currentIndex Total data points seen so far. Unique values should be log(currentIndex)*expectedCoefficient + offset.
  7. * @param expectedCoefficient What slope do we expect.
  8. * @return The predicted value for log(currentIndex)
  9. */
  10. private static double predictSize(Matrix m, int currentIndex, double expectedCoefficient) {
  11. int rows = m.rowSize();
  12. Matrix a = m.viewPart(0, rows, 1, 2);
  13. Matrix b = m.viewPart(0, rows, 0, 1);
  14. Matrix ata = a.transpose().times(a);
  15. Matrix atb = a.transpose().times(b);
  16. QRDecomposition s = new QRDecomposition(ata);
  17. Matrix r = s.solve(atb).transpose();
  18. assertEquals(expectedCoefficient, r.get(0, 0), 0.2);
  19. return r.times(new DenseVector(new double[]{Math.log(currentIndex), 1})).get(0);
  20. }

代码示例来源:origin: apache/mahout

  1. double predict5 = predictSize(m5.viewPart(0, k, 0, 3), i, 0.5);
  2. assertEquals(predict5, Math.log(s5.size()), 1);
  3. double predict9 = predictSize(m9.viewPart(0, k, 0, 3), i, 0.9);
  4. assertEquals(predict9, Math.log(s9.size()), 1);

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testViewPart() {
  3. int[] offset = {1, 1};
  4. int[] size = {2, 1};
  5. Matrix view = test.viewPart(offset, size);
  6. assertEquals(2, view.rowSize());
  7. assertEquals(1, view.columnSize());
  8. for (int row = 0; row < view.rowSize(); row++) {
  9. for (int col = 0; col < view.columnSize(); col++) {
  10. assertEquals("value[" + row + "][" + col + ']',
  11. values[row + 1][col + 1], view.get(row, col), EPSILON);
  12. }
  13. }
  14. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testBasics() {
  3. Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
  4. System.out.println(a.toString());
  5. assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10);
  6. assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus(
  7. new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10);
  8. assertEquals(4, a.get(0, 3), 1.0e-10);
  9. System.out.println(a);
  10. Matrix m = new DenseMatrix(4, 4).assign(a);
  11. assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
  12. System.out.println(m);
  13. assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate(
  14. Functions.PLUS, Functions.ABS), 1.0e-10);
  15. System.out.println(a.plus(a));
  16. assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
  17. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void rank1() {
  3. Matrix x = new DenseMatrix(3, 3);
  4. x.viewRow(0).assign(new double[]{1, 2, 3});
  5. x.viewRow(1).assign(new double[]{2, 4, 6});
  6. x.viewRow(2).assign(new double[]{3, 6, 9});
  7. CholeskyDecomposition rr = new CholeskyDecomposition(x.transpose().times(x), false);
  8. assertEquals(0, new DenseVector(new double[]{3.741657, 7.483315, 11.22497}).aggregate(rr.getL().transpose().viewRow(0), Functions.PLUS, new DoubleDoubleFunction() {
  9. @Override
  10. public double apply(double arg1, double arg2) {
  11. return Math.abs(arg1) - Math.abs(arg2);
  12. }
  13. }), 1.0e-5);
  14. assertEquals(0, rr.getL().viewPart(0, 3, 1, 2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9);
  15. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testProjection() {
  3. Vector v1 = new DenseVector(10).assign(Functions.random());
  4. WeightedVector v2 = new WeightedVector(v1, v1, 31);
  5. assertEquals(v1.dot(v1), v2.getWeight(), 1.0e-13);
  6. assertEquals(31, v2.getIndex());
  7. Matrix y = new DenseMatrix(10, 4).assign(Functions.random());
  8. Matrix q = new QRDecomposition(y.viewPart(0, 10, 0, 3)).getQ();
  9. Vector nullSpace = y.viewColumn(3).minus(q.times(q.transpose().times(y.viewColumn(3))));
  10. WeightedVector v3 = new WeightedVector(q.viewColumn(0).plus(q.viewColumn(1)), nullSpace, 1);
  11. assertEquals(0, v3.getWeight(), 1.0e-13);
  12. Vector qx = q.viewColumn(0).plus(q.viewColumn(1)).normalize();
  13. WeightedVector v4 = new WeightedVector(qx, q.viewColumn(0), 2);
  14. assertEquals(Math.sqrt(0.5), v4.getWeight(), 1.0e-13);
  15. WeightedVector v5 = WeightedVector.project(q.viewColumn(0), qx);
  16. assertEquals(Math.sqrt(0.5), v5.getWeight(), 1.0e-13);
  17. }

代码示例来源:origin: apache/mahout

  1. @Test
  2. public void testBasics() {
  3. Matrix a = new UpperTriangular(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
  4. assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10);
  5. assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus(
  6. new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10);
  7. assertEquals(4, a.get(0, 3), 1.0e-10);
  8. print(a);
  9. Matrix m = new DenseMatrix(4, 4).assign(a);
  10. assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
  11. print(m);
  12. assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate(
  13. Functions.PLUS, Functions.ABS), 1.0e-10);
  14. assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
  15. }

代码示例来源:origin: org.apache.mahout/mahout-mrlegacy

  1. @Test
  2. public void testLeftVectors() throws IOException {
  3. Matrix A = lowRankMatrixInMemory(20, 20);
  4. SequentialBigSvd s = new SequentialBigSvd(A, 6);
  5. SingularValueDecomposition svd = new SingularValueDecomposition(A);
  6. // can only check first few singular vectors
  7. Matrix u1 = svd.getU().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  8. Matrix u2 = s.getU().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  9. assertEquals(u1, u2);
  10. }

代码示例来源:origin: org.apache.mahout/mahout-mrlegacy

  1. @Test
  2. public void testRightVectors() throws IOException {
  3. Matrix A = lowRankMatrixInMemory(20, 20);
  4. SequentialBigSvd s = new SequentialBigSvd(A, 6);
  5. SingularValueDecomposition svd = new SingularValueDecomposition(A);
  6. Matrix v1 = svd.getV().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  7. Matrix v2 = s.getV().viewPart(0, 20, 0, 3).assign(Functions.ABS);
  8. assertEquals(v1, v2);
  9. }

相关文章