(A2) 데이터시각화 특강 중간고사 대비문제
힌트
- 랜덤시드 생성: np.random.seed()를 이용
np.random.seed(201855393)
y1 = np.random.normal(loc=0, scale=1, size = 10000)
y2 = np.random.normal(loc=1, scale=1, size = 10000)
A = pd.DataFrame({'score':y1, 'class':['A']*len(y1)})
B = pd.DataFrame({'score':y2, 'class':['B']*len(y2)})
df = pd.concat([A,B], ignore_index=True)
sns.histplot(df,x='score', hue='class')
자료
x=[1,2,3,4]
y=[1,2,4,3]
시각화예시
x=[1,2,3,4]
y=[1,2,4,3]
_, axs = plt.subplots(2,2)
axs[0,0].plot(x,y,'o:r')
axs[0,1].plot(x,y,'Xb')
axs[1,0].plot(x,y,'xm')
axs[1,1].plot(x,y,'.--k')
x = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]
y1 = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]
y2 = [9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]
y3 = [7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]
x4 = [8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]
y4 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]
fig, axs = plt.subplots(2,2)
axs[0,0].plot(x,y1,'o')
axs[0,0].set_title('(a)')
axs[0,1].plot(x,y2,'o')
axs[0,1].set_title('(b)')
axs[1,0].plot(x,y3,'o')
axs[1,0].set_title('(c)')
axs[1,1].plot(x4,y4,'o')
axs[1,1].set_title('(d)')
fig.tight_layout()
(하니) 그림 (a)는 양의 상관계수를 가진다.
(나애리) 그림 (b)는 산점도가 직선이 아니라 곡선의 모양을 띄고 있으므로 상관계수는 0이다.
(홍두깨) 그림 (c)에서 상단의 이상치를 제외하면 상관계수는 1이다.
(고은혜) 그림 (c)와 (d)의 상관계수를 각각 $r_1, r_2$라고 하자. 그리고 (c)와 (d)에서 각각 이상치를 제외한뒤 구한 상관계수를 $\tilde{r}_1, \tilde{r}_2$ 이라고 하자. 그렇다면 $r_1 r_2 < \tilde{r}_1\tilde{r}_2$ 가 성립한다.
-
하니
n=len(x)
xx1 = (x-np.mean(x)) / (np.std(x) * np.sqrt(n))
yy1 = (y1-np.mean(y1)) / (np.std(y1) * np.sqrt(n))
sum(xx1*yy1)
-
나애리
yy2 = (y2-np.mean(y2)) / (np.std(y2) * np.sqrt(n))
sum(xx1*yy2)
-
홍두깨
xo1 = [10, 8, 13, 9, 11, 14, 6, 4, 7, 5]
yo1 = [7.46, 6.77, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]
nn=len(xo1)
xxx1 = (xo1-np.mean(xo1)) / (np.std(xo1) * np.sqrt(nn))
yyy1 = (yo1-np.mean(yo1)) / (np.std(yo1) * np.sqrt(nn))
sum(xxx1*yyy1)
-
고은혜
yy3 = (y3-np.mean(y3)) / (np.std(y3) * np.sqrt(n))
yy4 = (y4-np.mean(y4)) / (np.std(y4) * np.sqrt(n))
sum(xx1*yy3)*sum(xx1*yy4)
xo2 = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
yo2 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 5.56, 7.91, 6.89]
xxx2 = 0
yyy2 = (yo2-np.mean(yo2)) / (np.std(yo2) * np.sqrt(nn))
sum(xxx1*yyy1)*sum(xxx2*yyy2)
x1=np.arange(0,10,0.1)
y1=x1+np.random.normal(loc=0,scale=1.0,size=len(x1))
plt.plot(x1,y1,'.')
(하니) 두 변수는 양의 상관관계에 있어 보인다.
(나애리) 완전한 직선 모양이 되지는 않았으므로 상관계수는 1보다 작다.
(홍두깨) 산점도를 파악하면 $x$가 증가할때 $y$도 증가하므로 $x$가 $y$를 유발하는 원인이라 해석할 수 있다.
(고은혜) $y$를 유지한채 $x$의 값을 모두 2배로 하면 상관계수의 값도 2배가 된다.
-
하니
n=len(x1)
xx1 = (x1-np.mean(x1)) / (np.std(x1) * np.sqrt(n))
yy1 = (y1-np.mean(y1)) / (np.std(y1) * np.sqrt(n))
sum(xx1*yy1)
-
나애리
sum(xx1*yy1)
-
고은혜
x2 = x1*2
xx2 = (x1-np.mean(x2)) / (np.std(x2) * np.sqrt(n))
sum(xx2*yy1)
df=pd.read_csv('https://raw.githubusercontent.com/PacktPublishing/Pandas-Cookbook/master/data/employee.csv')
df
df자료에는 결측치가 존재한다. 예를들면 아래에서 9번자료의 RACE는 아무것도 입력되어있지 않아 NaN으로 처리되었다.
df.iloc[8:11,:]
아래코드를 실행하여 결측치를 포함한 행을 제거하고 몇개의 결측치가 제거되었는지 답하라.
df=df.dropna()
df.isnull().sum()
df=df.dropna(axis=0)
df.isnull().sum()
pd.Series(df.columns)
df.groupby(by='GENDER').agg({'BASE_SALARY':'mean'})
시각화예시
tidy=df.groupby(['RACE','GENDER']).agg({'BASE_SALARY': [np.mean,max,min]}).stack().reset_index().rename(columns={'level_2':'aggtype'})
fig=ggplot(tidy)
bar=geom_bar(aes(x='aggtype',y='BASE_SALARY',fill='GENDER'),stat='identity',position='dodge')
fig+bar+coord_flip()+facet_wrap(facets="RACE")