Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

ldaModel

설명

LDA(잠재 디리클레 할당) 모델은 문서 모음에서 기저 토픽을 발견하고 토픽 내 단어 확률을 추정하는 토픽 모델입니다. 이 모델이 bag-of-n-grams 모델을 사용하여 피팅된 경우 소프트웨어는 n-gram을 개별 단어로 처리합니다.

생성

fitlda 함수를 사용하여 LDA 모델을 만듭니다.

속성

모두 확장

LDA 모델 내 토픽 개수로, 양의 정수로 지정됩니다.

토픽 집중도로, 양의 스칼라로 지정됩니다. 이 함수는 토픽당 집중도를 TopicConcentration/NumTopics로 설정합니다. 자세한 내용은 LDA(잠재 디리클레 할당) 항목을 참조하십시오.

단어 집중도로, 음이 아닌 스칼라로 지정됩니다. 소프트웨어는 단어당 집중도를 WordConcentration/numWords로 설정하며, 여기서 numWords는 입력 문서의 단어집 크기입니다. 자세한 내용은 LDA(잠재 디리클레 할당) 항목을 참조하십시오.

입력 문서 집합의 토픽 확률로, 벡터로 지정됩니다. 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 모델 피팅 시 기록되는 정보로, 다음 필드를 포함하는 구조체로 지정됩니다.

  • TerminationCode – 종료 시 최적화 상태

    • 0 – 반복 한도에 도달했습니다.

    • 1 - 로그 가능도에 대한 허용오차가 충족되었습니다.

  • TerminationStatus - 반환되는 종료 코드에 대한 설명

  • NumIterations – 수행된 반복 횟수

  • NegativeLogLikelihoodfitlda에 전달된 데이터에 대한 음의 로그 가능도

  • Perplexityfitlda에 전달된 데이터에 대한 혼잡도(Perplexity)

  • Solver – 사용된 솔버의 이름

  • History – 최적화 내역을 포함하는 구조체

  • StochasticInfo – 확률적 솔버용 정보를 포함하는 구조체

데이터형: struct

모델 내 단어 목록으로, string형 벡터로 지정됩니다.

데이터형: string

객체 함수

logpDocument log-probabilities and goodness of fit of LDA model
predictPredict top LDA topics of documents
resumeResume fitting LDA model
topkwordsbag-of-words 모델 또는 LDA 토픽에서 가장 중요한 단어
transformTransform documents into lower-dimensional space
wordcloud텍스트, bag-of-words 모델, bag-of-n-grams 모델 또는 LDA 모델에서 워드 클라우드 차트 만들기

예제

모두 축소

이 예제에서 결과를 재현하기 위해 rng'default'로 설정합니다.

rng('default')

예제 데이터를 불러옵니다. 파일 sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다. 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 토픽에서 확률이 가장 높은 단어의 테이블을 만듭니다.

이 결과를 재현하기 위해 rng'default'로 설정합니다.

rng('default')

예제 데이터를 불러옵니다. 파일 sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다. 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 모델을 피팅하는 데 사용된 문서 토픽 확률(토픽 혼합이라고도 함)을 구합니다.

이 결과를 재현하기 위해 rng'default'로 설정합니다.

rng('default')

예제 데이터를 불러옵니다. 파일 sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다. 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")

Figure contains an axes object. The axes object with title Document 1 Topic Probabilities, xlabel Topic Index, ylabel Probability contains an object of type bar.

이 예제에서 결과를 재현하기 위해 rng'default'로 설정합니다.

rng('default')

예제 데이터를 불러옵니다. 파일 sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다. 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))

세부 정보

모두 확장

버전 내역

R2017b에 개발됨