本文整理汇总了Java中org.apache.mahout.math.Vector.getNumNondefaultElements方法的典型用法代码示例。如果您正苦于以下问题:Java Vector.getNumNondefaultElements方法的具体用法?Java Vector.getNumNondefaultElements怎么用?Java Vector.getNumNondefaultElements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mahout.math.Vector
的用法示例。
在下文中一共展示了Vector.getNumNondefaultElements方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: vectorToSortedString
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
public static String vectorToSortedString(Vector vector, String[] dictionary) {
List<Pair<String,Double>> vectorValues =
new ArrayList<Pair<String, Double>>(vector.getNumNondefaultElements());
Iterator<Vector.Element> it = vector.iterateNonZero();
while (it.hasNext()) {
Vector.Element e = it.next();
vectorValues.add(Pair.of(dictionary != null ? dictionary[e.index()] : String.valueOf(e.index()),
e.get()));
}
Collections.sort(vectorValues, new Comparator<Pair<String, Double>>() {
@Override public int compare(Pair<String, Double> x, Pair<String, Double> y) {
return y.getSecond().compareTo(x.getSecond());
}
});
Iterator<Pair<String,Double>> listIt = vectorValues.iterator();
StringBuilder bldr = new StringBuilder(2048);
bldr.append('{');
int i = 0;
while (listIt.hasNext() && i < 25) {
i++;
Pair<String,Double> p = listIt.next();
bldr.append(p.getFirst());
bldr.append(':');
bldr.append(p.getSecond());
bldr.append(',');
}
if (bldr.length() > 1) {
bldr.setCharAt(bldr.length() - 1, '}');
}
return bldr.toString();
}
示例2: postInitCorpus
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
private void postInitCorpus() {
totalCorpusWeight = 0;
int numNonZero = 0;
for (int i=0; i<numDocuments; i++) {
Vector v = corpusWeights.viewRow(i);
double norm;
if (v != null && (norm = v.norm(1)) != 0) {
numNonZero += v.getNumNondefaultElements();
totalCorpusWeight += norm;
}
}
String s = "Initializing corpus with %d docs, %d terms, %d nonzero entries, total termWeight %f";
log.info(String.format(s, numDocuments, numTerms, numNonZero, totalCorpusWeight));
}