Package org.apache.mahout.matrix

Examples of org.apache.mahout.matrix.DenseVector$Element


   * @param v a Vector of rectangle centers
   * @param dv a Vector of rectangle sizes
   */
  public static void plotRectangle(Graphics2D g2, Vector v, Vector dv) {
    double[] flip = { 1, -1 };
    Vector v2 = v.clone().assign(new DenseVector(flip), new TimesFunction());
    v2 = v2.minus(dv.divide(2));
    int h = size / 2;
    double x = v2.get(0) + h;
    double y = v2.get(1) + h;
    g2.draw(new Rectangle2D.Double(x * ds, y * ds, dv.get(0) * ds, dv.get(1)
View Full Code Here


   * @param v a Vector of rectangle centers
   * @param dv a Vector of rectangle sizes
   */
  public static void plotEllipse(Graphics2D g2, Vector v, Vector dv) {
    double[] flip = { 1, -1 };
    Vector v2 = v.clone().assign(new DenseVector(flip), new TimesFunction());
    v2 = v2.minus(dv.divide(2));
    int h = size / 2;
    double x = v2.get(0) + h;
    double y = v2.get(1) + h;
    g2
View Full Code Here

   * @param my double y-value of the sample mean
   * @param sd double standard deviation of the samples
   */
  private static void generateSamples(int num, double mx, double my, double sd) {
    double[] params = { mx, my, sd, sd };
    sampleParams.add(new DenseVector(params));
    System.out.println("Generating " + num + " samples m=[" + mx + ", " + my
        + "] sd=" + sd);
    for (int i = 0; i < num; i++)
      sampleData.add(new DenseVector(new double[] {
          UncommonDistributions.rNorm(mx, sd),
          UncommonDistributions.rNorm(my, sd) }));
  }
View Full Code Here

   * @param sdy double y-value standard deviation of the samples
   */
  private static void generate2dSamples(int num, double mx, double my,
      double sdx, double sdy) {
    double[] params = { mx, my, sdx, sdy };
    sampleParams.add(new DenseVector(params));
    System.out.println("Generating " + num + " samples m=[" + mx + ", " + my
        + "] sd=[" + sdx + ", " + sdy + ']');
    for (int i = 0; i < num; i++)
      sampleData.add(new DenseVector(new double[] {
          UncommonDistributions.rNorm(mx, sdx),
          UncommonDistributions.rNorm(my, sdy) }));
  }
View Full Code Here

  public InferredDocument infer(Vector wordCounts) {
    double docTotal = wordCounts.zSum();
    int docLength = wordCounts.size();

    // initialize variational approximation to p(z|doc)
    Vector gamma = new DenseVector(state.numTopics);
    gamma.assign(state.topicSmoothing + docTotal / state.numTopics);
    Vector nextGamma = new DenseVector(state.numTopics);

    DenseMatrix phi = new DenseMatrix(state.numTopics, docLength);

    // digamma is expensive, precompute
    Vector digammaGamma = digamma(gamma);
    // and log normalize:
    double digammaSumGamma = digamma(gamma.zSum());
    digammaGamma = digammaGamma.plus(-digammaSumGamma);

    Map<Integer, Integer> columnMap = new HashMap<Integer, Integer>();

    int iteration = 0;
    final int MAX_ITER = 20;

    boolean converged = false;
    double oldLL = 1;
    while (!converged && iteration < MAX_ITER) {
      nextGamma.assign(state.topicSmoothing); // nG := alpha, for all topics

      int mapping = 0;
      for (Iterator<Vector.Element> iter = wordCounts.iterateNonZero();
          iter.hasNext();) {
      Vector.Element e = iter.next();
        int word = e.index();
        Vector phiW = eStepForWord(word, digammaGamma);
        phi.assignColumn(mapping, phiW);
        if (iteration == 0) { // first iteration
          columnMap.put(word, mapping);
        }

        for (int k = 0; k < nextGamma.size(); ++k) {
          double g = nextGamma.getQuick(k);
          nextGamma.setQuick(k, g + e.get() * Math.exp(phiW.get(k)));
        }

        mapping++;
      }

View Full Code Here

  /**
   * Compute log q(k|w,doc) for each topic k, for a given word.
   */
  private Vector eStepForWord(int word, Vector digammaGamma) {
    Vector phi = new DenseVector(state.numTopics); // log q(k|w), for each w
    double phiTotal = Double.NEGATIVE_INFINITY; // log Normalizer
    for (int k = 0; k < state.numTopics; ++k) { // update q(k|w)'s param phi
      phi.set(k, state.logProbWordGivenTopic(word, k) + digammaGamma.get(k));
      phiTotal = LDAUtil.logSum(phiTotal, phi.get(k));

      assert !Double.isNaN(phiTotal);
      assert !Double.isNaN(state.logProbWordGivenTopic(word, k));
      assert !Double.isInfinite(state.logProbWordGivenTopic(word, k));
      assert !Double.isNaN(digammaGamma.get(k));
    }
    return phi.plus(-phiTotal); // log normalize
  }
View Full Code Here

    return phi.plus(-phiTotal); // log normalize
  }


  private static Vector digamma(Vector v) {
    Vector digammaGamma = new DenseVector(v.size());
    digammaGamma.assign(v, new BinaryFunction() {
      @Override
      public double apply(double unused, double g) {
        return digamma(g);
      }
    });
View Full Code Here

   * @param x     an Observation
   * @return the Vector of probabilities
   */
  private Vector normalizedProbabilities(DirichletState<O> state,
                                         O x) {
    Vector pi = new DenseVector(numClusters);
    double max = 0;
    for (int k = 0; k < numClusters; k++) {
      double p = state.adjustedProbability(x, k);
      pi.set(k, p);
      if (max < p) {
        max = p;
      }
    }
    // normalize the probabilities by largest observed value
    pi.assign(new TimesFunction(), 1.0 / max);
    return pi;
  }
View Full Code Here

   * Compute the bound centroid by averaging the bound points
   *
   * @return a Vector which is the new bound centroid
   */
  public Vector computeBoundCentroid() {
    Vector result = new DenseVector(getCenter().size());
    for (Vector v : boundPoints) {
      result.assign(v, new PlusFunction());
    }
    return result.divide(boundPoints.size());
  }
View Full Code Here

    if (tokenizer.countTokens() != nball) {
      throw new IllegalArgumentException("Wrong number of attributes in the string");
    }

    int nbattrs = dataset.nbAttributes();
    DenseVector vector = new DenseVector(nbattrs);

    int aId = 0;
    int label = -1;
    for (int attr = 0; attr < nball; attr++) {
      String token = tokenizer.nextToken();
     
      if (ArrayUtils.contains(dataset.getIgnored(), attr)) {
        continue; // IGNORED
      }
     
      if (attr == dataset.getLabelId()) {
        label = dataset.labelCode(token);
      } else if (dataset.isNumerical(aId)) {
        vector.set(aId++, Double.parseDouble(token));
      } else {
        vector.set(aId, dataset.valueOf(aId, token));
        aId++;
      }
    }

    if (label == -1)
View Full Code Here

TOP

Related Classes of org.apache.mahout.matrix.DenseVector$Element

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.