0) Seaborn ๋ผ์ด๋ธ๋ฌ๋ฆฌ
matplotlib์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ์๊ฐํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
๋ค์ํ ๊ทธ๋ํ๋ฅผ ๊ณ ์์ค์์ ์ฝ๊ฒ ๊ทธ๋ฆด ์ ์๋ค.
1) ๊ทธ๋ํ์ ์ข ๋ฅ
1. figure-level vs axes-level
seaborn ํจ์๋ matplotlib ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ปค์คํฐ๋ง์ด์ง ํ๊ฑฐ๋ ์์ฒด ์ปค์คํฐ๋ง์ด์ง์ ์ํํ ์ ์๋ค.
์ด๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด์ figure-level๊ณผ axes-level์ ๋ํ ์ดํด๊ฐ ํ์ํ๋ค.
figure-level
ํด๋น ๋ ๋ฒจ์ ํจ์๋ seaborn ๊ฐ์ฒด๋ฅผ ํตํด์ matplotlib๊ณผ ์ํธ์์ฉ ํ๊ธฐ ๋๋ฌธ์ FacetGrid(seaborn์ figure)์ ์ด์ฉํด์ ๊ทธ๋ํ ์ปค์คํฐ๋ง์ด์ง์ ์ํํด์ผ ํ๋ค.
- replot
- displot
- catplot
- lmplot
axes-level
ํด๋น ๋ ๋ฒจ์ ํจ์๋ matplotlib.pyplot.Axes ๊ฐ์ฒด ์์ ๋ฐ์ดํฐ๋ฅผ ํ์ํ๊ธฐ ๋๋ฌธ์ matplotlib ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ์๋ก ์ปค์คํฐ๋ง์ด์ง์ ํ ์ ์๋ค.
- ์ด์ธ์ ๋๋จธ์ง
2. seaborn ๋ผ์ด๋ธ๋ก ํํํ ์ ์๋ ๊ทธ๋ํ ๋ชฉ๋ก
Relational plots
- scatterplot : ์ฌ๋ฌ ์ ์ผ๋ก ์ด๋ฃจ์ด์ง ์ฐ์ ๋
- lineplot : ํ๋์ ์ ์ผ๋ก ์ด๋ฃจ์ด์ง ๊บพ์์ ๊ทธ๋ํ
- replot : figure-level ํจ์๋ก ์์ ๋ ํจ์๋ฅผ ํํํ ์ ์์
- kind ์์ฑ์ ์ง์ ํ์ง ์์ผ๋ฉด scatterplot ํํ์ด๋ค.
- kind="line" ์์ฑ์ ์ง์ ํ๋ฉด lineplot ํํ์ด๋ค.
๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
Visualizing statistical relationships — seaborn 0.13.0 documentation
Visualizing statistical relationships Statistical analysis is a process of understanding how variables in a dataset relate to each other and how those relationships depend on other variables. Visualization can be a core component of this process because, w
seaborn.pydata.org
Distribution plots
- hisplot : ํ์คํ ๊ทธ๋จ ํํ
- kdeplot : ์ปค๋ ๋ฐ๋ ํจ์ ํํ
- ecdfplot : ๊ฒฝํ์ ๋์ ๋ถํฌ ํจ์ ํํ
- rugplot : ์ค์ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ์ ๋ถ์ผ๋ก ๋ณด๋ ํํ
- displot : figure-level ํจ์๋ก ์์ ๋ค์ฏ ํจ์๋ฅผ ํํํ ์ ์์
- ๊ธฐ๋ณธ ํจ์๋ histplot ์
- kind="kde" ์์ฑ์ ์ง์ ํ๋ฉด kdeplot ํํ์ด๋ค.
- kind="ecdf" ์์ฑ์ ์ง์ ํ๋ฉด ecdfplot ํํ์ด๋ค.
- rug=True ์์ฑ์ ์ง์ ํ๋ฉด rugplot ํํ์ด๋ค.
๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
Visualizing distributions of data — seaborn 0.13.0 documentation
Visualizing distributions of data An early step in any effort to analyze or model data should be to understand how the variables are distributed. Techniques for distribution visualization can provide quick answers to many important questions. What range do
seaborn.pydata.org
Categorical plots
- stripplot : ์นดํ ๊ณ ๋ฆฌ ๋ณ์์ ๋ํ y ๋ถํฌ
- swarmplot : stripplot์ ๊ฒน์น๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ฒฉ์ ์กฐ์ ํ์ฌ ํํ(์๊ฐ์ ์ผ๋ก ์์๋ณด๊ธฐ ์ฉ์ด)
- boxplot : ๋ฐ์คํ๋กฏ(์์น์ ์๋ฃ๋ฅผ ํํํ๋ ๊ทธ๋ํ)
- violinplot : ์ผ๋ณ๋, ์ฐ์ํ ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ์ค๋ช ํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๊ทธ๋ํ
- boxenplot : boxplot๊ณผ ์ ์ฌํ์ง๋ง, ๋ถํฌ์ ํํ๋ฅผ ๋ ์์ธํ๊ฒ ํํํ๋ ๊ทธ๋ํ
- pointplot : ํน์ ์ ์ฌ์ด์ ํํ์ ์ง์ ์ผ๋ก ํํํ๋ ๊ทธ๋ํ
- barplot : ๋ง๋๊ทธ๋ํ ํํ
- counterplot : ํ์คํ ๊ทธ๋จ๊ณผ ์ ์ฌํ ํํ๋ก ๊ฐ์๋ฅผ ํํ
- catplot : figure-level ํจ์๋ก ์์ ์ฌ๋ ํจ์๋ฅผ ํํํ ์ ์์
- plot ์์ ๋จ์ด๋ฅผ kind์์ฑ์ผ๋ก ์ง์ ํ๋ฉด ์ฌ๋ ๊ฐ์ง ํจ์๊ฐ ๋ชจ๋ ํํ๋๋ค. catplot(kind="๋จ์ด")
๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
Visualizing categorical data — seaborn 0.13.0 documentation
Visualizing categorical data In the relational plot tutorial we saw how to use different visual representations to show the relationship between multiple variables in a dataset. In the examples, we focused on cases where the main relationship was between t
seaborn.pydata.org
Regression plots
- regplot : ๋ฐ์ดํฐ์ ๋ถํฌ์ ํด๋น ๋ฐ์ดํฐ์ ํ๊ท ํจ์๋ฅผ ํํ
- residplot : ํ๊ท ํจ์์ ์์ฐจ ๊ทธ๋ํ
- lmplot : figure-level ํจ์๋ก ์์ regplot์ ํํํ ์ ์์
Matrix plots
- heapmap : ์ด๋ฏธ์ง ์์ ์ด๋ถํฌ ํํ์ ๊ทธ๋ํ๋ฅผ ํํ
- clustermap : ์ด๋ฏธ์ง ์์ ๊ตฐ์งํ๋ ํํ์ ๊ทธ๋ํ๋ฅผ ํํ
3. matplotlib ๊พธ๋ฏธ๊ธฐ ์์ฑ
matplotlib.pyplot์ ์ด์ฉํ๋ฉด ๊ทธ๋ํ์ ๋ค์ํ ์์๋ฅผ ๋ณ๊ฒฝ/์ถ๊ฐํ ์ ์๋ค.
๊พธ๋ฏธ๊ธฐ ์์ฑ์ ์ ์ฉํ ๋ค์๋ plt.show() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊พธ๋ฏธ๊ธฐ ์์ฑ์ด ๋ณด์ด๋๋ก ํ๋ค.
1. ์ ๋ชฉ ์ถ๊ฐ
- plt.title(์ ๋ชฉ)
2. ์ถ์ ์ค๋ช ์ถ๊ฐ
- plt.xlabel(X์ถ ๋ผ๋ฒจ๋ช )
- plt.ylabel(y์ถ ๋ผ๋ฒจ๋ช )
3. ๊ทธ๋ํ ์ถ์ ์๊ฐํ ํ๋ ๋ฒ์๋ฅผ ์ง์
- plt.xlim(์๊ฐํ ์์ x๊ฐ, ์๊ฐํ ๋ x๊ฐ)
- plt.ylim(์๊ฐํ ์์ y๊ฐ, ์๊ฐํ ๋ y๊ฐ)
4. ๊ทธ๋ํ์ ํฌ๊ธฐ
์ฐ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํ๊ณ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ๋๋ฌธ์ figure์ ์ง์ ํ๋ ํจ์๊ฐ plotํจ์ ์์ ์์นํด์ผ ํ๋ค.
- plt.figure(figsize=(x์ถ ์ฌ์ด์ฆ, y์ถ ์ฌ์ด์ฆ)
2) ์น ์คํฌ๋ํ ํ ์๊ฐํ(BeautifulSoup, Seaborn, WordCloud)
1. ๋ง๋๊ทธ๋ํ ํํ๋ก ์๊ฐํ
์๋์ ์ฝ๋๋ ํด์์ฝ๋ ์ฌ์ดํธ์ ์ด๋ ์ข ๋ฅ์ ์ง๋ฌธ์ด ๋ง์ด ์ฌ๋ผ์ค๋์ง์ ๋ํด ์๊ฐํํ๋ ์ฝ๋์ด๋ค.
QnA | ํ๋ก๊ทธ๋๋จธ์ค ์ปค๋ฎค๋ํฐ
ํ๋ก๊ทธ๋๋จธ์ค QnA๋ ํ๋ก๊ทธ๋๋ฐ ๋ฌธ์ ํด๊ฒฐ์ ์ํ QnA์๋น์ค์ ๋๋ค. ํ๋ก๊ทธ๋๋ฐ๊ณผ ๊ด๋ จํด์ ๊ฐ๋ฐ์๋ค๋ผ๋ฆฌ ๊ถ๊ธํ๊ฑด ๋ฌผ์ด๋ณด๊ณ ์๋๊ฑด ํจ๊ป ๋๋ ์. C, Java, Python, Ruby๋ฑ์ ์ฝ๋๋ฅผ ์น์์ ์ง์ ์คํ
qna.programmers.co.kr
๊ณผ์
1. ํ๊ทธ(์ด๋ ์ข ๋ฅ์ ์ง๋ฌธ)์ ์์น๋ฅผ ํ์ ํ๋ค.(.html)
<ul class="question-tags">
<li>
<span>์ฌ๊ธฐ!</span>
</li>
<li>
<span>์ฌ๊ธฐ!</span>
</li>
....
</ul>
2. ๊ฐ ํ์ด์ง๋ง๋ค BeautifulSoup๋ฅผ ์ด์ฉํด ์น ์คํฌ๋ํ์ ์งํํ๋ค.
3. ์คํฌ๋ํ ์ ๋ถํ๋ฅผ ์ค์ด๊ธฐ ์ํด ํ์ด๋จธ๋ฅผ ์ด์ฉํ๋ค.
4. 2๋ฒ์ ์ ๋ณด๋ฅผ dictionary ํํ๋ก ์ ์ฅํ๋ค.
5. Counter๋ก ๋ณํ : Counter๋ฅผ ์ฌ์ฉํด ๊ฐ์ฅ ๋น๋๊ฐ ๋์ value๋ค์ ์ถ์ถํ๋ค.
6. dictionary๋ฅผ seaborn์ ํตํด ์๊ฐํํ๋ค.
# ์ง๋ฌธ์ ๋น๋๋ฅผ ์ฒดํฌํ๋ dict๋ฅผ ๋ง๋ ํ, ๋น๋๋ฅผ ์๊ฐํํด ๋ณด์.
import time
## 4. 2๋ฒ์ ์ ๋ณด๋ฅผ dictionary ํํ๋ก ์ ์ฅ
frequency={}
import requests
from bs4 import BeautifulSoup
for i in range(10):
## 2. ๊ฐ ํ์ด์ง๋ง๋ค BeautifulSoup๋ฅผ ์ด์ฉํด ์น ์คํฌ๋ํ์ ์งํ
res=requests.get("https://hashcode.co.kr?page={}".format(i))
soup=BeautifulSoup(res.text, "html.parser")
## 1. ul->li์ text๋ง ์ถ์ถ
ul_tags=soup.findAll("ul", "question-tags")
for ul in ul_tags:
li_tags=ul.find_all("li")
for li in li_tags:
tag=li.text.strip()
frequency[tag]=frequency.get(tag, 0)+1
## 3. ์คํฌ๋ํ์ ๋ถํ๋ฅผ ์ค์ด๊ธฐ ์ํด ํ์ด๋จธ๋ฅผ ์ด์ฉ
time.sleep(0.5)
## 5. Counter๋ก ๋ณํ : Counter๋ฅผ ์ฌ์ฉํด ๊ฐ์ฅ ๋น๋๊ฐ ๋์ value๋ค์ ์ถ์ถ
from collections import Counter
counter=Counter(frequency)
counter.most_common(10)
## 6. dictionary๋ฅผ seaborn์ ํตํด ์๊ฐํ
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10))
plt.title("Frequency of question in Hashcode")
plt.xlabel("Tag")
plt.ylabel("Frequency")
x_list=[elem[0] for elem in counter.most_common(10)]
y_list=[elem[1] for elem in counter.most_common(10)]
sns.barplot(x=x_list, y=y_list)
plt.show()
๊ฒฐ๊ณผ
์๊ฐํ ๊ฒฐ๊ณผ python>c>java>c++>pandas... ์นดํ ๊ณ ๋ฆฌ ์์ผ๋ก ์ง๋ฌธ์ด ๋ง์ด ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
2. WordColud ํํ๋ก ์๊ฐํ
WordCloud๋?
์์ฃผ ๋ฑ์ฅํ๋ ํ ์คํธ๋ฅผ ์ค์๋๋ ์ธ๊ธฐ๋๋ฅผ ๊ณ ๋ คํด ์๊ฐ์ ์ผ๋ก ํํํ ๊ฒ์ผ๋ก
์์ฃผ ๋ฑ์ฅํ๋ ๋จ์ด์ผ์๋ก ํฌ๊ณ ๋๊ป๊ฒ ํ์๋๋ค.
wordCloud ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํ๋ค.
์์ฐ์ด์ฒ๋ฆฌ์์ ๋ง์ด ์ฐ์ด๋๋ฐ, ํ ๋ฌธ์ฅ์์ ๋จ์ด๋ฅผ ์ถ์ถํ์ฌ ์์ฐ์ด๋ถ์์ ์งํํ ๋ค ์๊ฐํํ๋ค.
์๋์ ์ฝ๋๋ ํด์์ฝ๋ ์ฌ์ดํธ์ ์ ๋ชฉ์ ์คํฌ๋ํ ํ ๋ค, ์ด๋ ๋จ์ด๊ฐ ๋ง์ด ๋์ค๋์ง์ ๋ํด ์๊ฐํํ๋ ์ฝ๋์ด๋ค.
๊ณผ์
1. ํ๊ทธ(์ด๋ ์ข ๋ฅ์ ์ง๋ฌธ)์ ์์น๋ฅผ ํ์ ํ๋ค.(.html)
<ul class="question-tags">
<li>
<div>...</div>
<div>...</div>
<div class=question>
<div class="top">
<h4>์ ๋ชฉ์ฌ๊ธฐ์๋ค.</h4>
</div>
</div>
</li>
<li>
<div>...</div>
<div>...</div>
<div class=question>
<div class="top">
<h4>์ ๋ชฉ์ฌ๊ธฐ์๋ค.</h4>
</div>
</div>
</li>
</ul>
2. ๊ฐ ํ์ด์ง๋ง๋ค BeautifulSoup๋ฅผ ์ด์ฉํด ์น ์คํฌ๋ํ์ ์งํํ๋ค.
3. ์คํฌ๋ํ ์ ๋ถํ๋ฅผ ์ค์ด๊ธฐ ์ํด ํ์ด๋จธ๋ฅผ ์ด์ฉํ๋ค.
4. ์ ๋ชฉ๋ค์ list ํํ๋ก ์ ์ฅํ๋ค.
5. list์ ๋ฌธ์ฅ๋ค์์ ๋จ์ด๋ฅผ ์ถ์ถํ์ฌ ์ ๋ชฉ๋ค์ ๋จ์ด๋ชฉ๋ก์ ์ป๋๋ค.
6. Counter๋ก ๋ณํ : ๋จ์ด๋ชฉ๋ก์ Counter๋ก ๋ณํํ๋ค.
7. dictionary๋ฅผ wordcloud์ ํตํด ์๊ฐํํ๋ค.
# ์ง๋ฌธ ๋ฆฌ์คํธ์ ์ ๋ชฉ์์ ๋จ์ด๋ฅผ ์ถ์ถํ์ฌ ์๊ฐํํ๊ธฐ
import time
import requests
from bs4 import BeautifulSoup
## 4. ์ ๋ชฉ๋ค์ list ํํ๋ก ์ ์ฅํ๋ค.
title=[]
## 2. ๊ฐ ํ์ด์ง๋ง๋ค BeautifulSoup๋ฅผ ์ด์ฉํด ์น ์คํฌ๋ํ์ ์งํํ๋ค.
for i in range(6):
res=requests.get("https://hashcode.co.kr?page={}".format(i), {"User-Agent":user_agent})
soup=BeautifulSoup(res.text, "html.parser")
## 1. li->data๋ฅผ ์ฐพ๋๋ค.
parsed_datas=soup.find_all("li", "question-list-item")
for data in parsed_datas:
title.append(data.h4.text.strip())
## 3. ์คํฌ๋ํ ์ ๋ถํ๋ฅผ ์ค์ด๊ธฐ ์ํด ํ์ด๋จธ๋ฅผ ์ด์ฉํ๋ค.
time.sleep(0.5)
##5. list์ ๋ฌธ์ฅ๋ค์์ ๋จ์ด๋ฅผ ์ถ์ถํ์ฌ ์ ๋ชฉ๋ค์ ๋จ์ด๋ชฉ๋ก์ ์ป๋๋ค.
# ๋ฌธ์ฅ์์ ๋ช
์ฌ๋ฅผ ์ถ์ถํ๋ ํํ์ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
from konlpy.tag import Hannanum
# Hannanum ๊ฐ์ฒด๋ฅผ ์์ฑ(ํํ์ ๋ถ์ ๊ฐ์ฒด)ํ ํ, .nouns()๋ฅผ ํตํด ๋ช
์ฌ๋ฅผ ์ถ์ถํฉ๋๋ค.
words=[]
hannanum = Hannanum()
for t in title:
nouns = hannanum.nouns(t)
words+=nouns
## 6. Counter๋ก ๋ณํ : ๋จ์ด๋ชฉ๋ก์ Counter๋ก ๋ณํํ๋ค.
from collections import Counter
counter=Counter(words)
## 7. dictionary๋ฅผ wordCloud๋ฅผ ์ด์ฉํด ์๊ฐํํ๋ค.
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# WordCloud๋ฅผ ์ด์ฉํด ํ
์คํธ ๊ตฌ๋ฆ์ ๋ง๋ค์ด๋ด
์๋ค.
wordcloud=WordCloud(font_path="C:\Windows\Fonts\malgun.ttf",
background_color="white", width=2000, height=2000)
img=wordcloud.generate_from_frequencies(counter)
plt.imshow(img)
๊ฒฐ๊ณผ
์๊ฐํ ๊ฒฐ๊ณผ ํ์ด์ฌ, ์ง๋ฌธ, ๋ฌธ์ , ์ค๋ฅ, ์ฝ๋ ๋ฑ์ ํค์๋ ๋ฑ์ด ์์ฃผ ๋น์ถ๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.