2016-09-02 6 views
0

LDAモデル(org.apache.spark.ml.clustering.LDA)からvocabArrayを取得する方法。私はちょうどスキャンされた単語の数を返すvocabSizeを取得しています。トピック索引をLDAのトピック単語に変換する方法

理想的には、モデルからの実際の単語の配列を必要とし、termindicesに基づいて、バケット内の単語を見たいと思います。

私はスカラーでこれを行う必要があります。どんな提案も役に立ちます。私が今まで試してみました

物事、私topicIndicesは、私はこの

val topics = topicIndices.map { case (terms, termWeights) => 
     terms.zip(termWeights).map { case (term, weight) => (vocabArray(term.toInt), weight) } 
    } 

などの話題を取得しようとしています

topicIndices: org.apache.spark.sql.DataFrame = [topic: int, termIndices: array<int>, termWeights: array<double>] 

データフレームである。しかし、それは次のようなエラーがスローされます

> 

val topics = topicIndices.map { case (terms, termWeights) => 
     terms.zip(termWeights).map { case (term, weight) => (vocabArray(term.toInt), weight) } 
    } <console>:96: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: org.apache.spark.sql.Row 
     val topics = topicIndices.map { case (terms, termWeights) => 
              ^<console>:97: error: not found: value terms 
      terms.zip(termWeights).map { case (term, weight) => (vocabArray(term.toInt), weight) } 
      ^
+0

spark-shellを使用していますか? – eliasah

+0

私はこの実験にdatabricksノートブックを使用しています。 – Nabs

+0

問題は古いmllibのLDA記述規則でトピックをArrayに戻すために使用されます。各トピックは(用語索引、トピックの用語の重み)でした。 mlのLDA記述は[topic:int、termIndices:array 、termWeights:array ]を返しています。以前は、キー値のペアをマップするのは簡単でした。この新しいマップでマッピングする方法は何ですか? – Nabs

答えて

2

問題が解決しました。ここに欠けている部分があります。ひとたびあなたが記述記号からdfを取得すると、対応する単語を得るのに役立つコードがここにあります。 (注:このコードはLDAのmlライブラリで動作します)

val topicDF = model.describeTopics(maxTermsPerTopic = 10) 
for ((row) <- topicDF) { 
     val topicNumber = row.get(0) 
     val topicTerms = row.get(1) 
     println ("Topic: "+ topicNumber) 
} 

import scala.collection.mutable.WrappedArray 

val vocab = vectorizer.vocabulary 

for ((row) <- topicDF) { 
    val topicNumber = row.get(0) 
    //val terms = row.get(1) 
    val terms:WrappedArray[Int] = row.get(1).asInstanceOf[WrappedArray[Int]] 
    for ((termIdx) <- 0 until 4) { 
     println("Topic:" + topicNumber + " Word:" + vocab(termIdx)) 
    } 
} 

topicDF.printSchema 
import org.apache.spark.sql.Row 

topicDF.collect().foreach { r => 
       r match { 
         case _: Row => ("Topic:" + r) 
         case unknow => println("Something Else") 
     } 
} 

topicDF.collect().foreach { r => { 
         println("Topic:" + r(0)) 
         val terms:WrappedArray[Int] = r(1).asInstanceOf[WrappedArray[Int]] 
         terms.foreach { 
           t => { 
             println("Term:" + vocab(t)) 
           } 
         } 
       } 
     } 
関連する問題