Package org.apache.mahout.math

Examples of org.apache.mahout.math.DenseMatrix


    return this;
  }
 
  public Matrix getMatrix() {
    int length = confusionMatrix.length;
    Matrix m = new DenseMatrix(length, length);
    for (int r = 0; r < length; r++) {
      for (int c = 0; c < length; c++) {
        m.set(r, c, confusionMatrix[r][c]);
      }
    }
    Map<String,Integer> labels = Maps.newHashMap();
    for(Map.Entry<String, Integer> entry : labelMap.entrySet()) {
      labels.put(entry.getKey(), entry.getValue());
    }
    m.setRowLabelBindings(labels);
    m.setColumnLabelBindings(labels);
    return m;
  }
View Full Code Here


  @Test
  public void testHebbianSolver() {
    int numColumns = 800;
    Matrix corpus = randomSequentialAccessSparseMatrix(1000, 900, numColumns, 30, 1.0);
    int rank = 50;
    Matrix eigens = new DenseMatrix(rank, numColumns);
    TrainingState state = new TrainingState(eigens, null);
    long optimizedTime = timeSolver(corpus,
                                    0.00001,
                                    5,
                                    rank,
View Full Code Here

  @Test
  public void testLanczosSolver() throws Exception {
    int numColumns = 800;
    Matrix corpus = randomSequentialAccessSparseMatrix(1000, 900, numColumns, 30, 1.0);
    int rank = 50;
    Matrix eigens = new DenseMatrix(rank, numColumns);
    long time = timeLanczos(corpus, eigens, rank, false);
    assertTrue("Lanczos taking too long!  Are you in the debugger? :)", time < 10000);
    assertOrthonormal(eigens);
    assertEigen(eigens, corpus, 0.1, false);
  }
View Full Code Here

  public void testLanczosSolverSymmetric() throws Exception {
    int numColumns = 400;
    Matrix corpus = randomSequentialAccessSparseMatrix(500, 450, numColumns, 10, 1.0);
    Matrix gramMatrix = corpus.times(corpus.transpose());
    int rank = 30;
    Matrix eigens = new DenseMatrix(rank, gramMatrix.numCols());
    long time = timeLanczos(gramMatrix, eigens, rank, true);
    assertTrue("Lanczos taking too long!  Are you in the debugger? :)", time < 10000);
    assertOrthonormal(eigens);
    assertEigen(eigens, gramMatrix, 0.1, true);
  }
View Full Code Here

   *         singular values) have been found.
   */
  public TrainingState solve(Matrix corpus,
                             int desiredRank) {
    int cols = corpus.numCols();
    Matrix eigens = new DenseMatrix(desiredRank, cols);
    List<Double> eigenValues = new ArrayList<Double>();
    log.info("Finding " + desiredRank + " singular vectors of matrix with " + corpus.numRows() + " rows, via Hebbian");
    /**
     * The corpusProjections matrix is a running cache of the residual projection of each corpus vector against all
     * of the previously found singular vectors.  Without this, if multiple passes over the data is made (per
     * singular vector), recalculating these projections eventually dominates the computational complexity of the
     * solver.
     */
    Matrix corpusProjections = new DenseMatrix(corpus.numRows(), desiredRank);
    TrainingState state = new TrainingState(eigens, corpusProjections);
    for (int i = 0; i < desiredRank; i++) {
      Vector currentEigen = new DenseVector(cols);
      Vector previousEigen = null;
      while (hasNotConverged(currentEigen, corpus, state)) {
View Full Code Here

  private Matrix illegal22;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    legal22 = new DenseMatrix(new double[][]{{0.5, 0.5}, {0.3, 0.7}});
    legal23 = new DenseMatrix(new double[][]{{0.2, 0.2, 0.6},
        {0.3, 0.3, 0.4}});
    legal33 = new DenseMatrix(new double[][]{{0.1, 0.1, 0.8},
        {0.1, 0.2, 0.7}, {0.2, 0.3, 0.5}});
    legal2 = new DenseVector(new double[]{0.4, 0.6});
    illegal22 = new DenseMatrix(new double[][]{{1, 2}, {3, 4}});
  }
View Full Code Here

  }

  @Test
  public void testNormalizeModel() {
    DenseVector ip = new DenseVector(new double[]{10, 20});
    DenseMatrix tr = new DenseMatrix(new double[][]{{10, 10}, {20, 25}});
    DenseMatrix em = new DenseMatrix(new double[][]{{5, 7}, {10, 15}});
    HmmModel model = new HmmModel(tr, em, ip);
    HmmUtils.normalizeModel(model);
    // the model should be valid now
    HmmUtils.validate(model);
  }
View Full Code Here

  }

  @Test
  public void testTruncateModel() {
    DenseVector ip = new DenseVector(new double[]{0.0001, 0.0001, 0.9998});
    DenseMatrix tr = new DenseMatrix(new double[][]{
        {0.9998, 0.0001, 0.0001}, {0.0001, 0.9998, 0.0001},
        {0.0001, 0.0001, 0.9998}});
    DenseMatrix em = new DenseMatrix(new double[][]{
        {0.9998, 0.0001, 0.0001}, {0.0001, 0.9998, 0.0001},
        {0.0001, 0.0001, 0.9998}});
    HmmModel model = new HmmModel(tr, em, ip);
    // now truncate the model
    HmmModel sparseModel = HmmUtils.truncateModel(model, 0.01);
View Full Code Here

    double[][] emissionP = {{0.8, 0.1, 0.1}, {0.6, 0.1, 0.3},
        {0.1, 0.8, 0.1}, {0.0, 0.1, 0.9}};
    // initialize the initial probability vector
    double[] initialP = {0.2, 0.1, 0.4, 0.3};
    // now generate the model
    model = new HmmModel(new DenseMatrix(transitionP), new DenseMatrix(
        emissionP), new DenseVector(initialP));
    model.registerHiddenStateNames(hiddenNames);
    model.registerOutputStateNames(outputNames);
    // make sure the model is valid :)
    HmmUtils.validate(model);
View Full Code Here

    }

    @Override
    public Matrix deserialize(JsonElement x, Type type, JsonDeserializationContext jsonDeserializationContext) {
      JsonObject data = x.getAsJsonObject();
      Matrix r = new DenseMatrix(data.get("rows").getAsInt(), data.get("cols").getAsInt());
      int i = 0;
      for (JsonElement row : data.get("data").getAsJsonArray()) {
        int j = 0;
        for (JsonElement element : row.getAsJsonArray()) {
          r.set(i, j, element.getAsDouble());
          j++;
        }
        i++;
      }
      return r;
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.DenseMatrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.