ldaModel
潜在的ディリクレ配分 (LDA) モデル
説明
潜在的ディリクレ配分 (LDA) モデルは、文書のコレクションに内在するトピックを発見し、トピック内の単語の確率を推測するトピック モデルです。bag-of-n-grams モデルを使用して当てはめられたモデルの場合、ソフトウェアは n-gram を個々の単語として扱います。
作成
関数 fitlda
を使用して LDA モデルを作成します。
プロパティ
NumTopics
— トピック数
正の整数
LDA モデルのトピック数。正の整数として指定します。
TopicConcentration
— トピックの集中度
正のスカラー
トピックの集中度。正のスカラーとして指定します。関数は、トピックごとの集中度を TopicConcentration/NumTopics
に設定します。詳細については、潜在的ディリクレ配分を参照してください。
WordConcentration
— 単語の集中度
1
(既定値) | 非負のスカラー
単語の集中度。非負のスカラーとして指定します。ソフトウェアは単語ごとの集中度を WordConcentration/numWords
に設定します。ここで、numWords
は入力文書のボキャブラリ サイズです。詳細については、潜在的ディリクレ配分を参照してください。
CorpusTopicProbabilities
— 入力文書セットのトピック確率
ベクトル
入力文書セットのトピック確率。ベクトルとして指定します。LDA モデルのコーパス トピック確率は、LDA モデルを当てはめるために使用されるデータ セット全体における各トピックの観測確率です。CorpusTopicProbabilities
は 1 行 K 列のベクトルで、K はトピックの数です。CorpusTopicProbabilities
の k 番目のエントリは、トピック k の観測確率に対応します。
DocumentTopicProbabilities
— 入力文書ごとのトピック確率
行列
入力文書ごとのトピック確率。行列として指定します。LDA モデルの文書トピック確率は、LDA モデルを当てはめるために使用される各文書における各トピックの観測確率です。DocumentTopicProbabilities
は D 行 K 列の行列です。ここで、D は LDA モデルを当てはめるために使用される文書の数、K はトピックの数です。DocumentTopicProbabilities
の (d,k) 番目のエントリは、文書 d におけるトピック k の観測確率に対応します。
いずれかのトピックの確率が 0 の場合 (CorpusTopicProbabilities
に 0 が含まれる)、DocumentTopicProbabilities
と TopicWordProbabilities
の対応する列は 0 になります。
DocumentTopicProbabilities
の行の順序は、学習データの文書の順序に対応します。
TopicWordProbabilities
— トピックごとの単語確率
行列
トピックごとの単語確率。行列として指定します。LDA モデルのトピック ワード確率は、LDA モデルの各トピックにおける各単語の観測確率です。TopicWordProbabilities
は V 行 K 列の行列です。ここで、V は Vocabulary
の単語数、K はトピックの数です。TopicWordProbabilities
の (v,k) 番目のエントリは、トピック k における単語 v の観測確率に対応します。
いずれかのトピックの確率が 0 の場合 (CorpusTopicProbabilities
に 0 が含まれる)、DocumentTopicProbabilities
と TopicWordProbabilities
の対応する列は 0 になります。
TopicWordProbabilities
の行の順序は、Vocabulary
の単語の順序に対応します。
TopicOrder
— トピックの順序
'initial-fit-probability'
(既定値) | 'unordered'
トピックの順序。次のいずれかとして指定します。
'initial-fit-probability'
– 初期モデルの当てはめのコーパス トピック確率でトピックを並べ替えます。これらの確率は、fitlda
によって返される最初のldaModel
オブジェクトのCorpusTopicProbabilities
プロパティです。関数resume
は、結果のldaModel
オブジェクトのトピックを並べ替えません。'unordered'
– トピックを順序付けしません。
FitInfo
— LDA モデルを当てはめる時に記録される情報
struct
LDA モデルを当てはめる時に記録される情報。次のフィールドをもつ struct として指定します。
TerminationCode
– 終了時の最適化のステータス0 – 反復制限に達しました。
1 – 対数尤度の許容誤差は満たされています。
TerminationStatus
– 返された終了コードの説明NumIterations
– 実行された反復の回数NegativeLogLikelihood
–fitlda
に渡されるデータの負の対数尤度Perplexity
–fitlda
に渡されるデータのパープレキシティSolver
– 使用するソルバーの名前History
– 最適化の履歴を保持する StructStochasticInfo
– 確率的ソルバーの情報を保持する Struct
データ型: struct
Vocabulary
— モデル内の単語のリスト
string ベクトル
モデル内の単語のリスト。string ベクトルとして指定します。
データ型: string
オブジェクト関数
logp | Document log-probabilities and goodness of fit of LDA model |
predict | Predict top LDA topics of documents |
resume | Resume fitting LDA model |
topkwords | Most important words in bag-of-words model or LDA topic |
transform | Transform documents into lower-dimensional space |
wordcloud | Create word cloud chart from text, bag-of-words model, bag-of-n-grams model, or LDA model |
例
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)
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
LDA トピックの最高確率単語
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);
LDA モデルの文書トピック確率
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")
文書の上位 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)
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))
詳細
潜在的ディリクレ配分
"潜在的ディリクレ配分" (LDA) モデルは、文書のコレクションに内在するトピックを発見し、トピック内の単語の確率を推測する文書トピック モデルです。LDA は、D 個の文書のコレクションを、単語確率 のベクトルによって特徴付けられた K 個のトピックにわたって、トピック混合率 としてモデル化します。このモデルは、トピック混合率 とトピック がそれぞれ集中度パラメーター と をもつディリクレ分布に従うと仮定します。
トピックの混合率 は、長さ K の確率ベクトルです。ここで、K はトピックの数です。エントリ は、トピック i が d 番目の文書に出現する確率です。トピックの混合率は、ldaModel
オブジェクトの DocumentTopicProbabilities
プロパティの行に対応します。
トピック は、長さ V の確率ベクトルです。ここで、V はボキャブラリの単語数です。エントリ は、ボキャブラリの v 番目の単語が i 番目のトピックに出現する確率に対応します。トピック は、ldaModel
オブジェクトの TopicWordProbabilities
プロパティの列に対応します。
トピック とそのトピック混合率でのディリクレ事前分布 について、LDA は文書に対して次の生成プロセスを仮定します。
トピックの混合率を でサンプリングします。確率変数 は、長さ K の確率ベクトルです。ここで、K はトピックの数です。
文書内の単語ごとに、次のようにします。
トピック インデックスを でサンプリングします。確率変数 z は 1 から K までの整数です。ここで、K はトピックの数です。
単語を でサンプリングします。確率変数 w は 1 ~ V の整数です。ここで、V はボキャブラリの単語数であり、ボキャブラリ内の対応する単語を表します。
この生成プロセスの下で、単語 を含む文書の同時分布と、トピックの混合率 、トピック インデックス が次によって与えられます。
ここで、N は文書内の単語数です。z に対する同時分布を合計し、 で積分することで、文書 w の周辺分布が得られます。
次の図は、LDA モデルを確率的グラフィカル モデルとして示したものです。網かけのあるノードは観測変数、網かけのないノードは潜在変数、枠線のないノードはモデル パラメーターです。矢印は確率変数間の依存関係を強調しており、各グループは繰り返しノードを示しています。
ディリクレ分布
"ディリクレ分布" は、多項分布を連続的に一般化したものです。与えられたカテゴリ数 と集中度パラメーター について (ここで は、長さ K の実数の正のベクトル)、ディリクレ分布の確率密度関数は次で与えられます。
ここで、B は次で与えられる多変量ベータ関数を示します。
ディリクレ分布の特殊なケースは "対称ディリクレ分布" です。対称ディリクレ分布は、集中度パラメーター によって特徴付けられます。ここで、 のすべての要素は同じになります。
バージョン履歴
R2017b で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)