最近使用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.Vector
val 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值,可是,當我要取出這個值對應的詞時,卻發現無從下手,不知道找到對應的詞匯。有沒有大神知道呢?
uj5u.com熱心網友回復:
這里好少人,您可以 @cloud881001問問
uj5u.com熱心網友回復:
您好,你的問題解決了嗎?同問怎么解決對應到詞上去啊uj5u.com熱心網友回復:
Hi , 樓主解決了嗎?能否說一下解決方法呢?uj5u.com熱心網友回復:
hello,http://stackoverflow.com/questions/35205865/what-is-the-difference-between-hashingtf-and-countvectorizer-in-spark
HashingTF不可逆的,CountVectorizer我也沒找到如何逆,不知道你解決了沒有?
rube.q
uj5u.com熱心網友回復:
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寫法強轉下就成了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/56395.html
標籤:Spark
下一篇:大資料學習
