[TIL]KDT_20230421
๐ KDT WEEK 3 DAY 5 TIL
- Seaborn
- WordCloud
๐ฅ Seaborn
ํ์ด์ฌ์ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ, matplotlib ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ค.
Lineplot๊ณผ Barplot์ ๊ตฌํ์๋ ๋ค์์ด ํ์ํ๋ค.
๋ณดํต sns๋ก ๋ง์ด ์ค์ธ๋ค๊ณ ํ๋ค.
Lineplot
Barplot
๊ทธ๋ฆฌ๊ณ ,
์ ์ถ๊ฐํ๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ๋ผ๋ฒจ๋ ๋ถ์ผ ์ ์๋ค.
๊ณต๊ณต์ฌ์ดํธ ๋ฐ์ดํฐ๋ก ์๊ฐํํด๋ณด๊ธฐ
์ฌ์ดํธ : https://www.weather.go.kr/w/weather/forecast/short-term.do
๋จ๊ธฐ์๋ณด - ๊ธฐ์์ฒญ ๋ ์จ๋๋ฆฌ
2023๋ 04์ 21์ผ (๊ธ)์์ผ 11:00 ๋ฐํ (์ด๊ด์๋ณด๊ด: ์ด์์ฐ) โก (์ข ํฉ) ์ค๋ ์คํ~์ ๋ ์ ๋จ๊ถ ๋น ์กฐ๊ธ, ์ค๋๊ณผ ๋ด์ผ ํด์ ์ค์ฌ ๊ฐํ ์ ์โ (์ค๋) ์ ๊ตญ ๊ฐ๋ ๊ตฌ๋ฆ๋ง๊ฒ ์ผ๋, ์ ๋ผ๊ถ ์คํ์ ๊ตฌ๋ฆ๋ง์,
www.weather.go.kr
์ญ์ ํญ์ ํด์๋ ๊ฒ์ฒ๋ผ, ์ฌ์ดํธ์์ ํ์ํ ์ ๋ณด๋ฅผ ๊ฒ์ฌํด์.. ์ด๋ค ํ๊ทธ๊ฐ ํ์ํ์ง ์กฐ์ฌํ๋ค.
์ ๋ฒ์ ๋ช ๋ฒํด๋ดค์ผ๋, ๋ฐ๋ก ์ฝ๋๋ก ์์ฑํด๋ณด์๋ค.
from selenium import webdriver
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import seaborn as sns
import matplotlib.pyplot as plt
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.weather.go.kr/w/weather/forecast/short-term.do")
driver.implicitly_wait(2)
# ๋ ์ง ๋ณ ๊ธฐ์จ์ ๊ฐ์ ธ์จ๋ค
temps = driver.find_element(By.ID, "my-tchart").text
# ํ์์๋ ๋ถ๋ถ์ ๋ผ์ด๋ด๊ณ , intํ ๋ฐฐ์ด๋ก ์ ์ฅํด์ค๋ค.
tList = [int(i) for i in temps.replace("โ","").split("\n")]
plt.ylim(min(tList) - 2, max(tList) + 2)
plt.title("Expected Temperature from now on")
# tList๋ฅผ ์๊น ๋ฐฐ์ ๋ lineplot์ผ๋ก ํํ!
sns.lineplot(
x = [i for i in range(len(tList))],
y = tList
)
plt.show()
์ฌ๊ธฐ์ id = "my-tchart" ์ธ ํ๊ทธ๋ฅผ ๊ฐ์ ธ์์ text๋ฅผ ๋ชจ๋ ์ฐ์ด๋์์๋ ์จ๋๋ง ๊ตฌํ ์ ์์๋๋ฐ,
์ด๊ฑด ์ฌ์ดํธ์์ ๋ณธ ํ๊ทธ์ ์์ฑํ ๋ด์ฉ์ด ๊ทธ๊ฒ ๋ฟ์ด๋ผ ๊ฐ๋ฅํ ๊ฒ์์ผ๋ก, ๋ค๋ฅธ ๊ฐ๋ฐ์์๋ ์กฐ์ฌํด์ผ ํ๋ค!
์ ์ฐํ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค!
ํ๋ก๊ทธ๋๋จธ์ค ํ๊ทธ ๋น๋์ ์๊ฐํํ๊ธฐ
ํ์ฉํ ์ฌ์ดํธ ์ฃผ์ : https://qna.programmers.co.kr/
QnA | ํ๋ก๊ทธ๋๋จธ์ค ์ปค๋ฎค๋ํฐ
ํ๋ก๊ทธ๋๋จธ์ค QnA๋ ํ๋ก๊ทธ๋๋ฐ ๋ฌธ์ ํด๊ฒฐ์ ์ํ QnA์๋น์ค์ ๋๋ค. ํ๋ก๊ทธ๋๋ฐ๊ณผ ๊ด๋ จํด์ ๊ฐ๋ฐ์๋ค๋ผ๋ฆฌ ๊ถ๊ธํ๊ฑด ๋ฌผ์ด๋ณด๊ณ ์๋๊ฑด ํจ๊ป ๋๋ ์. C, Java, Python, Ruby๋ฑ์ ์ฝ๋๋ฅผ ์น์์ ์ง์ ์คํ
qna.programmers.co.kr
์ด๋ฒ์๋ ์ฌ์ดํธ ์์ ์ง๋ฌธ ํ๊ทธ๋ฅผ ๋ชจ์ ๋น๋์๋ฅผ ์๊ฐํํ๋ ์ค์ต์ ํด๋ณด์๋ค.
from bs4 import BeautifulSoup
import requests
import time
user_agent = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
frequency = {} # "ํ๊ทธ์ด๋ฆ" : "๋น๋์" ๋ก ํ์ฉ
# 1 ~ 10ํ์ด์ง์์์ ํ์
for i in range(1,11):
res = requests.get("https://qna.programmers.co.kr/?page={}".format(i), user_agent)
soup = BeautifulSoup(res.text, "html.parser")
ul_tags = soup.find_all("ul", "question-tags")
for ul in ul_tags:
li_tags = ul.find_all("li")
for li in li_tags:
tag = li.text.replace("\n","")
# frequency์ ํ๊ทธ ํค๊ฐ ์กด์ฌํ์ง์์ผ๋ฉด 1, ์กด์ฌํ๋ฉด ๊ทธ ์ซ์์ + 1
frequency[tag] = frequency.get(tag,0) + 1
time.sleep(0.5)
print(frequency)
์ง๋ฌธ ํ๊ทธ๊ฐ ul์ question-tags๋ก ์กด์ฌํ๋ค๋ ๊ฑธ ํ์ ํ๊ณ , frequency์ ์ ์ฅํ๋ ์ฝ๋์ด๋ค.
์ฝ๋์ ์ดํด์ ๋ช ๊ฐ์ง ํ์ํ ๋ด์ฉ์ ์ ์ด๋ณด๋ฉด,
- ul > li > text โก ํ๊ทธ
- tag์์ \n ์ผ๋ก ์ฒ๋ฆฌ๋ ๋ฌธ๊ตฌ๊ฐ ๋ง์ replace๋ก ์์ ๋๊ณ , ํ์ํ ๋ถ๋ถ๋ง ๋จ๊ฒจ๋์๋ค.
- ์ฌ์ดํธ ๊ณผ๋ถํ ๋ฐฉ์ง๋ฅผ ์ํด time.sleep์ ์ ์ฉํ์๋ค.
๊ฒฐ๊ณผ๋..
์ด ์ค์์ ๋น๋ ์๋ณ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ ๋ฆฌํ๊ณ ๋ง๋๊ทธ๋ํ๋ก ๋ง๋ค์ด๋ณด์๋ค.
# ์ ์ฝ๋์์ ์ด์ด์ง๋ค.
from collections import Counter
import seaborn as sns
import matplotlib.pyplot as plt
counter = Counter(frequency)
commonList = counter.most_common(10)
# commonList๊ฐ (์ด๋ฆ, ๋น๋์)์ ์ด์ค ๋ฐฐ์ด ํํ์ด๋ฏ๋ก x, y๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋๋๋ค
x = [e[0] for e in commonList]
y = [e[1] for e in commonList]
plt.figure(figsize=(15,5))
plt.title("Frequency of question in Hashcode")
plt.xlabel("Tag")
plt.ylabel("Frequency")
sns.barplot(x=x, y=y)
plt.show()
collections์ Counter๋ฅผ ์ฌ์ฉํ๋ฉด value์ ์ซ์๊ฐ ๋์ ๊ฒ๋ถํฐ ์ฐจ๋ก๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
commonList = counter.most_common(10)์ ๊ฐ์ฅ ๋์ ๊ฒ๋ถํฐ ์ฐจ๋ก๋ก 10๊ฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ค.
figure๋ก ํฌ๊ธฐ ์กฐ์ ์ด ์์ผ๋ฉด, x์ ์ด๋ฆ์ด ์๋ก ๊ฒน์ณ๋ณด์ธ๋ค.
๐ฆ Wordcloud
ํ์ด์ฌ์ ํ ์คํธ ํด๋ผ์ฐ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, ํ ์คํธ ๊ตฌ๋ฆ์ ๊ทธ๋ฆด ์ ์๋ค
์งํํ๊ธฐ์ ์ pip install wordcloud, pip install konlpy, java๊ฐ ํ์ํ๋ค.
์๋์ฐ์ ๊ฒฝ์ฐ ์ค์นํ JAVA_HOME์ Path ๊ฒฝ๋ก๋ ์ถ๊ฐ๋ก ์ง์ ํด์ค์ผ wordcloud๊ฐ ๋์ํ๋ค.
์ฐธ๊ณ ํ๋ ๋ธ๋ก๊ทธ : https://default-womyn.tistory.com/entry/konlpy-Okt-%EC%98%A4%EB%A5%98-No-JVM-shared-library-file-jvmdll-found
WordCloud๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์, ๋ค์๊ณผ ๊ฐ์ ์์๋ก ์งํํ๋ค
- KoNLPy ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ํ๊ตญ์ด ๋ฌธ์ฅ์ ์ ์ฒ๋ฆฌ
- Counter๋ฅผ ์ด์ฉํด ๋น๋์ ์ธก์
- WordCloud๋ฅผ ์ด์ฉํด ์๊ฐํ
WordCloud๋ฅผ ์งํํ๊ธฐ ์ํ ์์..
# ์๊ฐํ์ ์ฐ์ด๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# ํ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์
๋๋ฆฌ ์์ฑ
from collections import Counter
# ๋ฌธ์ฅ์์ ๋ช
์ฌ๋ฅผ ์ถ์ถํ๋ ํํ์ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
from konlpy.tag import Hannanum
#WordCloud์ ์ฌ์ฉํ ์ ๊ตญ๊ฐ ๊ฐ์ฌ
national_anthem = """
๋ํด๋ฌผ๊ณผ ๋ฐฑ๋์ฐ์ด ๋ง๋ฅด๊ณ ๋ณ๋๋ก
ํ๋๋์ด ๋ณด์ฐํ์ฌ ์ฐ๋ฆฌ๋๋ผ ๋ง์ธ
๋ฌด๊ถํ ์ผ์ฒ๋ฆฌ ํ๋ ค ๊ฐ์ฐ
๋ํ ์ฌ๋ ๋ํ์ผ๋ก ๊ธธ์ด ๋ณด์ ํ์ธ
๋จ์ฐ ์์ ์ ์๋๋ฌด ์ฒ ๊ฐ์ ๋๋ฅธ ๋ฏ
๋ฐ๋ ์๋ฆฌ ๋ถ๋ณํจ์ ์ฐ๋ฆฌ ๊ธฐ์์ผ์ธ
๋ฌด๊ถํ ์ผ์ฒ๋ฆฌ ํ๋ ค ๊ฐ์ฐ
๋ํ ์ฌ๋ ๋ํ์ผ๋ก ๊ธธ์ด ๋ณด์ ํ์ธ
๊ฐ์ ํ๋ ๊ณตํํ๋ฐ ๋๊ณ ๊ตฌ๋ฆ ์์ด
๋ฐ์ ๋ฌ์ ์ฐ๋ฆฌ ๊ฐ์ด ์ผํธ๋จ์ฌ์ผ์ธ
๋ฌด๊ถํ ์ผ์ฒ๋ฆฌ ํ๋ ค ๊ฐ์ฐ
๋ํ ์ฌ๋ ๋ํ์ผ๋ก ๊ธธ์ด ๋ณด์ ํ์ธ
์ด ๊ธฐ์๊ณผ ์ด ๋ง์ผ๋ก ์ถฉ์ฑ์ ๋คํ์ฌ
๊ดด๋ก์ฐ๋ ์ฆ๊ฑฐ์ฐ๋ ๋๋ผ ์ฌ๋ํ์ธ
๋ฌด๊ถํ ์ผ์ฒ๋ฆฌ ํ๋ ค ๊ฐ์ฐ
๋ํ ์ฌ๋ ๋ํ์ผ๋ก ๊ธธ์ด ๋ณด์ ํ์ธ
"""
์ ๊ตญ๊ฐ ๊ฐ์ฌ์์์ ํํ์ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ Hannanum์ ํตํด ๋ช ์ฌ๋ฅผ ์ถ์ถํ๋ ์ฝ๋์ด๋ค.
hannanum = Hannanum()
nouns = hannanum.nouns(national_anthem)
words = [noun for noun in nouns if len(noun) > 1] #์ด, ํ, ๋ฏ ๋ฑ ํ ๊ธ์ ๋จ์ด๋ ์ ์ธ
์ด๋ ๊ฒํ๋ฉด words์ ๋ฐฐ์ดํํ๋ก ๋ํด๋ฌผ, ๋ฐฑ๋์ฐ, ํ๋๋๊ฐ์ ๋จ์ด๋ค์ด ๋ด๊ธฐ๊ฒ ๋๋ค.
์ฌ๊ธฐ์ counter๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ๋จ์ด์ ๊ฐ์๋ฅผ ์ธ์ด๋๊ณ , wordCloud๋ก ํํํ๋ค!
import matplotlib as mpl
from matplotlib import font_manager, rc
counter = Counter(words) # ๋จ์ด ์ ์ธ๊ธฐ
font_path = r"C:\Users\asd\Desktop\malgun.ttf" # ํ๊ธ ๊นจ์ง ๋ฐฉ์ง๋ฅผ ์ํ ํฐํธ ์ถ๊ฐ
wordcolud = WordCloud(font_path= font_path) # ํฐํธ ๊ฒฝ๋ก๋ฅผ ๋ฃ๊ณ wordCloud ๋์!
img = wordcolud.generate_from_frequencies(counter)
plt.imshow(img)
๋ณธ๋ ํ๊ธ์ด ๋์ค์ง ์์ผ๋ ํฐํธ๋ฅผ ์ถ๊ฐํ์ฌ ์ถ๋ ฅํ๋ฉด..
๋ฐฐ๊ฒฝํ๋ฉด ์๊น๋ WordCloud ๋ฉ์๋์ background ์ธ์๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฐ๊ฟ ์ ์๋ค.
+ ํ๋ก๊ทธ๋๋จธ์ค ์ง๋ฌธ์ ๋จ์ด ๋น๋์๋ก WordColud ๋ง๋ค๊ธฐ
import time
import requests
from bs4 import BeautifulSoup
#์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from collections import Counter
from konlpy.tag import Hannanum
user_agent = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
questions = [] # ์ง๋ฌธ๋ค์ด ๋ด๊ธธ ๋ฐฐ์ด
# 1~5ํ์ด์ง ํ์
for i in range(1, 6):
res = requests.get("https://hashcode.co.kr/?page={}".format(i), {"User-Agent": user_agent})
soup = BeautifulSoup(res.text, "html.parser")
parsed_datas = soup.find_all("li", "question-list-item")
for data in parsed_datas:
questions.append(data.h4.text.strip())
time.sleep(0.5)
# ์์์ ๋ง๋ questions ๋ฐฐ์ด์ ์ด์ฉํ์ฌ WordCloud ๋ง๋ค๊ธฐ
words = []
hannanum = Hannanum()
# questions ๋ฐฐ์ด์์ ๋ชจ๋ ๋จ์ด๋ค์ words ๋ฐฐ์ด์ ์ถ๊ฐ
for question in questions:
nouns = hannanum.nouns(question)
words += nouns
counter = Counter(words) # ๋จ์ด์ด๋ฆ : ๊ฐ์ ๋ง๋ค๊ธฐ
wordcloud = WordCloud(
font_path= r"C:/Users/asd/Desktop/malgun.ttf",
background_color="white"
)
img = wordcloud.generate_from_frequencies(counter)
plt.imshow(img)
๊ฒฐ๊ณผ

๐ค ๊ณต๋ถํ๋ฉด์ ์ด๋ ค์ ๋ ๋ด์ฉ
์ฌ๋ฌ ๊ตฐ๋ฐ์์ ๋ถ๋ช์ณค๋ค..
์๋์ฐ๋ผ์ ๊ทธ๋ฐ์ง Path ์ค์ ์๋ ๊ผฌ์ด๊ณ , ํ๊ธ ์ถ๋ ฅ์๋ ๊ผฌ์ด๊ณ ..
๊ฐ์ ๋ฃ๋ ์๊ฐ๋ณด๋ค ์ด๊ฑธ ํด๊ฒฐํ๋๋ฐ ์๊ฐ์ ๋ ๋ง์ด ์ด๊ฒ๊ฐ๋คใ ใ ใ
๊ฒฐ๊ตญ ๋๋ค ํด๊ฒฐ๋ฒ์ ๊ฐ๋จํ๋ค.. ๋ด๊ฐ ์๊พธ ํค๋ฉ์์ ๋ฟใ
๊ทธ๋๋ ์๋ฏธ์๋ ์งํ๋ฅผ ๋ง๋ค ์ ์๋ ์๊ฐ์ด๋ผ ์ฌ๋ฐ์๋ค! ๐คญ