일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- gensim
- Jpype
- 파이썬
- jvm.py
- Python.h
- 프로그래머스
- centos8
- dead lock
- pandas
- Java
- ShallowCopy
- pyLDAvis
- rest api
- 완주하지못한선수
- Pythonic
- pip install mariadb
- tweepy
- git bash
- elastic search
- 나무자르기
- ELASTIC
- 토픽모델링
- MariaDB
- dataframe
- bulk post
- bs4
- js
- ChromeDriverManager
- 백준
- KONLPY
- Today
- Total
부리부리부리
[Python] pyLDAvis & gensim_models 오류 본문
ValidationError: * Not all rows (distributions) in topic_term_dists sum to 1.
ValidationError: *Not all rows (distributions) in topic_term_dists sum to 1.
이 오류 같은 경우는
>> pyLDAvis.show(ldamodel, corpus, dictionary)
ldamodel에 corpus를 Input으로 넣었을 때,
이런 식으로 나오는데, 여기서 행의 총합이 1이 안될 경우 생기는 오류이다.
나같은 경우에는 데이터 전처리 때 null data를 지우지 않아서 생긴 오류이다.
(즉, 형태소 분석 및 말뭉치 변환 작업을 했을때 LENGTH가 0이 된 놈을 쳐내주지 않아서 생긴 ERROR)
해결하고 나서 들뜬 마음에 다시 돌렸다.
import gensim
from gensim import corpora
import pyLDAvis
import pyLDAvis.gensim_models as gensimvis
"""
dataset : 형태소 분석을 완료한 단어 집합
NUM_TOPICS : LDA model 학습 시 정해야 하는 TOPIC의 개수
"""
def LDA(dataset,NUM_TOPICS):
dictionary = corpora.Dictionary(dataset)
dictionary.filter_extremes(no_below=10, no_above=0.5)
corpus = [dictionary.doc2bow(text) for text in dataset]
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = NUM_TOPICS, id2word=dictionary,
passes=50, random_state=2053)
lda_visualization = gensimvis.prepare(ldamodel,corpus,dictionary)
pyLDAvis.show(lda_visualization)
위 코드 실행 시 다음과 같은 오류가 뜬다.
'/LDAvis.css': ["text/css", open(urls.LDAVIS_CSS_URL, 'r').read()],
OSError: [Errno 22] Invalid argument: "https://cdn.jsdelivr.net/gh/bmabey/pyLDAvis@3.3.1/pyLDAvis/js/ldavis.v1.0.0.css"
근데 이 오류는 진짜 찾아보다가 못찾겠어서 다른 방법을 사용했다.
pyLDAvis.save_html(lda_visualization, './pyldavis_html.html')
html을 저장해서 내가 직접 웹을 띄워서 확인했다.
## test.py
import webbrowser
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello():
return render_template('pyldavis_html.html')
webbrowser.open('http://127.0.0.1:5000')
if __name__ == "__main__":
app.run()
^^^^^^^^ test.py ^^^^^^^
render_template( html ) 을 사용하려면 먼저 templates라는 폴더가 코드를 돌리는 pwd에 있어야한다.
pwd는 다음과 같으므로
저기서 templates 폴더를 새로 만들어준 뒤에 미리 만들어놓은 pyldavis_html.html 파일을 넣어준다.
그러고 나서 test.py를 실행시켜주면 ...
짜잔~ ㅅㄱ링~
'언어 > Python' 카테고리의 다른 글
[pandas] DataFrame을 사용하는 이유 (2) | 2022.11.25 |
---|---|
[Python] 회사 자동 출근 프로그램 (pyinstaller 이용) (0) | 2022.01.11 |
Anaconda 명령어 모음 (0) | 2022.01.11 |
[Python] gensim - LDA parameters (0) | 2021.12.15 |
[Python] module konlpy 관련 오류 해결 (0) | 2021.12.13 |