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

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

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

Vector.iterateNonZero介绍

[英]Iterates over all non-zero 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

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

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

private NonZeroIterator() {
 it = vector.iterateNonZero();
}

代码示例来源:origin: tdunning/MiA

public void map(VarLongWritable userID, VectorWritable userVector,
      Context context) throws IOException, InterruptedException {
    Iterator<Vector.Element> it = userVector.get().iterateNonZero();
    while (it.hasNext()) {
      int index1 = it.next().index();
      Iterator<Vector.Element> it2 = userVector.get().iterateNonZero();
      while (it2.hasNext()) {
        int index2 = it2.next().index();
        context.write(new IntWritable(index1), new IntWritable(index2));
      }
    }
  }
}

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

public Vector logNormalize(double power, double normLength) {   
 // we can special case certain powers
 if (Double.isInfinite(power) || power <= 1.0) {
  throw new IllegalArgumentException("Power must be > 1 and < infinity");
 } else {
  double denominator = normLength * Math.log(power);
  Vector result = like().assign(this);
  Iterator<Element> iter = result.iterateNonZero();
  while (iter.hasNext()) {
   Element element = iter.next();
   element.set(Math.log1p(element.get()) / denominator);
  }
  return result;
 }
}

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

Matrix createRiIiMaybeTransposed(Vector ratingVector) {
  Preconditions.checkArgument(ratingVector.isSequentialAccess());
  Matrix RiIiMaybeTransposed = new DenseMatrix(ratingVector.getNumNondefaultElements(), 1);
  Iterator<Vector.Element> ratingsIterator = ratingVector.iterateNonZero();
  int index = 0;
  while (ratingsIterator.hasNext()) {
   Vector.Element elem = ratingsIterator.next();
   RiIiMaybeTransposed.setQuick(index++, 0, elem.get());
  }
  return RiIiMaybeTransposed;
 }
}

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

/**
 * Copy-constructor (for use in turning a sparse vector into a dense one, for example)
 * @param vector
 */
public DenseVector(Vector vector) {
 super(vector.size());
 values = new double[vector.size()];
 Iterator<Element> it = vector.iterateNonZero();
 while (it.hasNext()) {
  Element e = it.next();
  values[e.index()] = e.get();
 }
}

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

public RandomAccessSparseVector(Vector other) {
 this(other.size(), other.getNumNondefaultElements());
 Iterator<Element> it = other.iterateNonZero();
 Element e;
 while (it.hasNext() && (e = it.next()) != null) {
  values.put(e.index(), e.get());
 }
}

代码示例来源:origin: tdunning/MiA

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();
      itemIndexWritable.set(itemIndex);
      context.write(itemIndexWritable, 
          new VectorOrPrefWritable(userID, preferenceValue));
    }
  }
}

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

@Override
public Vector times(double x) {
 if (x == 0.0) {
  return like();
 }
 Vector result = like().assign(this);
 if (x == 1.0) {
  return result;
 }
 
 Iterator<Element> iter = result.iterateNonZero();
 while (iter.hasNext()) {
  Element element = iter.next();
  element.set(element.get() * x);
 }
 return result;
}

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

@Override
public Vector divide(double x) {
 if (x == 1.0) {
  return like().assign(this);
 }
 Vector result = like().assign(this);
 Iterator<Element> iter = result.iterateNonZero();
 while (iter.hasNext()) {
  Element element = iter.next();
  element.set(element.get() / x);
 }
 return result;
}

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

public void addAll(Vector v) {
 if (size() != v.size()) {
  throw new CardinalityException(size(), v.size());
 }
 
 Iterator<Element> iter = v.iterateNonZero();
 while (iter.hasNext()) {
  Element element = iter.next();
  values[element.index()] += element.get();
 }
}

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

/** Y' Cu p(u) */
private Matrix YtransponseCuPu(Vector userRatings) {
 Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!");
 Vector YtransponseCuPu = new DenseVector(numFeatures);
 Iterator<Vector.Element> ratings = userRatings.iterateNonZero();
 while (ratings.hasNext()) {
  Vector.Element e = ratings.next();
  YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS);
 }
 return columnVectorAsMatrix(YtransponseCuPu);
}

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

/** Asserts a vector using enumeration equals a given dense vector */
private static void doTestEnumeration(double[] apriori, Vector vector) {
 double[] test = new double[apriori.length];
 Iterator<Vector.Element> iter = vector.iterateNonZero();
 while (iter.hasNext()) {
  Vector.Element e = iter.next();
  test[e.index()] = e.get();
 }
 for (int i = 0; i < test.length; i++) {
  assertEquals(apriori[i], test[i], EPSILON);
 }
}

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

private int copySortedRandomAccessSparseVector(Vector other) {
 int elementCount = other.getNumNondefaultElements();
 OrderedElement[] sortableElements = new OrderedElement[elementCount];
 Iterator<Element> it = other.iterateNonZero();
 Element e;
 int s=0;
 while (it.hasNext() && (e = it.next()) != null) {
  sortableElements[s++] = new OrderedElement(e.index(), e.get());
 }
 Arrays.sort(sortableElements);
 for (int i = 0; i < sortableElements.length; i++) {
  values.getIndices()[i] = sortableElements[i].index;
  values.getValues()[i] = sortableElements[i].value;
 }
 values = new OrderedIntDoubleMapping(values.getIndices(), values.getValues(), elementCount);
 return elementCount;
}

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

public SequentialAccessSparseVector(Vector other) {
 this(other.size(), other.getNumNondefaultElements());
 if (other.isSequentialAccess()) {
  Iterator<Element> it = other.iterateNonZero();
  Element e;
  while (it.hasNext() && (e = it.next()) != null) {
   set(e.index(), e.get());
  }
 } else {
  // If the incoming Vector to copy is random, then adding items
  // from the Iterator can degrade performance dramatically if
  // the number of elements is large as this Vector tries to stay
  // in order as items are added, so it's better to sort the other
  // Vector's elements by index and then add them to this
  copySortedRandomAccessSparseVector(other);
 }    
}

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

@Override
public Vector assign(Vector other) {
 if (size() != other.size()) {
  throw new CardinalityException(size(), other.size());
 }
 values.clear();
 Iterator<Element> it = other.iterateNonZero();
 Element e;
 while (it.hasNext() && (e = it.next()) != null) {
  setQuick(e.index(), e.get());
 }
 return this;
}

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

@Override
public Vector minus(Vector that) {
 if (size != that.size()) {
  throw new CardinalityException(size, that.size());
 }
 // TODO: check the numNonDefault elements to further optimize
 Vector result = like().assign(this);
 Iterator<Element> iter = that.iterateNonZero();
 while (iter.hasNext()) {
  Element thatElement = iter.next();
  int index = thatElement.index();
  result.setQuick(index, this.getQuick(index) - thatElement.get());
 }
 return result;
}

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

@Override
public Vector assign(Vector other) {
 int size = size();
 if (size != other.size()) {
  throw new CardinalityException(size, other.size());
 }
 if (other instanceof SequentialAccessSparseVector) {
  values = ((SequentialAccessSparseVector)other).values.clone();
 } else {
  values = new OrderedIntDoubleMapping();
  Iterator<Element> othersElems = other.iterateNonZero();
  while (othersElems.hasNext()) {
   Element elem = othersElems.next();
   setQuick(elem.index(), elem.get());
  }
 }
 return this;
}

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

@Override
public Vector times(Vector x) {
 if (size != x.size()) {
  throw new CardinalityException(size, x.size());
 }
 Vector to = this;
 Vector from = x;
 // Clone and edit to the sparse one; if both are sparse, edit the more sparse one (more zeroes)
 if (isDense() || (!x.isDense() && getNumNondefaultElements() > x.getNumNondefaultElements())) {
  to = x;
  from = this;
 }
 Vector result = to.like().assign(to);
 Iterator<Element> iter = result.iterateNonZero();
 while (iter.hasNext()) {
  Element element = iter.next();
  element.set(element.get() * from.getQuick(element.index()));
 }
 return result;
}

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

@Override
public Vector plus(Vector x) {
 if (size != x.size()) {
  throw new CardinalityException(size, x.size());
 }
 // prefer to have this be the denser than x
 if (!isDense() && (x.isDense() || x.getNumNondefaultElements() > this.getNumNondefaultElements())) {
  return x.plus(this);
 }
 Vector result = like().assign(this);
 Iterator<Element> iter = x.iterateNonZero();
 while (iter.hasNext()) {
  Element e = iter.next();
  int index = e.index();
  result.setQuick(index, this.getQuick(index) + e.get());
 }
 return result;
}

相关文章