07wk-2: Numpy 활용 (2)

Author

최규빈

Published

April 19, 2023

강의영상

youtube: https://youtube.com/playlist?list=PLQqh36zP38-xYcEpzHG1vC5hYDpgE5V5Z

imports

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

예비학습: matplotlib

범례추가

- 예시1

y1= [1.0, 2.0, 3.0, 4.0]
y2= [1.1, 2.2, 2.8, 0.0]
plt.plot(y1,'--o',label='y1')
plt.plot(y2,'--o',label='y2')
plt.legend()
<matplotlib.legend.Legend at 0x7fd0c8ed6e80>

- 예시2

y1= [1.0, 2.0, 3.0, 4.0]
y2= [1.1, 2.2, 2.8, 0.0]
plt.plot(y1,'--o',label=r'$y_1$')
plt.plot(y2,'--o',label=r'$y_2$')
plt.legend()
<matplotlib.legend.Legend at 0x7fd0c8fcc2e0>

넘파이 활용 (2단계)

변환을 의미하는 행렬

1-3. 아래는 우산모양의 scatter plot을 그릴 수 있는 (xi,yi) 좌표값을 불러와서 시각화하는 코드이다.

# 자료저장 
data_raw = np.array(pd.read_csv('https://raw.githubusercontent.com/guebin/2021IR/master/_notebooks/round2.csv')).T
data_raw
array([[ 12,  12,  13, ..., 636, 636, 637],
       [313, 314, 279, ..., 416, 417, 409]])
# 저장된 자료를 x,y로 분해 
x_raw,y_raw = data_raw
x_raw,y_raw
(array([ 12,  12,  13, ..., 636, 636, 637]),
 array([313, 314, 279, ..., 416, 417, 409]))
# 시각화
plt.figure(figsize=(6,6)) # 그림크기고정
plt.plot(x_raw,y_raw,'o')

1. x_rawy_raw를 각각 표준화하고 x, y에 저장하라. 결과를 시각화하라. 그리고 x, y를 바탕으로 data matrix를 다시 구성하라.

(풀이)

표준화

x= (x_raw-np.mean(x_raw)) / np.std(x_raw,ddof=1)
y= (y_raw-np.mean(y_raw)) / np.std(y_raw,ddof=1)

시각화

plt.figure(figsize=(6,6)) # 그림크기고정
plt.plot(x,y,'o')

data matrix 재구성

data = np.array([x,y])
data
array([[-1.79820085, -1.79820085, -1.79255336, ...,  1.72583347,
         1.72583347,  1.73148096],
       [-0.18390658, -0.17703046, -0.41769466, ...,  0.52433376,
         0.53120988,  0.47620092]])

2. 아래와 같은 규칙에 따라 (xi,yi)(x~i,y~i)로 변환하라. 단 여기에서 θ=π4로 가정한다.

  • xxi=x~i=(cosθ)xi(sinθ)yi
  • yyi=y~i=(sinθ)xi+(cosθ)yi

(xi,yi)(x~i,y~i)를 겹쳐서 시각화 하라. 이 변환이 의미하는 바는 무엇이라고 생각하는가?

(풀이1)

theta = np.pi/4 
xx = np.cos(theta)*x - np.sin(theta)*y
yy = np.sin(theta)*x + np.cos(theta)*y
plt.figure(figsize=(6,6))
plt.plot(x,y,'o', label=r'$(x,y)$') 
plt.plot(xx,yy,'o', label=r'$(xx,yy)$')
plt.legend()
<matplotlib.legend.Legend at 0x7f79adb12490>

(풀이2)

아래의 규칙을 다시 고려하자.

  • xxi=x~i=(cosθ)xi(sinθ)yi
  • yyi=y~i=(sinθ)xi+(cosθ)yi

이는 아래와 같이 재표현가능하다.

  • {xx1=(cosθ)x1(sinθ)y1yy1=(sinθ)x1+(cosθ)y1

  • {xx2=(cosθ)x2(sinθ)y2yy2=(sinθ)x2+(cosθ)y2

  • {xxn=(cosθ)xn(sinθ)y1yyn=(sinθ)xn+(cosθ)yn

이는 다시 아래와 같이 재 표현가능하다.

  • [xx1yy1]=[cosθsinθsinθcosθ][x1y1]

  • [xx2yy2]=[cosθsinθsinθcosθ][x2y2]

  • [xxnyyn]=[cosθsinθsinθcosθ][xnyn]

이는 다시 아래와 같이 재 표현가능하다. (이게 처음엔 생각하기 어려워요!! )

  • [xx1xx2xxnyy1yy2yyn]=[cosθsinθsinθcosθ][x1x2xny1y2yn]

위의 수식에 맞추어 변환을 진행하면

theta = np.pi/4 
R = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta),np.cos(theta)]])
R 
array([[ 0.70710678, -0.70710678],
       [ 0.70710678,  0.70710678]])
R @ data
array([[-1.14147842, -1.14634057, -0.97217191, ...,  0.8495886 ,
         0.84472644,  0.88761703],
       [-1.40156161, -1.39669946, -1.56288136, ...,  1.59110851,
         1.59597066,  1.56106683]])
xx,yy = R @ data 
plt.figure(figsize=(6,6))
plt.plot(x,y,'o', label=r'$(x,y)$') 
plt.plot(xx,yy,'o', label=r'$(xx,yy)$')
plt.legend()
<matplotlib.legend.Legend at 0x7f79b231b940>

3. 2번과 같은 변환을 8번 연속진행한 결과를 시각화하라.

(풀이)

plt.figure(figsize=(6,6))
xx,yy=np.linalg.matrix_power(R,8) @ data
plt.plot(x,y,'o',label=r'$data=(x,y)$') 
plt.plot(xx,yy,'o',label=r'$R^8@data=(xx,yy)$')
plt.legend()
<matplotlib.legend.Legend at 0x7f79b2198220>

4. 2번의 변환을 역변환하는 코드를 구현하라. 원본, 변환, 역변환한 자료를 순서대로 시각화 하라. 결과를 아래와 같이 저장하라.

  • 원본: x,y
  • 2번변환: xx,yy
  • 2번의역변환 xxx,yyy

(풀이)

xx,yy = R@data 
xxx,yyy = np.linalg.inv(R)@data
plt.figure(figsize=(6,6))
plt.plot(x,y,'.', label=r'$data = (x,y)$') 
plt.plot(xx,yy,'.', label=r'$R@data = (xx,yy)$')
plt.plot(xxx,yyy,'.', label=r'$R^{-1}@data = (xxx,yyy)$')
plt.legend()
<matplotlib.legend.Legend at 0x7f79b208d3d0>

5-8. 시계열 자료와 이동평균

5. 아래의 수식에 따르는 수열 xt를 생성하라. 단, T=1000 으로 설정하고 랜덤시드는 np.random.seed(2)와 같이 설정한다.

  • x1=ϵ1
  • x2=ϵ1+ϵ2
  • x3=ϵ1+ϵ2+ϵ3
  • xT=t=1Tϵt

생성결과를 시각화하라.

hint 아래의 코드를 활용하면 쉽다.

np.cumsum([1,2,3,4])
array([ 1,  3,  6, 10])

(풀이)

T = 1000
np.random.seed(2)
epsilon = np.random.randn(1000)
x = epsilon.cumsum()
plt.plot(x)

6. 아래와 같은 변환을 수행하고 결과를 시각화 하라.

  • xx1=13(x1+x2)
  • xx2=13(x1+x2+x3)
  • xx3=13(x2+x3+x4)
  • xxT1=13(xT1+xT1+xT)
  • xxT=13(xT1+xT)

hint 아래의 수식을 이용하라.

  • [xx1xx2xx3xxT1xxT]=[1/31/300000001/31/31/300000001/31/31/3000000000001/31/31/300000001/31/3][x1x2x3xT1xT]

(풀이)

M = np.array([[abs(i-j)<2 for i in range(T)] for j in range(T)])*1/3
M
array([[0.33333333, 0.33333333, 0.        , ..., 0.        , 0.        ,
        0.        ],
       [0.33333333, 0.33333333, 0.33333333, ..., 0.        , 0.        ,
        0.        ],
       [0.        , 0.33333333, 0.33333333, ..., 0.        , 0.        ,
        0.        ],
       ...,
       [0.        , 0.        , 0.        , ..., 0.33333333, 0.33333333,
        0.        ],
       [0.        , 0.        , 0.        , ..., 0.33333333, 0.33333333,
        0.33333333],
       [0.        , 0.        , 0.        , ..., 0.        , 0.33333333,
        0.33333333]])
plt.plot(x,label=r'$x$')
plt.plot(M@x,'--',label=r'$M@x$')
plt.legend()
<matplotlib.legend.Legend at 0x7fd9917e2670>

  • 끝이 올라간 이유? 마지막에 13(xT1+xT+xT+1) 대신에 13(xT1+xT+0) 을 이용하였기 때문

7. 아래와 같은 변환을 수행하고 결과를 시각화하라.

  • xx1=13(x1+x1+x2)
  • xx2=13(x1+x2+x3)
  • xx3=13(x2+x3+x4)
  • xxT1=13(xT1+xT1+xT)
  • xxT=13(xT1+xT+xT)

(풀이)

M[0,0] = 2/3 
M[-1,-1] = 2/3 
plt.plot(x,label=r'$x$')
plt.plot(M@x,label=r'$M@x$')
plt.legend()
<matplotlib.legend.Legend at 0x7fd96893a190>

8. 7번의 변환을 100번 반복하고 시각화하라.

(풀이)

plt.plot(x,label=r'$x$')
plt.plot(np.linalg.matrix_power(M,100)@x,label=r'$M^{100}@x$')
plt.legend()
<matplotlib.legend.Legend at 0x7fd9687943d0>

9. 7번의 변환을 1000번 반복하고 시각화하라.

plt.plot(x,label=r'$x$')
plt.plot(np.linalg.matrix_power(M,1000)@x,label=r'$M^{1000}@x$')
plt.legend()
<matplotlib.legend.Legend at 0x7fd96862aac0>

HW

없음