Package org.apache.mahout.math

Examples of org.apache.mahout.math.DenseMatrix


    this.numCategories = numCategories;
    this.prior = prior;

    updateSteps = new DenseVector(numFeatures);
    updateCounts = new DenseVector(numFeatures).assign(perTermAnnealingOffset);
    beta = new DenseMatrix(numCategories - 1, numFeatures);
  }
View Full Code Here


    values[1][0] = 0.0;
    values[2][0] = 0.0;
    values[1][1] = 0.0;
    values[2][2] = 0.0;

    Matrix dataset = new DenseMatrix(values);
    this.trainer.train(labelset, dataset);
    assertFalse(this.trainer.getModel().classify(dataset.getColumn(3)));
    assertTrue(this.trainer.getModel().classify(dataset.getColumn(0)));
  }
View Full Code Here

   * @return matrix of alpha factors.
   */
  public static Matrix forwardAlgorithm(HmmModel model, int[] observations,
                                        boolean scaled) {

    Matrix alpha = new DenseMatrix(observations.length, model.getNrOfHiddenStates());

    forwardAlgorithm(alpha, model, observations, scaled);

    return alpha;
  }
View Full Code Here

   */
  public static Matrix backwardAlgorithm(HmmModel model, int[] observations,
                                         boolean scaled) {

    // initialize the matrix
    Matrix beta = new DenseMatrix(observations.length, model.getNrOfHiddenStates());
    // compute the beta factors
    backwardAlgorithm(beta, model, observations, scaled);

    return beta;
  }
View Full Code Here

    List<String> data = CharStreams.readLines(isr);
    String first = data.get(0);
    data = data.subList(1, data.size());

    List<String> values = Lists.newArrayList(onCommas.split(first));
    Matrix r = new DenseMatrix(data.size(), values.size());

    int column = 0;
    Map<String, Integer> labels = Maps.newHashMap();
    for (String value : values) {
      labels.put(value, column);
      column++;
    }
    r.setColumnLabelBindings(labels);

    int row = 0;
    for (String line : data) {
      column = 0;
      values = Lists.newArrayList(onCommas.split(line));
      for (String value : values) {
        r.set(row, column, Double.parseDouble(value));
        column++;
      }
      row++;
    }
View Full Code Here

    assertEquals(m.transpose().times(m).times(x1).minus(m.transpose().times(b)).norm(2), r.getNormalEquationResidual(), 1.0e-9);
  }
 
  @Test
  public void random() {
    Matrix m = new DenseMatrix(200, 30).assign(Functions.random());

    Vector b = new DenseVector(200).assign(1);

    LSMR r = new LSMR();
    Vector x1 = r.solve(m, b);
View Full Code Here

    assertEquals(m.times(x1).minus(b).norm(2), r.getResidualNorm(), 1.0e-5);
    assertEquals(actual, r.getNormalEquationResidual(), 1.0e-9);
  }

  private static Matrix hilbert(int n) {
    Matrix r = new DenseMatrix(n, n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        r.set(i, j, 1.0 / (i + j + 1));
      }
    }
    return r;
  }
View Full Code Here

    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

   * Allocates a new data-structure for accumulating information about AUC and a few other accuracy
   * measures.
   * @param threshold The threshold to use in computing the confusion matrix.
   */
  public Auc(double threshold) {
    confusion = new DenseMatrix(2, 2);
    entropy = new DenseMatrix(2, 2);
    this.rand = RandomUtils.getRandom();
    this.threshold = threshold;
  }
View Full Code Here

      int[] hiddenSequence, double pseudoCount) {
    // make sure the pseudo count is not zero
    pseudoCount = pseudoCount == 0 ? Double.MIN_VALUE : pseudoCount;

    // initialize the parameters
    DenseMatrix transitionMatrix = new DenseMatrix(nrOfHiddenStates, nrOfHiddenStates);
    DenseMatrix emissionMatrix = new DenseMatrix(nrOfHiddenStates, nrOfOutputStates);
    // assign a small initial probability that is larger than zero, so
    // unseen states will not get a zero probability
    transitionMatrix.assign(pseudoCount);
    emissionMatrix.assign(pseudoCount);
    // given no prior knowledge, we have to assume that all initial hidden
    // states are equally likely
    DenseVector initialProbabilities = new DenseVector(nrOfHiddenStates);
    initialProbabilities.assign(1.0 / nrOfHiddenStates);

    // now loop over the sequences to count the number of transitions
    countTransitions(transitionMatrix, emissionMatrix, observedSequence,
        hiddenSequence);

    // make sure that probabilities are normalized
    for (int i = 0; i < nrOfHiddenStates; i++) {
      // compute sum of probabilities for current row of transition matrix
      double sum = 0;
      for (int j = 0; j < nrOfHiddenStates; j++) {
        sum += transitionMatrix.getQuick(i, j);
      }
      // normalize current row of transition matrix
      for (int j = 0; j < nrOfHiddenStates; j++) {
        transitionMatrix.setQuick(i, j, transitionMatrix.getQuick(i, j) / sum);
      }
      // compute sum of probabilities for current row of emission matrix
      sum = 0;
      for (int j = 0; j < nrOfOutputStates; j++) {
        sum += emissionMatrix.getQuick(i, j);
      }
      // normalize current row of emission matrix
      for (int j = 0; j < nrOfOutputStates; j++) {
        emissionMatrix.setQuick(i, j, emissionMatrix.getQuick(i, j) / sum);
      }
    }

    // return a new model using the parameter estimations
    return new HmmModel(transitionMatrix, emissionMatrix, initialProbabilities);
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.