通过密钥提取tfidf向量而不破坏文件格式

q3aa0525  于 2021-06-21  发布在  Pig
关注(0)|答案(1)|浏览(452)

我有大约200000个输出格式为seq2sparse的tfidf向量。现在我需要提取500,但不是随机像分裂函数。我知道其中500个的密钥,我需要它们的数据格式与seq2sparse中的相同。当我打开包含200000个条目的sequencefile时,我可以看到键是用org.apache.hadoop.io.text编码的,值是用org.apache.mahout.math.vectorwritable编码的。
但是当我尝试使用https://github.com/kevinweil/elephant-bird/blob/master/mahout/src/main/java/com/twitter/elephantbird/pig/mahout/vectorwritableconverter.java

https://github.com/kevinweil/elephant-bird/blob/master/pig/src/main/java/com/twitter/elephantbird/pig/store/sequencefilestorage.java
在pig拉丁语中,为了读写它们,输出的key和value都是org.apache.hadoop.io.text。
我确实需要这500个条目,因为我想在trainnb和testnb中使用它们。
基本上,这将是足够的,知道我如何可以做的事情,如马霍特seqdumper逆转。

thigvfpy

thigvfpy1#

虽然没有特定的mahout命令来执行此操作,但您可以使用mahout命令编写一个相对简单的实用程序函数:

  1. org.apache.mahout.common.Pair;
  2. org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable;
  3. org.apache.mahout.math.VectorWritable;

以及:

  1. org.apache.hadoop.io.SequenceFile;
  2. org.apache.hadoop.io.Text;
  3. com.google.common.io.Closeables;

您可以执行以下操作:

  1. // load up the 500 desired keys with some function
  2. Vector<Text>desiredKeys = getDesiredKeys();
  3. //create a new SequenceFile writer for the 500 Desired Vectors
  4. SequenceFile.Writer writer =
  5. SequenceFile.createWriter(fs, conf, output500filePath ,
  6. Text.class,
  7. VectorWritable.class);
  8. try {
  9. // create an iterator over the tfidfVector sequence file
  10. SequenceFileIterable<Text, VectorWritable>seqFileIterable =
  11. new SequenceFileIterable<Text, VectorWritable>(
  12. tfidfVectorPath, true, conf)
  13. // loop over tfidf sequence file and write out only Pairs with keys
  14. // contained in the desiredKeys Vector to the output500file
  15. for (Pair<Text, VectorWritable> pair : seqFileIterable) {
  16. if(desiredKeys.contains(pair.getFirst())){
  17. writer.append(pair.getFirst(),pair.getSecond());
  18. }
  19. }
  20. }finally {
  21. Closeables.close(writer, false);
  22. }

并使用“output500file”的路径作为trainnb的输入。使用vector.contains()并不是最有效的方法,但这是一般的想法。

展开查看全部

相关问题