Package org.apache.mahout.math

Examples of org.apache.mahout.math.Vector$Element


    Multiset<String> counts = parse(f);
    return vectorize(counts, w, normalize, dimension);
  }

  static Vector vectorize(Multiset<String> doc, CorpusWeighting w, boolean normalize, int dimension) {
    Vector v = new RandomAccessSparseVector(dimension);
    FeatureVectorEncoder encoder = new StaticWordValueEncoder("text");
    for (String word : doc.elementSet()) {
      encoder.addToVector(word, w.weight(word, doc.count(word)), v);
    }
    if (normalize) {
      return v.assign(Functions.div(v.norm(2)));
    } else {
      return v;
    }
  }
View Full Code Here


    if (f.isDirectory()) {
      for (File file : f.listFiles()) {
        recursivelyVectorize(csv, sf, file, w, normalize, dimension);
      }
    } else {
      Vector v = vectorizeFile(f, w, normalize, dimension);
      csv.printf("%s,%s", f.getParentFile().getName(), f.getName());
      for (int i = 0; i < v.size(); i++) {
        csv.printf(",%.5f", v.get(i));
      }
      csv.printf("\n");
      sf.append(new Text(f.getParentFile().getName()), new VectorWritable(v));
    }
  }
View Full Code Here

  }

  public void reduce(VarLongWritable key, Iterable<VectorWritable> values,
      Context context) throws IOException, InterruptedException {

    Vector recommendationVector = null;
    for (VectorWritable vectorWritable : values) {
      recommendationVector = recommendationVector == null ? vectorWritable
          .get() : recommendationVector.plus(vectorWritable.get());
    }

    Queue<RecommendedItem> topItems = new PriorityQueue<RecommendedItem>(
        recommendationsPerUser + 1,
        Collections.reverseOrder(ByValueRecommendedItemComparator
            .getInstance()));

    Iterator<Vector.Element> recommendationVectorIterator = recommendationVector
        .iterateNonZero();
    while (recommendationVectorIterator.hasNext()) {
      Vector.Element element = recommendationVectorIterator.next();
      int index = element.index();
      float value = (float) element.get();
View Full Code Here

    Reducer<IntWritable, IntWritable, IntWritable, VectorWritable> {

  public void reduce(IntWritable itemIndex1,
      Iterable<IntWritable> itemIndex2s, Context context)
      throws IOException, InterruptedException {
    Vector cooccurrenceRow = new RandomAccessSparseVector(
        Integer.MAX_VALUE, 100);
    for (IntWritable intWritable : itemIndex2s) {
      int itemIndex2 = intWritable.get();
      cooccurrenceRow.set(itemIndex2,
          cooccurrenceRow.get(itemIndex2) + 1.0);
    }
    context.write(itemIndex1, new VectorWritable(cooccurrenceRow));
  }
View Full Code Here

  public Ops() {
  }

  @Override
  public List<Double> classify(String text) throws TException {
    Vector features = new RandomAccessSparseVector(FEATURES);
    enc.addText(text.toLowerCase());
    enc.flush(1, features);
    bias.addToVector((byte[]) null, 1, features);
    Vector r = model.classifyFull(features);
    List<Double> rx = Lists.newArrayList();
    for (int i = 0; i < r.size(); i++) {
      rx.add(r.get(i));
    }
    return rx;
  }
View Full Code Here

    Mapper<VarLongWritable, VectorWritable, IntWritable, VectorOrPrefWritable> {

  public void map(VarLongWritable key, VectorWritable value, Context context)
      throws IOException, InterruptedException {
    long userID = key.get();
    Vector userVector = value.get();
    Iterator<Vector.Element> it = userVector.iterateNonZero();
    IntWritable itemIndexWritable = new IntWritable();
    while (it.hasNext()) {
      Vector.Element e = it.next();
      int itemIndex = e.index();
      float preferenceValue = (float) e.get();
View Full Code Here

    extends
    Reducer<VarLongWritable, VectorWritable, VarLongWritable, VectorWritable> {

  public void reduce(VarLongWritable key, Iterable<VectorWritable> values,
      Context context) throws IOException, InterruptedException {
    Vector partial = null;
    for (VectorWritable vectorWritable : values) {
      partial = partial == null ? vectorWritable.get() : partial
          .plus(vectorWritable.get());
    }
    context.write(key, new VectorWritable(partial));
  }
View Full Code Here

    Reducer<VarLongWritable, VarLongWritable, VarLongWritable, VectorWritable> {

  public void reduce(VarLongWritable userID,
      Iterable<VarLongWritable> itemPrefs, Context context)
      throws IOException, InterruptedException {
    Vector userVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 100);
    for (VarLongWritable itemPref : itemPrefs) {
      userVector.set((int) itemPref.get(), 1.0f);
    }
    context.write(userID, new VectorWritable(userVector));
  }
View Full Code Here

    extends
    Mapper<IntWritable, VectorAndPrefsWritable, VarLongWritable, VectorWritable> {
  public void map(IntWritable key,
      VectorAndPrefsWritable vectorAndPrefsWritable, Context context)
      throws IOException, InterruptedException {
    Vector cooccurrenceColumn = vectorAndPrefsWritable.getVector();
    List<Long> userIDs = vectorAndPrefsWritable.getUserIDs();
    List<Float> prefValues = vectorAndPrefsWritable.getValues();
   
    for (int i = 0; i < userIDs.size(); i++) {
      long userID = userIDs.get(i);
      float prefValue = prefValues.get(i);
      Vector partialProduct = cooccurrenceColumn.times(prefValue);
      context.write(new VarLongWritable(userID),
          new VectorWritable(partialProduct));
    }
  }
View Full Code Here

 
  public static List<Vector> getPoints(double[][] raw) {
    List<Vector> points = new ArrayList<Vector>();
    for (int i = 0; i < raw.length; i++) {
      double[] fr = raw[i];
      Vector vec = new RandomAccessSparseVector(fr.length);
      vec.assign(fr);
      points.add(vec);
    }
    return points;
  }
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.Vector$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.