Main Content

ldaModel

潜在的ディリクレ配分 (LDA) モデル

説明

潜在的ディリクレ配分 (LDA) モデルは、文書のコレクションに内在するトピックを発見し、トピック内の単語の確率を推測するトピック モデルです。bag-of-n-grams モデルを使用して当てはめられたモデルの場合、ソフトウェアは n-gram を個々の単語として扱います。

作成

関数 fitlda を使用して LDA モデルを作成します。

プロパティ

すべて展開する

LDA モデルのトピック数。正の整数として指定します。

トピックの集中度。正のスカラーとして指定します。関数は、トピックごとの集中度を TopicConcentration/NumTopics に設定します。詳細については、潜在的ディリクレ配分を参照してください。

単語の集中度。非負のスカラーとして指定します。ソフトウェアは単語ごとの集中度を WordConcentration/numWords に設定します。ここで、numWords は入力文書のボキャブラリ サイズです。詳細については、潜在的ディリクレ配分を参照してください。

入力文書セットのトピック確率。ベクトルとして指定します。LDA モデルのコーパス トピック確率は、LDA モデルを当てはめるために使用されるデータ セット全体における各トピックの観測確率です。CorpusTopicProbabilities は 1 行 K 列のベクトルで、K はトピックの数です。CorpusTopicProbabilities の k 番目のエントリは、トピック k の観測確率に対応します。

入力文書ごとのトピック確率。行列として指定します。LDA モデルの文書トピック確率は、LDA モデルを当てはめるために使用される各文書における各トピックの観測確率です。DocumentTopicProbabilities は D 行 K 列の行列です。ここで、D は LDA モデルを当てはめるために使用される文書の数、K はトピックの数です。DocumentTopicProbabilities の (d,k) 番目のエントリは、文書 d におけるトピック k の観測確率に対応します。

いずれかのトピックの確率が 0 の場合 (CorpusTopicProbabilities に 0 が含まれる)、DocumentTopicProbabilitiesTopicWordProbabilities の対応する列は 0 になります。

DocumentTopicProbabilities の行の順序は、学習データの文書の順序に対応します。

トピックごとの単語確率。行列として指定します。LDA モデルのトピック ワード確率は、LDA モデルの各トピックにおける各単語の観測確率です。TopicWordProbabilities は V 行 K 列の行列です。ここで、V は Vocabulary の単語数、K はトピックの数です。TopicWordProbabilities の (v,k) 番目のエントリは、トピック k における単語 v の観測確率に対応します。

いずれかのトピックの確率が 0 の場合 (CorpusTopicProbabilities に 0 が含まれる)、DocumentTopicProbabilitiesTopicWordProbabilities の対応する列は 0 になります。

TopicWordProbabilities の行の順序は、Vocabulary の単語の順序に対応します。

トピックの順序。次のいずれかとして指定します。

  • 'initial-fit-probability' – 初期モデルの当てはめのコーパス トピック確率でトピックを並べ替えます。これらの確率は、fitlda によって返される最初の ldaModel オブジェクトの CorpusTopicProbabilities プロパティです。関数 resume は、結果の ldaModel オブジェクトのトピックを並べ替えません。

  • 'unordered' – トピックを順序付けしません。

LDA モデルを当てはめる時に記録される情報。次のフィールドをもつ struct として指定します。

  • TerminationCode – 終了時の最適化のステータス

    • 0 – 反復制限に達しました。

    • 1 – 対数尤度の許容誤差は満たされています。

  • TerminationStatus – 返された終了コードの説明

  • NumIterations – 実行された反復の回数

  • NegativeLogLikelihoodfitlda に渡されるデータの負の対数尤度

  • Perplexityfitlda に渡されるデータのパープレキシティ

  • Solver – 使用するソルバーの名前

  • History – 最適化の履歴を保持する Struct

  • StochasticInfo – 確率的ソルバーの情報を保持する Struct

データ型: struct

モデル内の単語のリスト。string ベクトルとして指定します。

データ型: string

オブジェクト関数

logpDocument log-probabilities and goodness of fit of LDA model
predictPredict top LDA topics of documents
resumeResume fitting LDA model
topkwordsMost important words in bag-of-words model or LDA topic
transformTransform documents into lower-dimensional space
wordcloudCreate word cloud chart from text, bag-of-words model, bag-of-n-grams model, or LDA model

すべて折りたたむ

この例の結果を再現するために、rng'default' に設定します。

rng('default')

サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

bagOfWords を使用して bag-of-words モデルを作成します。

bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

          Counts: [154×3092 double]
      Vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    "contracted"    …    ]
        NumWords: 3092
    NumDocuments: 154

トピック数 4 の LDA モデルを当てはめます。

numTopics = 4;
mdl = fitlda(bag,numTopics)
Initial topic assignments sampled in 0.263378 seconds.
=====================================================================================
| Iteration  |  Time per  |  Relative  |  Training  |     Topic     |     Topic     |
|            | iteration  | change in  | perplexity | concentration | concentration |
|            | (seconds)  |   log(L)   |            |               |   iterations  |
=====================================================================================
|          0 |       0.17 |            |  1.215e+03 |         1.000 |             0 |
|          1 |       0.02 | 1.0482e-02 |  1.128e+03 |         1.000 |             0 |
|          2 |       0.02 | 1.7190e-03 |  1.115e+03 |         1.000 |             0 |
|          3 |       0.01 | 4.3796e-04 |  1.118e+03 |         1.000 |             0 |
|          4 |       0.01 | 9.4193e-04 |  1.111e+03 |         1.000 |             0 |
|          5 |       0.01 | 3.7079e-04 |  1.108e+03 |         1.000 |             0 |
|          6 |       0.01 | 9.5777e-05 |  1.107e+03 |         1.000 |             0 |
=====================================================================================
mdl = 
  ldaModel with properties:

                     NumTopics: 4
             WordConcentration: 1
            TopicConcentration: 1
      CorpusTopicProbabilities: [0.2500 0.2500 0.2500 0.2500]
    DocumentTopicProbabilities: [154×4 double]
        TopicWordProbabilities: [3092×4 double]
                    Vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    …    ]
                    TopicOrder: 'initial-fit-probability'
                       FitInfo: [1×1 struct]

ワード クラウドを使用してトピックを可視化します。

figure
for topicIdx = 1:4
    subplot(2,2,topicIdx)
    wordcloud(mdl,topicIdx);
    title("Topic: " + topicIdx)
end

Figure contains objects of type wordcloud. The chart of type wordcloud has title Topic: 1. The chart of type wordcloud has title Topic: 2. The chart of type wordcloud has title Topic: 3. The chart of type wordcloud has title Topic: 4.

LDA トピックの最高確率単語の table を作成します。

結果を再現するために、rng'default' に設定します。

rng('default')

サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

bagOfWords を使用して bag-of-words モデルを作成します。

bag = bagOfWords(documents);

トピック数 20 の LDA モデルを当てはめます。詳細出力を抑制するには、'Verbose' を 0 に設定します。

numTopics = 20;
mdl = fitlda(bag,numTopics,'Verbose',0);

最初のトピックの上位 20 語を見つけます。

k = 20;
topicIdx = 1;
tbl = topkwords(mdl,k,topicIdx)
tbl=20×2 table
      Word        Score  
    ________    _________

    "eyes"        0.11155
    "beauty"      0.05777
    "hath"       0.055778
    "still"      0.049801
    "true"       0.043825
    "mine"       0.033865
    "find"       0.031873
    "black"      0.025897
    "look"       0.023905
    "tis"        0.023905
    "kind"       0.021913
    "seen"       0.021913
    "found"      0.017929
    "sin"        0.015937
    "three"      0.013945
    "golden"    0.0099608
      ⋮

スコアに対して逆平均スケーリングを使用して、最初のトピックの上位 20 語を見つけます。

tbl = topkwords(mdl,k,topicIdx,'Scaling','inversemean')
tbl=20×2 table
      Word       Score  
    ________    ________

    "eyes"        1.2718
    "beauty"     0.59022
    "hath"        0.5692
    "still"      0.50269
    "true"       0.43719
    "mine"       0.32764
    "find"       0.32544
    "black"      0.25931
    "tis"        0.23755
    "look"       0.22519
    "kind"       0.21594
    "seen"       0.21594
    "found"      0.17326
    "sin"        0.15223
    "three"      0.13143
    "golden"    0.090698
      ⋮

スケーリングされたスコアをサイズ データとして使用してワード クラウドを作成します。

figure
wordcloud(tbl.Word,tbl.Score);

Figure contains an object of type wordcloud.

LDA モデルを当てはめるために使用される文書の文書トピック確率 (トピック混合率とも呼ばれる) を取得します。

結果を再現するために、rng'default' に設定します。

rng('default')

サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

bagOfWords を使用して bag-of-words モデルを作成します。

bag = bagOfWords(documents);

トピック数 20 の LDA モデルを当てはめます。詳細出力を抑制するには、'Verbose' を 0 に設定します。

numTopics = 20;
mdl = fitlda(bag,numTopics,'Verbose',0)
mdl = 
  ldaModel with properties:

                     NumTopics: 20
             WordConcentration: 1
            TopicConcentration: 5
      CorpusTopicProbabilities: [0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500]
    DocumentTopicProbabilities: [154x20 double]
        TopicWordProbabilities: [3092x20 double]
                    Vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    ...    ] (1x3092 string)
                    TopicOrder: 'initial-fit-probability'
                       FitInfo: [1x1 struct]

学習データ内の最初の文書のトピック確率を表示します。

topicMixtures = mdl.DocumentTopicProbabilities;
figure
bar(topicMixtures(1,:))
title("Document 1 Topic Probabilities")
xlabel("Topic Index")
ylabel("Probability")

この例の結果を再現するために、rng'default' に設定します。

rng('default')

サンプル データを読み込みます。ファイル sonnetsPreprocessed.txt には、シェイクスピアのソネット集の前処理されたバージョンが格納されています。ファイルには、1 行に 1 つのソネットが含まれ、単語がスペースで区切られています。sonnetsPreprocessed.txt からテキストを抽出し、テキストを改行文字で文書に分割した後、文書をトークン化します。

filename = "sonnetsPreprocessed.txt";
str = extractFileText(filename);
textData = split(str,newline);
documents = tokenizedDocument(textData);

bagOfWords を使用して bag-of-words モデルを作成します。

bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

          Counts: [154×3092 double]
      Vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    "contracted"    …    ]
        NumWords: 3092
    NumDocuments: 154

トピック数 20 の LDA モデルを当てはめます。

numTopics = 20;
mdl = fitlda(bag,numTopics)
Initial topic assignments sampled in 0.513255 seconds.
=====================================================================================
| Iteration  |  Time per  |  Relative  |  Training  |     Topic     |     Topic     |
|            | iteration  | change in  | perplexity | concentration | concentration |
|            | (seconds)  |   log(L)   |            |               |   iterations  |
=====================================================================================
|          0 |       0.04 |            |  1.159e+03 |         5.000 |             0 |
|          1 |       0.05 | 5.4884e-02 |  8.028e+02 |         5.000 |             0 |
|          2 |       0.04 | 4.7400e-03 |  7.778e+02 |         5.000 |             0 |
|          3 |       0.04 | 3.4597e-03 |  7.602e+02 |         5.000 |             0 |
|          4 |       0.03 | 3.4662e-03 |  7.430e+02 |         5.000 |             0 |
|          5 |       0.03 | 2.9259e-03 |  7.288e+02 |         5.000 |             0 |
|          6 |       0.03 | 6.4180e-05 |  7.291e+02 |         5.000 |             0 |
=====================================================================================
mdl = 
  ldaModel with properties:

                     NumTopics: 20
             WordConcentration: 1
            TopicConcentration: 5
      CorpusTopicProbabilities: [0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500 0.0500]
    DocumentTopicProbabilities: [154×20 double]
        TopicWordProbabilities: [3092×20 double]
                    Vocabulary: ["fairest"    "creatures"    "desire"    "increase"    "thereby"    "beautys"    "rose"    "might"    "never"    "die"    "riper"    "time"    "decease"    "tender"    "heir"    "bear"    "memory"    "thou"    …    ]
                    TopicOrder: 'initial-fit-probability'
                       FitInfo: [1×1 struct]

新しい文書の配列の上位トピックを予測します。

newDocuments = tokenizedDocument([
    "what's in a name? a rose by any other name would smell as sweet."
    "if music be the food of love, play on."]);
topicIdx = predict(mdl,newDocuments)
topicIdx = 2×1

    19
     8

ワード クラウドを使用して、予測されたトピックを可視化します。

figure
subplot(1,2,1)
wordcloud(mdl,topicIdx(1));
title("Topic " + topicIdx(1))
subplot(1,2,2)
wordcloud(mdl,topicIdx(2));
title("Topic " + topicIdx(2))

Figure contains objects of type wordcloud. The chart of type wordcloud has title Topic 19. The chart of type wordcloud has title Topic 8.

詳細

すべて展開する

バージョン履歴

R2017b で導入