[AI] 시각화 연습_1

황동규·2023년 6월 29일
2

AI

목록 보기
1/6
post-thumbnail

오늘은 AI를 공부해보면서 Seaborn을 이용한 시각화 연습한 것 중 Relational 그래프를 다뤄보려한다.

Seaborn이란?

Seaborn은 파이썬 데이터 시각화 라이브러리이고, Matplotlib을 기반으로 한 통계 그래픽스 패키지이다.

Relational 그래프

두 가지 변수의 관계를 나타내는 그래프이다.

  • scatterplot : 산점도
  • lineplot : 라인
  • relplot : scatterplot와 lineplot을 합친 그래프

모듈 및 데이터 셋

모듈

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

데이터 셋

#tips 데이터
tips = sns.load_dataset("tips")
tips
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
... ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

244 rows × 7 columns

# fmri 데이터 셋
fmri = sns.load_dataset("fmri")
fmri
subject timepoint event region signal
0 s13 18 stim parietal -0.017552
1 s5 14 stim parietal -0.080883
2 s12 18 stim parietal -0.081033
3 s11 18 stim parietal -0.046134
4 s10 18 stim parietal -0.037970
... ... ... ... ... ...
1059 s0 8 cue frontal 0.018165
1060 s13 7 cue frontal -0.029130
1061 s12 7 cue frontal -0.004939
1062 s11 7 cue frontal -0.025367
1063 s0 0 cue parietal -0.006899

1064 rows × 5 columns

Relational 그래프 정리

scatterplot

scatterplot은 산점도를 나타내는 그래프이다.

#scatterplot : 산점도 scatter(x, y, data)
sns.scatterplot(x='total_bill', y='tip', data=tips)
<Axes: xlabel='total_bill', ylabel='tip'>

#hue : 의미에 따라 점의 색을 변경
#style: 모양 구분
sns.scatterplot(x='total_bill', y= 'tip', data = tips, hue = 'day', style = 'time')
<Axes: xlabel='total_bill', ylabel='tip'>

lineplot

lineplot은 line을 나타내는 그래프이며, 데이터가 연속적일 경우 주로 사용한다. lineplot(x,y,data)

sns.lineplot(x='timepoint', y='signal', data=fmri)
<Axes: xlabel='timepoint', ylabel='signal'>

# 위의 그래프에서 색이 칠해져 있는 부분은 신뢰구간(confidene interval)로 ci 파라미터로 조절 가능
# hue와 style 옵션을 사용할 수 있음

sns.lineplot(x='timepoint',y='signal',data=fmri,hue='event',style='event',ci=None)
<Axes: xlabel='timepoint', ylabel='signal'>

relplot

relplot은 scatterplot과 lineplot을 합쳐 놓은 그래프이다.

  • kind 파라미터에 scatter나 line으로 형식 선택 가능 (default=scatter)
  • scatterplot과 lineplot은 AxeSubplot을 반환하지만, relplot은 FaceGrid를 반환
  • FaceGrid를 반환하는 경우 여러 그래프를 한 번에 그릴수 있음
  • hue와 style 옵션 모두 사용 가능

#scatter
sns.relplot(x='total_bill',y='tip',kind='scatter',hue ='time',data=tips)
<seaborn.axisgrid.FacetGrid at 0x164067f70>

#line
sns.relplot(x='timepoint',y='signal',kind='line',hue ='event', style ='event',ci = None,data=fmri)
<seaborn.axisgrid.FacetGrid at 0x16337f880>

느낀점

AI를 입문한지 얼마 되지 않아 Seaborn과 pandas를 다루는게 미숙하고 어렵지만 계속 공부하고 사용해보면서 익숙해져봐야겠다. Seaborn을 통해 데이터 셋을 한 후 Relational 그래프를 통해 시각화 한 것을 보니 좀 신기하고 흥미로웠다. 더 열심히 공부해봐야겠다.

profile
develop_blog

2개의 댓글

comment-user-thumbnail
2023년 7월 2일

최고에용!🫵👍👎👍👍

답글 달기
comment-user-thumbnail
2023년 9월 21일

🥰❤️

답글 달기

관련 채용 정보