- colab사용하여 word cloud 생성하기
from google.colab import auth
from google.colab import drive
auth.authenticate_user()
drive.mount('/content/drive')
cd '/content/drive/MyDrive/Colab Notebooks/data/'
#데이터 호출
import pandas as pd
import os
from google.colab import files
print(os.getcwd())
file_name = '뉴스.csv'
# df = pd.read_csv(file_name, encoding='utf-8')
df = pd.read_csv(file_name, encoding='CP949')
arr_str = df['title']
str_title = ' '.join(arr_str)
# word cloud생성
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 한글 폰트 패스로 지정
import matplotlib.font_manager as fm
import re
import collections
import numpy as np
# 제외할 단어
spwords = set(STOPWORDS)
arrSpwords = ['1월','2월']
for w in arrSpwords:
spwords.add(w)
# 대상 단어
text_dictionary= WordCloud().process_text(str_title)
text_dictionary = {k: v for k, v in text_dictionary.items() if k not in arrSpwords}
text_dictionary={k: v for k, v in sorted(text_dictionary.items(),reverse=True, key=lambda item: item[1])}
# 상위빈도 15개 분리
text_dictionary_split = dict(list(text_dictionary.items())[:15])
# word cloud
wordcloud = WordCloud(max_font_size=200, stopwords=spwords, font_path='BMDOHYEON.ttf',background_color='white', width=800, height=800).generate_from_frequencies(text_dictionary_split)
fig = plt.figure(figsize=(10, 10))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
fig.savefig(file_name.replace('csv','png'))
Jupyter notebook 설치 (0) | 2021.10.29 |
---|---|
[Python] 'pip3'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치 파일이 아닙니다. 오류 (0) | 2021.10.29 |
Python 설치방법(Windows 10) (0) | 2021.10.29 |
네이버 부동산 기사 크롤링 (0) | 2021.10.13 |
댓글 영역