本文整理汇总了Java中org.apache.mahout.math.Vector.get方法的典型用法代码示例。如果您正苦于以下问题:Java Vector.get方法的具体用法?Java Vector.get怎么用?Java Vector.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mahout.math.Vector
的用法示例。
在下文中一共展示了Vector.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rMultinom
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/** Returns one sample from a multinomial. */
public static int rMultinom(Vector probabilities) {
// our probability argument are not normalized.
double total = probabilities.zSum();
double nextDouble = RANDOM.nextDouble();
double p = nextDouble * total;
for (int i = 0; i < probabilities.size(); i++) {
double pi = probabilities.get(i);
if (p < pi) {
return i;
} else {
p -= pi;
}
}
// can't happen except for round-off error so we don't care what we return here
return 0;
}
示例2: pTopicGivenTerm
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* Computes {@code p(topic x|term a, document i)} distributions given input document {@code i}.
* {@code pTGT[x][a]} is the (un-normalized) {@code p(x|a,i)}, or if docTopics is {@code null},
* {@code p(a|x)} (also un-normalized).
*
* @param document doc-term vector encoding {@code w(term a|document i)}.
* @param docTopics {@code docTopics[x]} is the overall weight of topic {@code x} in given
* document. If {@code null}, a topic weight of {@code 1.0} is used for all topics.
* @param termTopicDist storage for output {@code p(x|a,i)} distributions.
*/
private void pTopicGivenTerm(Vector document, Vector docTopics, Matrix termTopicDist) {
// for each topic x
for (int x = 0; x < numTopics; x++) {
// get p(topic x | document i), or 1.0 if docTopics is null
double topicWeight = docTopics == null ? 1.0 : docTopics.get(x);
// get w(term a | topic x)
Vector topicTermRow = topicTermCounts.viewRow(x);
// get \sum_a w(term a | topic x)
double topicSum = topicSums.get(x);
// get p(topic x | term a) distribution to update
Vector termTopicRow = termTopicDist.viewRow(x);
// for each term a in document i with non-zero weight
Iterator<Vector.Element> it = document.iterateNonZero();
while (it.hasNext()) {
Vector.Element e = it.next();
int termIndex = e.index();
// calc un-normalized p(topic x | term a, document i)
double termTopicLikelihood = (topicTermRow.get(termIndex) + eta) * (topicWeight + alpha) / (topicSum + eta * numTerms);
termTopicRow.set(termIndex, termTopicLikelihood);
}
}
}
示例3: sumXminusCdivRsquared
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* @param x
* a Vector
* @return the zSum(((x[i]-c[i])/r[i])^2) over all i
*/
private double sumXminusCdivRsquared(Vector x) {
double result = 0;
for (Iterator<Element> it = getRadius().iterateNonZero(); it.hasNext();) {
Element radiusElem = it.next();
int index = radiusElem.index();
double quotient = (x.get(index) - getCenter().get(index))
/ radiusElem.get();
result += quotient * quotient;
}
return result;
}
示例4: rDirichlet
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* Sample from a Dirichlet distribution, returning a vector of probabilities using a stick-breaking
* algorithm
*
* @param totalCounts
* an unnormalized count Vector
* @param alpha0
* a double
* @return a Vector of probabilities
*/
public static Vector rDirichlet(Vector totalCounts, double alpha0) {
Vector pi = totalCounts.like();
double total = totalCounts.zSum();
double remainder = 1.0;
for (int k = 0; k < pi.size(); k++) {
double countK = totalCounts.get(k);
total -= countK;
double betaK = rBeta(1.0 + countK, Math.max(0.0, alpha0 + total));
double piK = betaK * remainder;
pi.set(k, piK);
remainder -= piK;
}
return pi;
}
示例5: infer
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
public Vector infer(Vector original, Vector docTopics) {
Vector pTerm = original.like();
Iterator<Vector.Element> it = original.iterateNonZero();
while (it.hasNext()) {
Vector.Element e = it.next();
int term = e.index();
// p(a) = sum_x (p(a|x) * p(x|i))
double pA = 0;
for (int x = 0; x < numTopics; x++) {
pA += (topicTermCounts.viewRow(x).get(term) / topicSums.get(x)) * docTopics.get(x);
}
pTerm.set(term, pA);
}
return pTerm;
}