最近使用spark的MLIb来计算TF-IDF,按照官网的示例代码:import org.apache.spark.rdd.RDD
import org.apache.spark.SparkContext
import org.apache.spark.mllib.feature.HashingTF
import org.apache.spark.mllib.linalg.Vectorval sc: SparkContext = ...// Load documents (one per line).
val documents: RDD[Seq[String]] = sc.textFile("...").map(_.split(" ").toSeq)val hashingTF = new HashingTF()
val tf: RDD[Vector] = hashingTF.transform(documents)import org.apache.spark.mllib.feature.IDF// ... continue from the previous example
tf.cache()
val idf = new IDF().fit(tf)
val tfidf: RDD[Vector] = idf.transform(tf)最后得到是Vector的RDD,Vector是一个抽象类,在这里一般返回的是其子类SparseVector,包含了三个域:size,indices,values。 values是一个Double型的数组,就是文档中每个词的tf-idf值,可是,当我要取出这个值对应的词时,却发现无从下手,不知道找到对应的词汇。有没有大神知道呢?

解决方案 »

  1.   

    这里好少人,您可以 @cloud881001问问
      

  2.   

    hello,
    http://stackoverflow.com/questions/35205865/what-is-the-difference-between-hashingtf-and-countvectorizer-in-sparkHashingTF不可逆的,CountVectorizer我也没找到如何逆,不知道你解决了没有?
    rube.q
      

  3.   

    JavaRDD<Vector> idfvector=idfModel.transform(tagVectorTF);
            idfvector.foreach(new VoidFunction<Vector>() {

    /**
     * 
     */
    private static final long serialVersionUID = 1L; @Override
    public void call(Vector t) throws Exception {
    SparseVector ss=(SparseVector) t;
    double[] aa=ss.values();
    System.out.println("idf--"+t+"-st--"+aa[2]);

    }
    });
    java写法强转下就成了