Package org.apache.mahout.matrix

Examples of org.apache.mahout.matrix.Vector


  public void map(LongWritable key, Text values,
      OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
    String foo = values.toString();
    int ix = foo.indexOf(']');
    Cluster canopy = Cluster.decodeCluster(foo.substring(0, ix + 1));
    Vector point = AbstractVector.decodeVector(foo.substring(ix + 3));
    output.collect(new Text(canopy.getIdentifier()), new Text(point
        .asFormatString()));
  }
View Full Code Here


   * @param nPoints the number of times to add the point
   * @throws CardinalityException if the cardinalities disagree
   */
  void addPoints(Vector point, int nPoints) {
    numPoints += nPoints;
    Vector subTotal = (nPoints == 1) ? point.copy() : point.times(nPoints);
    pointTotal = (pointTotal == null) ? subTotal : pointTotal.plus(subTotal);
  }
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(center.cardinality());
    for (Vector v : boundPoints)
      result.assign(v, new PlusFunction());
    return result.divide(boundPoints.size());
  }
View Full Code Here

    collector.collect(new Text(getIdentifier()), new Text("merge "
        + canopy.toString()));
  }

  public boolean shiftToMean() {
    Vector centroid = computeCentroid();
    converged = new EuclideanDistanceMeasure().distance(centroid, center) < convergenceDelta;
    center = centroid;
    numPoints = 1;
    pointTotal = centroid.copy();
    return converged;
  }
View Full Code Here

  DirichletState<Vector> state;

  @Override
  public void map(WritableComparable<?> key, Text values,
      OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
    Vector v = DenseVector.decodeFormat(values.toString());
    // compute a normalized vector of probabilities that v is described by each model
    Vector pi = normalizedProbabilities(state, v);
    // then pick one model by sampling a Multinomial distribution based upon them
    // see: http://en.wikipedia.org/wiki/Multinomial_distribution
    int k = UncommonDistributions.rMultinom(pi);
    output.collect(new Text(String.valueOf(k)), values);
  }
View Full Code Here

   * @param state the DirichletState<Vector> of this iteration
   * @param v an Vector
   * @return the Vector of probabilities
   */
  private static Vector normalizedProbabilities(DirichletState<Vector> state, Vector v) {
    Vector pi = new DenseVector(state.numClusters);
    double max = 0;
    for (int k = 0; k < state.numClusters; k++) {
      double p = state.adjustedProbability(v, 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

   */
  public static Vector rBeta(int K, double shape1, double shape2) {
    //List<Double> params = new ArrayList<Double>(2);
    //params.add(shape1);
    //params.add(Math.max(0, shape2));
    Vector result = new DenseVector(K);
    for (int i = 0; i < K; i++)
      result.set(i, rBeta(shape1, shape2));
    return result;
  }
View Full Code Here

   */
  public static Vector rMultinom(int size, Vector probabilities) {
    // our probability argument may not be normalized.
    double total = probabilities.zSum();
    int cardinality = probabilities.cardinality();
    Vector result = new DenseVector(cardinality);
    for (int i = 0; total > 0 && i < cardinality; i++) {
      double p = probabilities.get(i);
      int ki = rBinomial(size, p / total);
      total -= p;
      size -= ki;
      result.set(i, ki);
    }
    return result;
  }
View Full Code Here

   *
   * @param alpha an unnormalized count Vector
   * @return a Vector of probabilities
   */
  public static Vector rDirichlet(Vector alpha) {
    Vector r = alpha.like();
    double total = alpha.zSum();
    double remainder = 1;
    for (int i = 0; i < r.size(); i++) {
      double a = alpha.get(i);
      total -= a;
      double beta = rBeta(a, Math.max(0, total));
      double p = beta * remainder;
      r.set(i, p);
      remainder -= p;
    }
    return r;
  }
View Full Code Here

  public DirichletState() {
  }

  public Vector totalCounts() {
    Vector result = new DenseVector(numClusters);
    for (int i = 0; i < numClusters; i++)
      result.set(i, clusters.get(i).totalCount);
    return result;
  }
View Full Code Here

TOP

Related Classes of org.apache.mahout.matrix.Vector

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.