Python(Colab) 워드클라우드(WordCloud)
2023. 6. 9. 16:30ㆍ파이썬
워드 클라우드
- 핵심 단어를 시각화 하는 기번
- 문서의 키워드, 개념 등을 직관적으로 파악할 수 있게 핵심 단어를 시각적으로 돋보이게 하는 기법
기초 설정
! pip install wordcloud
from wordcloud import WordCloud
generate(): 단어별 출현 빈도수를 비율로 반환하는 객체를 생성
.words_: 단어에 대한 비율을 반환
wordcloud.words_

글자수 데로 정렬하기
noun_text.sort(key=lambda x : len(x))
print(noun_text)

불용어말고 알고리즘으로 제외 시키기
#STOPWORDS말고 알고리즘으로 제외 시키기
stop_words = ['함','것','제','정','바','그']
noun_text = [each_word for each_word in noun_text if each_word not in stop_words]
print(noun_text)

한글자 전부 제외시키기
noun_text = [each_word for each_word in noun_text if len(each_word) > 1]
print(noun_text)

Counter 라이브러리 : 파일의 텍스트 빈도수를 체크해준다
기초설정
from collections import Counter
count = Counter(noun_text)
print(count)

가장 빈도수가 높은 6개만 뽑아오기
Counter.most_common(개수)
data = count.most_common(20)
print(data)

🟡시각화
기초설정
import matplotlib.pyplot as plt
표 그리기
plt.figure(figsize=(15,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

조건1. 최대 단어 100개만 나오게 한 후 시각화 하기
WordCloud(max_words)를 사용
wordcloud = WordCloud(max_words=100).generate(text)
plt.figure(figsize=(15,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

불용어:(워드 클라우드로 작성할 단어에서 제외할 단어)
기초설정
from wordcloud import STOPWORDS
STOPWORDS

불용어 추가하기
STOPWORDS.add()
STOPWORDS.add('said')
불용어를 제외한 워드 클라우드 시각화 하기
wordcloud = WordCloud(max_words=100,
font_path='/usr/share/fonts/truetype/nanum/NanumPen.ttf'
,stopwords=STOPWORDS).generate(text)
plt.figure(figsize=(15,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

워드클라우드 시각화에 마스크 씌우기
기초 설정
from PIL import Image
import numpy as np
alice_mask = np.array(Image.open('/content/drive/MyDrive/KDT-1/데이터분석/alice_mask.png'))
alice_mask
마스크 모양
시각화 하기
wordcloud = WordCloud(max_words=100,
font_path='/usr/share/fonts/truetype/nanum/NanumPen.ttf'
,stopwords=STOPWORDS
,mask=alice_mask
,background_color='pink').generate(text)
plt.figure(figsize=(15,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

728x90
'파이썬' 카테고리의 다른 글
라이브러리 알아보기 (0) | 2023.10.17 |
---|---|
Python(Colab) folium (0) | 2023.06.09 |
Python(Colab) 형태소 분석 KoNLPy (0) | 2023.06.09 |
Python(Colab) 데이터프레임 시각화 Matplotlib 라이브러리 (0) | 2023.06.09 |
Python(Colab) 데이터프레임 select_dtypes,원 핫 인코딩 (0) | 2023.06.08 |