本文整理汇总了Java中org.apache.mahout.math.Matrix.viewRow方法的典型用法代码示例。如果您正苦于以下问题:Java Matrix.viewRow方法的具体用法?Java Matrix.viewRow怎么用?Java Matrix.viewRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mahout.math.Matrix
的用法示例。
在下文中一共展示了Matrix.viewRow方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeMatrix
import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
static void writeMatrix(Matrix origMatrix,
Path outPath, Path tmpPath, String label) throws IOException {
Configuration conf = new Configuration();
Path outputDir = new Path(outPath, label + origMatrix.numRows() + "x"
+ origMatrix.numCols());
FileSystem fs = FileSystem.get(outputDir.toUri(), conf);
if (!fs.exists(outputDir)) {
Path outputFile = new Path(outputDir, "singleSliceMatrix");
SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
outputFile, IntWritable.class, VectorWritable.class);
VectorWritable vectorWritable = new VectorWritable();
try {
for (int r = 0; r < origMatrix.numRows(); r++) {
Vector vector = origMatrix.viewRow(r);
vectorWritable.set(vector);
writer.append(new IntWritable(r), vectorWritable);
}
} finally {
writer.close();
}
} else {
log.warn("----------- Skip matrix " + outputDir + " - already exists");
}
}
示例2: trainDocTopicModel
import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
public void trainDocTopicModel(Vector original, Vector topics, Matrix docTopicModel) {
// first calculate p(topic|term,document) for all terms in original, and all topics,
// using p(term|topic) and p(topic|doc)
pTopicGivenTerm(original, topics, docTopicModel);
normalizeByTopic(docTopicModel);
// now multiply, term-by-term, by the document, to get the weighted distribution of
// term-topic pairs from this document.
Iterator<Vector.Element> it = original.iterateNonZero();
while (it.hasNext()) {
Vector.Element e = it.next();
for (int x = 0; x < numTopics; x++) {
Vector docTopicModelRow = docTopicModel.viewRow(x);
docTopicModelRow.setQuick(e.index(), docTopicModelRow.getQuick(e.index()) * e.get());
}
}
// now recalculate p(topic|doc) by summing contributions from all of pTopicGivenTerm
topics.assign(0.0);
for (int x = 0; x < numTopics; x++) {
topics.set(x, docTopicModel.viewRow(x).norm(1));
}
// now renormalize so that sum_x(p(x|doc)) = 1
topics.assign(Functions.mult(1/topics.norm(1)));
}
示例3: pTopicGivenTerm
import org.apache.mahout.math.Matrix; //导入方法依赖的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);
}
}
}
示例4: toDistributedRowMatrix
import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
/**
* Convert an in-memory representation of a matrix to a distributed version It
* then can be used in distributed jobs
*
* @param oriMatrix
* @return path that contains the matrix files
* @throws IOException
*/
static DistributedRowMatrix toDistributedRowMatrix(Matrix origMatrix,
Path outPath, Path tmpPath, String label) throws IOException {
Configuration conf = new Configuration();
Path outputDir = new Path(outPath, label + origMatrix.numRows() + "x"
+ origMatrix.numCols());
FileSystem fs = FileSystem.get(outputDir.toUri(), conf);
if (!fs.exists(outputDir)) {
Path outputFile = new Path(outputDir, "singleSliceMatrix");
SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
outputFile, IntWritable.class, VectorWritable.class);
VectorWritable vectorWritable = new VectorWritable();
try {
for (int r = 0; r < origMatrix.numRows(); r++) {
Vector vector = origMatrix.viewRow(r);
vectorWritable.set(vector);
writer.append(new IntWritable(r), vectorWritable);
}
} finally {
writer.close();
}
} else {
log.warn("----------- Skip matrix " + outputDir + " - already exists");
}
DistributedRowMatrix dMatrix = new DistributedRowMatrix(outputDir, tmpPath,
origMatrix.numRows(), origMatrix.numCols());
dMatrix.setConf(conf);
return dMatrix;
}
示例5: testMAHOUT_1221
import org.apache.mahout.math.Matrix; //导入方法依赖的package包/类
@Test
public void testMAHOUT_1221() {
// create a matrix with an unassigned row 0
Matrix matrix = new SparseMatrix(1, 1);
Vector view = matrix.viewRow(0);
final double value = 1.23;
view.assign(value);
// test whether the update in the view is reflected in the matrix
assertEquals("Matrix valye", view.getQuick(0), matrix.getQuick(0, 0),
EPSILON);
}