在输出格式的seq2sparse交付中,我有大约200000个tfidf向量.现在我需要提取500,但不是随机的,比如分裂函数。我知道其中500个密钥,我需要它们的数据格式与来自seq2sparse的数据格式相同。当我用200000个条目打开序列文件时,我可以看到键是用org.apache.hadoop.io.Text编码的,而值是用org.apache.mahout.math.VectorWritable编码的。
和
在Pig拉丁文中,用于读和写它们的输出具有键和值都是org.apache.hadoop.io.Text。
我确实需要这种格式的500条条目,因为我想在trainnb和testnb中使用它们。
基本上,它就足以知道我如何能够做一些类似于mahout的反向操作。
发布于 2014-05-08 08:14:28
虽然没有特定的Mahout命令可以这样做,但您可以使用Mahout的命令编写一个相对简单的实用函数:
org.apache.mahout.common.Pair;
org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable;
org.apache.mahout.math.VectorWritable;
以及:
org.apache.hadoop.io.SequenceFile;
org.apache.hadoop.io.Text;
com.google.common.io.Closeables;
您可以这样做:
// load up the 500 desired keys with some function
Vector<Text>desiredKeys = getDesiredKeys();
//create a new SequenceFile writer for the 500 Desired Vectors
SequenceFile.Writer writer =
SequenceFile.createWriter(fs, conf, output500filePath ,
Text.class,
VectorWritable.class);
try {
// create an iterator over the tfidfVector sequence file
SequenceFileIterable<Text, VectorWritable>seqFileIterable =
new SequenceFileIterable<Text, VectorWritable>(
tfidfVectorPath, true, conf)
// loop over tfidf sequence file and write out only Pairs with keys
// contained in the desiredKeys Vector to the output500file
for (Pair<Text, VectorWritable> pair : seqFileIterable) {
if(desiredKeys.contains(pair.getFirst())){
writer.append(pair.getFirst(),pair.getSecond());
}
}
}finally {
Closeables.close(writer, false);
}
并使用"output500file“的路径作为对trainnb的输入。使用vector.contains()并不是最有效的方法,但这将是一般的想法。
https://stackoverflow.com/questions/23502362
复制