> ## Content Index
> Fetch the complete content index at: https://bongholee.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# [Python]Top2Vec 각 Document 별 Topic 찾기
- URL: https://bongholee.com/top2/
- Published: 2022-05-02T11:59:06.000Z
- Updated: 2022-05-02T11:59:06.000Z
- Author: Bongho Lee
- Tags: Data, #Import 2026-11-22 06:32

## 배경

- Top2Vec으로 토픽 모델링 이후 각 Document 별 해당되는 토픽을 알고자 할 때

## 해결책

- Top2Vec의 [Github](https://github.com/ddangelov/Top2Vec/blob/d625b507aa18a921b7d0a3710d1a4c176f9b8f84/top2vec/Top2Vec.py?ref=bongholee.com#L913) 코드를 확인해보니, Single Underscore Function으로 있는 \_calculate\_documents\_topic의 Return Value로 doc\_top, doc\_dist를 갖는데, doc\_top은 각각 Document 별 Topic에 대한 List이다. 그리고 doc\_dist는 Topic Vector와의 Distance 값이다.
- "model.search\_documents\_by\_topic(0, num\_docs=1)" 형태로 Topic 0에 해당하는 Document 1개를 가지고 오라고 할 때 doc\_top,doc\_dist를 통해 가지고 온다.
- 아직 모델이 개발중이라서 그런 것인지, 해당 변수에 대해서 model.doc\_top, model.doc\_list 형태로 바로 접근이 가능하다.

```python
import numpy as np
import pandas as pd
from top2vec import Top2Vec
from sklearn.datasets import fetch_20newsgroups

np.random.seed(0)

# Data Loading
newsgroups = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))

# Modeling 
model = Top2Vec(newsgroups.data, workers=4, embedding_model='universal-sentence-encoder')

model.search_documents_by_topic(0, num_docs=1)
```

위에까지 실행하고 나면 아래와 같은 결과를 볼 수 있다.

```python
#(array(['Well, the Patrick Division got a little more interesting last night. ...dtype=object), array([0.7826741], dtype=float32), array([17864]))
```

위 내용은 17864번 인덱스 Document가 Topic Vector로부터 가장 가까이 있고 거리 값은 0.7826741이라는 것이다. 위 Document는 model.doc\_top\[17684\], model.doc\_list\[17684\]에서 확인할 수 있다.

Topic Vector은 다음과 같이 동일한 Cluster를 가지고 있는 Document Vector의 평균 값을 이용해서 생성하는 듯하다.(자세히 보지는 않았음)

```python
self.topic_vectors = self._l2_normalize(
            np.vstack([self.document_vectors[np.where(cluster_labels == label)[0]]
                      .mean(axis=0) for label in unique_labels]))
```