Package org.apache.mahout.math.Vector

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


  public int maxIndex(Vector instance) {
    Iterator<Element> it = instance.iterator();
    int maxIndex = -1;
    double val = Integer.MIN_VALUE;
    while (it.hasNext()) {
      Element e = it.next();
      if (val <= e.get()) {
        maxIndex = e.index();
        val = e.get();
      }
    }
    return maxIndex;
  }
View Full Code Here


    }
    Vector sample = new RandomAccessSparseVector(original.size(), sampleSize);
    Iterator<Element> sampledElements =
        new FixedSizeSamplingIterator<Vector.Element>(sampleSize, original.nonZeroes().iterator());
    while (sampledElements.hasNext()) {
      Element elem = sampledElements.next();
      sample.setQuick(elem.index(), elem.get());
    }
    return sample;
  }
View Full Code Here

    public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) {
      Iterator<Vector.Element> xi = x.all().iterator();
      Iterator<Vector.Element> yi = y.all().iterator();
      OrderedIntDoubleMapping updates = new OrderedIntDoubleMapping(false);
      while (xi.hasNext() && yi.hasNext()) {
        Element xe = xi.next();
        updates.set(xe.index(), f.apply(xe.get(), yi.next().get()));
      }
      x.mergeUpdates(updates);
      return x;
    }
View Full Code Here

    @Override
    public Vector assign(Vector x, Vector y, DoubleDoubleFunction f) {
      Iterator<Vector.Element> xi = x.all().iterator();
      Iterator<Vector.Element> yi = y.all().iterator();
      while (xi.hasNext() && yi.hasNext()) {
        Element xe = xi.next();
        x.setQuick(xe.index(), f.apply(xe.get(), yi.next().get()));
      }
      return x;
    }
View Full Code Here

  private static void doTestIterators(Vector vector, Collection<Integer> expectedIndices) {
    expectedIndices = Sets.newHashSet(expectedIndices);
    Iterator<Element> allIterator = vector.all().iterator();
    int index = 0;
    while (allIterator.hasNext()) {
      Element element = allIterator.next();
      assertEquals(index, element.index());
      if (expectedIndices.contains(index)) {
        assertEquals((double) index * 2, element.get(), EPSILON);
      } else {
        assertEquals(0.0, element.get(), EPSILON);
      }
      index++;
    }

    for (Element element : vector.nonZeroes()) {
      index = element.index();
      assertTrue(expectedIndices.contains(index));
      assertEquals((double) index * 2, element.get(), EPSILON);
      expectedIndices.remove(index);
    }
    assertTrue(expectedIndices.isEmpty());
  }
View Full Code Here

    vector.set(4, 3);
    vector.set(6, 4);

    // Test non zero iterator.
    Iterator<Element> it = vector.nonZeroes().iterator();
    Element element = null;
    int i = 0;
    while (it.hasNext()) {  // hasNext is called more often than next
      if (i % 2 == 0) {
        element = it.next();
      }
      //noinspection ConstantConditions
      assertEquals(element.index(), 2* (i/2));
      assertEquals(element.get(), vector.get(2* (i/2)), 0);
      ++i;
    }
    assertEquals(7, i)// Last element is print only once.

    // Test all iterator.
    it = vector.all().iterator();
    element = null;
    i = 0;
    while (it.hasNext()) { // hasNext is called more often than next
      if (i % 2 == 0) {
        element = it.next();
      }
      //noinspection ConstantConditions
      assertEquals(element.index(), i/2);
      assertEquals(element.get(), vector.get(i/2), 0);
      ++i;
    }
    assertEquals(197, i)// Last element is print only once.
  }
View Full Code Here

    // Test all iterator.
    it = vector.all().iterator();
    i = 0;
    while (it.hasNext()) { // hasNext is called more often than next
      Element element = it.next();
      assertEquals(i, element.index());
      ++i;
    }
    assertFalse(it.hasNext());
    assertEquals(99, i)// Last element is print only once.
  }
View Full Code Here

    Set<Double> expectedValue = Sets.newHashSet(2.0, 8.0, 3.0, 6.0, 1.0);
    // Test non zero iterator.
    Iterator<Element> it = vector.nonZeroes().iterator();
    int i = 0;
    while (it.hasNext()) {
      Element e = it.next();
      assertTrue(expected.contains(e.index()));
      assertTrue(expectedValue.contains(e.get()));
      ++i;
    }
    assertEquals(8, i);

    // Check if the non zero elements are correct.
    assertEquals(8, vector.getNumNonZeroElements());

    // Set one element to 0.
    it = vector.nonZeroes().iterator();
    i = 0;
    while (it.hasNext()) {
      Element e = it.next();
      if (e.index() == 5) {
        e.set(0.0);
      }
      ++i;
    }
    assertEquals(8, i);
    assertEquals(7, vector.getNumNonZeroElements());

    // Remove one element
    it = vector.nonZeroes().iterator();
    i = 0;
    while (it.hasNext()) {
      Element e = it.next();
      if (e.index() == 5) {
        vector.set(5, 0.0);
      }
      ++i;
    }
    assertEquals(7, i); // This just got messed up.
View Full Code Here

  public double distance(Vector v1, Vector v2) {
    Vector distVector = v1.minus(v2);
    double sum = 0.0;
    Iterator<Element> it = distVector.iterateNonZero();
    while (it.hasNext()) {
      Element e = it.next();
      sum += Math.pow(Math.abs(e.get()), exponent);
    }
    return Math.pow(sum, 1.0 / exponent);
  }
View Full Code Here

    Vector vector = value.get();
    int label = key.get();
    double sigmaK = labelSum.get(label);
    Iterator<Element> it = vector.iterateNonZero();
    while (it.hasNext()) {
      Element e = it.next();
      double numerator = featureSum.get(e.index()) - e.get() + alphaI;
      double denominator = totalSum - sigmaK + vocabCount;
      double weight = Math.log(numerator / denominator);
      perLabelThetaNormalizer.set(label, perLabelThetaNormalizer.get(label) + weight);
    }
  }
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.