org.apache.mahout.math.Vector.iterator()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(104)

本文整理了Java中org.apache.mahout.math.Vector.iterator()方法的一些代码示例,展示了Vector.iterator()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector.iterator()方法的具体详情如下:
包路径:org.apache.mahout.math.Vector
类名称:Vector
方法名:iterator

Vector.iterator介绍

[英]Iterates over all elements

  • NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call #getElement(int) for the given index
    [中]迭代所有元素
    *注意:由于性能原因,实现可能会选择重用返回的元素,因此如果需要它的副本,应该为给定索引调用#getElement(int)

代码示例

代码示例来源:origin: cloudera/mahout

private AllIterator() {
 it = vector.iterator();
}

代码示例来源:origin: cloudera/mahout

@Override
public Iterator<Element> iterator() {
 return delegate.iterator();
}

代码示例来源:origin: cloudera/mahout

public Iterator<Element> iterator() {
 return new AbstractIterator<Element>() {
  private final Iterator<Element> i = vector.iterator();

代码示例来源:origin: org.apache.mahout/mahout-utils

public static void vectorToCSVString(Vector vector,
                   boolean namesAsComments,
                   Appendable bldr) throws IOException {
 if (namesAsComments && vector instanceof NamedVector){
  bldr.append('#').append(((NamedVector) vector).getName()).append('\n');
 }
 Iterator<Vector.Element> iter = vector.iterator();
 boolean first = true;
 while (iter.hasNext()) {
  if (first) {
   first = false;
  } else {
   bldr.append(',');
  }
  Vector.Element elt = iter.next();
  bldr.append(String.valueOf(elt.get()));
 }
 bldr.append('\n');
}

代码示例来源:origin: cloudera/mahout

private static void doTestIterators(Vector vector, Collection<Integer> expectedIndices) {
 expectedIndices = new HashSet<Integer>(expectedIndices);
 Iterator<Vector.Element> allIterator = vector.iterator();
 int index = 0;
 while (allIterator.hasNext()) {
  Vector.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++;
 }
 Iterator<Vector.Element> nonZeroIterator = vector.iterateNonZero();
 while (nonZeroIterator.hasNext()) {
  Vector.Element element = nonZeroIterator.next();
  index = element.index();
  assertTrue(expectedIndices.contains(index));
  assertEquals((double) index * 2, element.get(), EPSILON);
  expectedIndices.remove(index);
 }
 assertTrue(expectedIndices.isEmpty());
}

代码示例来源:origin: cloudera/mahout

@Test
public void testIteratorSet() {
 Vector clone = test.clone();
 Iterator<Vector.Element> it = clone.iterateNonZero();
 while (it.hasNext()) {
  Vector.Element e = it.next();
  e.set(e.get() * 2.0);
 }
 it = clone.iterateNonZero();
 while (it.hasNext()) {
  Vector.Element e = it.next();
  assertEquals(test.get(e.index()) * 2.0, e.get(), EPSILON);
 }
 clone = test.clone();
 it = clone.iterator();
 while (it.hasNext()) {
  Vector.Element e = it.next();
  e.set(e.get() * 2.0);
 }
 it = clone.iterator();
 while (it.hasNext()) {
  Vector.Element e = it.next();
  assertEquals(test.get(e.index()) * 2.0, e.get(), EPSILON);
 }
}

代码示例来源:origin: cloudera/mahout

@Test
public void testIterator() {
 Iterator<Vector.Element> iterator = test.iterateNonZero();
 checkIterator(iterator, gold);
 iterator = test.iterator();
 checkIterator(iterator, gold);
 double[] doubles = {0.0, 5.0, 0, 3.0};
 RandomAccessSparseVector zeros = new RandomAccessSparseVector(doubles.length);
 for (int i = 0; i < doubles.length; i++) {
  zeros.setQuick(i, doubles[i]);
 }
 iterator = zeros.iterateNonZero();
 checkIterator(iterator, doubles);
 iterator = zeros.iterator();
 checkIterator(iterator, doubles);
 doubles = new double[]{0.0, 0.0, 0, 0.0};
 zeros = new RandomAccessSparseVector(doubles.length);
 for (int i = 0; i < doubles.length; i++) {
  zeros.setQuick(i, doubles[i]);
 }
 iterator = zeros.iterateNonZero();
 checkIterator(iterator, doubles);
 iterator = zeros.iterator();
 checkIterator(iterator, doubles);
}

代码示例来源:origin: cloudera/mahout

assertEquals("mutation via setQuick() fails to change lengthSquared", expected, v.getLengthSquared(), EPSILON);
Iterator<Vector.Element> it = v.iterator();
while (it.hasNext()) {
 Vector.Element e = it.next();

相关文章