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

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

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

Matrix.numCols介绍

暂无

代码示例

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

protected TransposeViewVector(Matrix m, int offset, boolean rowToColumn) {
 super(rowToColumn ? m.numRows() : m.numCols());
 matrix = m;
 this.transposeOffset = offset;
 this.rowToColumn = rowToColumn;
 numCols = rowToColumn ? m.numCols() : m.numRows();
}

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

private static int viewSize(Matrix matrix, int row, int column, int rowStride, int columnStride) {
 if (rowStride != 0 && columnStride != 0) {
  int n1 = (matrix.numRows() - row) / rowStride;
  int n2 = (matrix.numCols() - column) / columnStride;
  return Math.min(n1, n2);
 } else if (rowStride > 0) {
  return (matrix.numRows() - row) / rowStride;
 } else {
  return (matrix.numCols() - column) / columnStride;
 }
}

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

public TransposedMatrixView(Matrix m) {
 super(m.numCols(), m.numRows());
 this.m = m;
}

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

public JacobiConditioner(Matrix a) {
 if (a.numCols() != a.numRows()) {
  throw new IllegalArgumentException("Matrix must be square.");
 }
 
 inverseDiagonal = new DenseVector(a.numCols());
 for (int i = 0; i < a.numCols(); ++i) {
  inverseDiagonal.setQuick(i, 1.0 / a.getQuick(i, i));
 }
}

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

static Matrix addLambdaTimesNuiTimesE(Matrix matrix, double lambda, int nui) {
 Preconditions.checkArgument(matrix.numCols() == matrix.numRows(), "Must be a Square Matrix");
 double lambdaTimesNui = lambda * nui;
 int numCols = matrix.numCols();
 for (int n = 0; n < numCols; n++) {
  matrix.setQuick(n, n, matrix.getQuick(n, n) + lambdaTimesNui);
 }
 return matrix;
}

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

private static void printMatrix(String name, Matrix m) {
 int rows = m.numRows();
 int columns = m.numCols();
 System.out.printf("%s - %d x %d\n", name, rows, columns);
 for (int i = 0; i < rows; i++) {
  for (int j = 0; j < columns; j++) {
   System.out.printf("%10.5f", m.get(i, j));
  }
  System.out.printf("\n");
 }
 System.out.printf("\n");
 System.out.printf("\n");
}

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

private static void printMatrix(String name, Matrix m) {
 int rows = m.numRows();
 int columns = m.numCols();
 System.out.printf("%s - %d x %d\n", name, rows, columns);
 for (int i = 0; i < rows; i++) {
  for (int j = 0; j < columns; j++) {
   System.out.printf("%10.5f", m.get(i, j));
  }
  System.out.printf("\n");
 }
 System.out.printf("\n");
 System.out.printf("\n");
}

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

public Vector currentTrainingProjection() {
 if (trainingProjections.viewRow(trainingIndex) == null) {
  trainingProjections.assignRow(trainingIndex, new DenseVector(currentEigens.numCols()));
 }
 return trainingProjections.viewRow(trainingIndex);
}

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

@Override
public Matrix timesLeft(Matrix that) {
 if (that.numCols() != diagonal.size()) {
  throw new IllegalArgumentException(
   "Incompatible number of rows in the left operand of matrix-matrix multiplication.");
 }
 Matrix m = that.like();
 for (int col = 0; col < diagonal.size(); col++) {
  m.assignColumn(col, that.viewColumn(col).times(diagonal.getQuick(col)));
 }
 return m;
}

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

/** test dimensions */
@Test
public void testDimensions() {
 Matrix matrix = new DenseMatrix(testSquare);
 int m = matrix.numRows();
 int n = matrix.numCols();
 SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
 assertEquals(m, svd.getU().numRows());
 assertEquals(m, svd.getU().numCols());
 assertEquals(m, svd.getS().numCols());
 assertEquals(n, svd.getS().numCols());
 assertEquals(n, svd.getV().numRows());
 assertEquals(n, svd.getV().numCols());
 
}

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

private static void compareMatrices(Matrix m, Matrix m2) {
 assertEquals(m.numRows(), m2.numRows());
 assertEquals(m.numCols(), m2.numCols());
 for (int r = 0; r < m.numRows(); r++) {
  for (int c = 0; c < m.numCols(); c++) {
   assertEquals(m.get(r, c), m2.get(r, c), EPSILON);
  }
 }
 Map<String,Integer> bindings = m.getRowLabelBindings();
 Map<String, Integer> bindings2 = m2.getRowLabelBindings();
 assertEquals(bindings == null, bindings2 == null);
 if (bindings != null) {
  assertEquals(bindings.size(), m.numRows());
  assertEquals(bindings.size(), bindings2.size());
  for (Map.Entry<String,Integer> entry : bindings.entrySet()) {
   assertEquals(entry.getValue(), bindings2.get(entry.getKey()));
  }
 }
 bindings = m.getColumnLabelBindings();
 bindings2 = m2.getColumnLabelBindings();
 assertEquals(bindings == null, bindings2 == null);
 if (bindings != null) {
  assertEquals(bindings.size(), bindings2.size());
  for (Map.Entry<String,Integer> entry : bindings.entrySet()) {
   assertEquals(entry.getValue(), bindings2.get(entry.getKey()));
  }
 }
}

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

@Test
public void testAggregateCols() {
 Vector v = test.aggregateColumns(new VectorFunction() {
  @Override
  public double apply(Vector v) {
   return v.zSum();
  }
 });
 for (int i = 0; i < test.numCols(); i++) {
  assertEquals(test.viewColumn(i).zSum(), v.get(i), EPSILON);
 }
}

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

int cols = B.numCols();
Matrix x = mType.like(columns, cols);

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

Matrix sp = new DenseMatrix(s.numRows()+1,s.numCols());
Matrix up = new DenseMatrix(u.numRows(),u.numCols()+1);
 for (int j = 0; j < u.numCols(); j++) {
  up.set(i, j, u.get(i, j));
 for (int j = 0; j < s.numCols(); j++) {
  sp.set(i, j, s.get(i, j));

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

/**
 * Test for the error reported in https://issues.apache.org/jira/browse/MAHOUT-1146
 */
@Test
public void testColumnView() {
 Matrix matrix = new DenseMatrix(5, 3);
 Vector column2 = matrix.viewColumn(2);
 Matrix outerProduct = column2.cross(column2);
 assertEquals(matrix.numRows(), outerProduct.numRows());
 assertEquals(matrix.numRows(), outerProduct.numCols());
}

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

@Test
public void testUniformView() {
 Matrix m1 = Matrices.uniformView(5, 6, 1234);
 Matrix m2 = Matrices.uniformView(5, 6, 1234);
 for (int row = 0; row < m1.numRows(); row++) {
  for (int col = 0; col < m1.numCols(); col++) {
   assertTrue(m1.getQuick(row, col) >= 0.0);
   assertTrue(m1.getQuick(row, col) < 1.0);
  }
 }
 Matrix diff = m1.minus(m2);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

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

@Test
public void testSymmetricUniformView() {
 Matrix m1 = Matrices.symmetricUniformView(5, 6, 1234);
 Matrix m2 = Matrices.symmetricUniformView(5, 6, 1234);
 for (int row = 0; row < m1.numRows(); row++) {
  for (int col = 0; col < m1.numCols(); col++) {
   assertTrue(m1.getQuick(row, col) >= -1.0);
   assertTrue(m1.getQuick(row, col) < 1.0);
  }
 }
 Matrix diff = m1.minus(m2);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

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

@Test
public void testEigen() {
 Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
 Matrix b = new DenseMatrix(a.numRows(), a.numCols());
 b.assign(a);
 assertEquals(0, a.minus(b).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 EigenDecomposition edA = new EigenDecomposition(a);
 EigenDecomposition edB = new EigenDecomposition(b);
 System.out.println(edA.getV());
 assertEquals(0, edA.getV().minus(edB.getV()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 assertEquals(0, edA.getRealEigenvalues().minus(edA.getRealEigenvalues()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

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

@Test
public void createRiIiMaybeTransposed() {
 Vector ratings = new SequentialAccessSparseVector(3);
 ratings.setQuick(1, 1.0);
 ratings.setQuick(3, 3.0);
 ratings.setQuick(5, 5.0);
 Matrix riIiMaybeTransposed = AlternatingLeastSquaresSolver.createRiIiMaybeTransposed(ratings);
 assertEquals(1, riIiMaybeTransposed.numCols(), 1);
 assertEquals(3, riIiMaybeTransposed.numRows(), 3);
 assertEquals(1.0, riIiMaybeTransposed.getQuick(0, 0), EPSILON);
 assertEquals(3.0, riIiMaybeTransposed.getQuick(1, 0), EPSILON);
 assertEquals(5.0, riIiMaybeTransposed.getQuick(2, 0), EPSILON);
}

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

@Test
public void testTimesMatrix() {
 Matrix transpose = test.transpose();
 Matrix value = test.times(transpose);
 assertEquals("rows", test.rowSize(), value.rowSize());
 assertEquals("cols", test.rowSize(), value.columnSize());
 Matrix expected = new DenseMatrix(new double[][]{{5.0, 11.0, 17.0},
   {11.0, 25.0, 39.0}, {17.0, 39.0, 61.0}}).times(1.21);
 for (int i = 0; i < expected.numCols(); i++) {
  for (int j = 0; j < expected.numRows(); j++) {
   assertTrue("Matrix times transpose not correct: " + i + ", " + j
     + "\nexpected:\n\t" + expected + "\nactual:\n\t"
     + value,
     Math.abs(expected.get(i, j) - value.get(i, j)) < 1.0e-12);
  }
 }
 Matrix timestest = new DenseMatrix(10, 1);
 /* will throw ArrayIndexOutOfBoundsException exception without MAHOUT-26 */
 timestest.transpose().times(timestest);
}

相关文章