BERTopic 퀵스타트 — 설치하고 토픽 뽑아보기
BERTopic 퀵스타트 — 설치하고 토픽 뽑아보기
BERTopic은 설치하고 몇 줄만 쓰면 문서 묶음에서 토픽을 뽑아주는 파이썬 라이브러리예요. 설치부터 첫 토픽 추출까지 한 번에 볼게요.
설치
pip install bertopic
PyPI 에서 torch, scikit-learn, umap-learn, hdbscan 같은 의존성이 함께 설치돼요.
기본 사용
from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups
docs = fetch_20newsgroups(subset='all', remove=('headers','footers','quotes'))['data']
topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)
topics: 각 문서가 속한 토픽 번호 목록이에요.-1은 이상치(어느 토픽에도 안 넣은 문서)예요.probs: 각 문서가 각 토픽에 속할 확률이에요.
결과 확인
>>> topic_model.get_topic_info().head(3)
Topic Count Name
0 -1 174 -1_people_gun_god_believe_war
1 0 124 0_space_nasa_launch_shuttle_orbit
2 1 108 1_windows_dos_drivers_drive_disk
get_topic(topic_id) 로 특정 토픽의 대표 단어와 가중치를 볼 수 있어요.
시각화
topic_model.visualize_topics() # 토픽 간 거리 지도
topic_model.visualize_barchart() # 토픽별 키워드 막대