11.1 대한 컴퍼니의 영업1부에 근무하는 강이안씨는 초고속 인터넷 가입형 상품 "빠르다넷"의 가입영업을 하고 있다. 강이안씨의 영업은 성공적으로 이루어져서 매달 새로운 사용자가 꾸준히 가입하고 있으며 이를 보여주기 위해, 다음 표와 같은 회사의 신규 사용자 통계자료를 준비했다.
월 | 7월 | 8월 | 9월 | 10월 | 11월 | 12월 |
---|---|---|---|---|---|---|
신규 사용자 | 456 | 492 | 578 | 599 | 670 | 854 |
11.1.1 이 표를 바탕으로 다음과 같은 차트를 그려보아라.
import matplotlib.pyplot as plt
month = [7, 8, 9, 10, 11, 12]
user = [456, 492, 578, 599, 670, 854]
plt.plot(month, user)
plt.title("Daehan company speeda net new customers")
plt.xlabel("Month")
plt.ylabel("User")
plt.grid()
plt.show()
11.1.2 이 표를 subplots()함수를 이용하여 다음과 같이 막대 그래프, 직선 그래프, 산포도 그래프, 수평막대 그래프로 표현하여라.
import numpy as np
y = np.arange(len(month))
plt.figure(figsize=(10,10), dpi=100)
plt.subplot(2,2,1)
plt.bar(month, user)
plt.xlabel("Month")
plt.ylabel("User")
plt.title("Bar chart")
plt.subplot(2,2,2)
plt.plot(month, user)
plt.xlabel("Month")
plt.ylabel("User")
plt.title("Line chart")
plt.subplot(2,2,3)
plt.scatter(month, user)
plt.xlabel("Month")
plt.ylabel("User")
plt.title("Scatter chart")
plt.subplot(2,2,4)
plt.barh(y, user)
plt.yticks(y, month)
plt.xlabel("User")
plt.ylabel("Month")
plt.title("Horizontal Bar chart")
plt.show()
**11.2 numpy의 sin(), cos() 함수를 호출하여 그림과 같은 주기함수를 표현해 보자. sin(), cos()등의 삼각함수는 그 주기가 2$\pi$이므로 x=0부터 x=2*np.pi*6 까지 6번 반복되는 주기함수를 얻을 수 있다. 이 코드에서 sin()함수는 빨간색 실선으로 표시되는데, x값이 커질수록 y의 진폭이 커지도록 하여라. cos()함수는 파란색 점선으로 나타나 있는데, 출력값은 -1에서 1사이이므로 이 값에 20을 곱하여 -20에서 20사이의 진폭을 가지도록 하여라.**
import matplotlib.pyplot as plt
import numpy as np
x1 = np.linspace(0, 2*np.pi*6, 200)
y1 = x1*np.sin(x1)
x2 = np.linspace(0, 2*np.pi*6, 200)
y2 = 20*np.cos(x2)
plt.figure(figsize=(8, 4), dpi=100)
plt.plot(x1, y1, color='r')
plt.plot(x2, y2, linestyle ='--', color='b')
plt.xlabel('x')
plt.grid()
plt.show()
11.3 numpy의 randint()함수를 사용하여 30개의 (x, y) 좌표쌍을 2차원 ndarray로 생성하여라. 이떄 x,y 범위는 0에서 50사이의 값으로 하여라. 그리고 생성된 좌표를 matplotlib의 산포도 그래프를 사용하여 그림과 같이 출력하여라.
import matplotlib.pyplot as plt
import numpy as np
rand_value = np.random.randint(1, 50, size=(30, 30))
plt.figure(figsize=(8, 4), dpi=100)
plt.scatter(rand_value[:, 1],rand_value[1, :] , color='r')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
11.4 numpy의 난수생성기를 이용하여 각각 1000개의 난수를 가지는 3가지 종류의 (x, y)분포를 생성하고 matplotlib의 산포도 그래프로 나타내어라. 왼쪽의 그림은 x값과 y값이 각각 평균이 25이고 표준편차가 6인 특성을 가진다. 가운데 그림을 x값의 평균이 25이고 표준편차가 6인 특성을 가지며 y값은 평균이 25이고 표준편차가 3인 특성을 가진다. 가장 오른쪽은 x값의 평균이 25이고 표준편차가 3인 특성을 가지며 y값은 평균이 25이고 표준편차가 6인 특성을 가진다.
import matplotlib.pyplot as plt
import numpy as np
mu1, sigma1 = 25, 6
mu2, sigma2 = 25, 3
Gauss_x1 = mu1 + sigma1*np.random.randn(1000)
Gauss_y1 = mu1 + sigma1*np.random.randn(1000)
Gauss_x2 = mu1 + sigma1*np.random.randn(1000)
Gauss_y2 = mu2 + sigma2*np.random.randn(1000)
Gauss_x3 = mu2 + sigma2*np.random.randn(1000)
Gauss_y3 = mu1 + sigma1*np.random.randn(1000)
plt.figure(figsize=(18, 6))
plt.subplot(1,3,1)
plt.scatter(Gauss_x1, Gauss_y1, alpha=0.4)
plt.xlim(0, 50)
plt.ylim(0, 50)
plt.xlabel('x')
plt.ylabel('y')
plt.subplot(1,3,2)
plt.scatter(Gauss_x2, Gauss_y2, color='r', alpha=0.4)
plt.xlim(0, 50)
plt.ylim(0, 50)
plt.xlabel('x')
plt.ylabel('y')
plt.subplot(1,3,3)
plt.scatter(Gauss_x3, Gauss_y3, color='g', alpha=0.4)
plt.xlim(0, 50)
plt.ylim(0, 50)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
11.5 다음은 동민이네 동물병원에 치료를 받은 개의 종류와 그 크기 데이터이다. 몸통 길이와 높이는 다음과 같이 cm 단위로 기록하였다.
닥스훈트
길이 | 77 | 78 | 85 | 83 | 73 | 77 | 73 | 80 |
---|---|---|---|---|---|---|---|---|
높이 | 25 | 28 | 29 | 30 | 21 | 22 | 17 | 35 |
사모예드
길이 | 75 | 77 | 86 | 86 | 79 | 83 | 83 | 83 |
---|---|---|---|---|---|---|---|---|
높이 | 56 | 57 | 50 | 53 | 60 | 53 | 49 | 61 |
말티즈
길이 | 34 | 38 | 38 | 41 | 30 | 37 | 41 | 35 |
---|---|---|---|---|---|---|---|---|
높이 | 22 | 25 | 19 | 30 | 21 | 24 | 28 | 18 |
11.5.1 이 데이터를 그림으로 다음과 같이 표시하여라. 닥스훈트는 빨간색 동그라미, 사모예드는 파란색 세모, 말티즈는 녹색 네모로 표시하여라(수평축은 길이, 수직축은 높이를 나타내도록 하여라).
import matplotlib.pyplot as plt
import numpy as np
length_d = [77, 78, 85, 93, 73, 77, 73, 80]
height_d = [25, 28, 29, 30, 21, 22, 17, 35]
length_s = [75, 77, 86, 86, 79, 83, 83, 83]
height_s = [56, 57, 50, 53, 60, 53, 49, 61]
length_m = [34, 38,38, 41, 30, 37, 41, 35]
height_m = [22, 25, 19, 30, 21, 24, 28, 18]
plt.figure(figsize=(12, 4), dpi=100)
plt.subplot(131)
plt.scatter(length_d,height_d ,marker='s' ,color='r')
plt.title("Dachshund size")
plt.xlabel("Length")
plt.ylabel("Height")
plt.grid()
plt.subplot(132)
plt.scatter(length_s,height_s ,marker='^', color='b')
plt.title("Samoyed size size")
plt.xlabel("Length")
plt.ylabel("Height")
plt.grid()
plt.subplot(133)
plt.scatter(length_m,height_m ,marker='s', color='g')
plt.title("Maltese size")
plt.xlabel("Length")
plt.ylabel("Height")
plt.grid()
plt.show()
11.5.2 이 세종류의 개들이 한 화면에 나타나도록 하여라. 이떄 다음과 같이 범례가 함께 나타나도록 하여라.
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(8, 4), dpi=100)
plt.scatter(length_d,height_d ,marker='s' ,color='r', label="Dachshund")
plt.scatter(length_s,height_s ,marker='^', color='b', label="Samoyed")
plt.scatter(length_m,height_m ,marker='s', color='g', label="Maltese")
plt.title("Dog size")
plt.xlabel("Length")
plt.ylabel("Height")
plt.legend()
plt.grid()
11.6 Infinite Series
**수학에서 급수는 수열의 합을 의미한다. 유한한 갯수의 항을 다루는 유한급수와는 달리, 무한히 많은 항의 합을 의미하는 무한급수는 그 합이 특정한 수로 수렴하는 경우도 있고, 그렇지 않은 경우도 있다. 수열 ${\displaystyle (a_{k})_{k=1}^{\infty }}$ 에 대한 (무한) 급수 ${ \sum _{k=1}^{\infty }a_{k}}$는 다음과 같이 표현할 수 있다.
$$ \lim_{n \to \infty} \sum_{k=1}^{n} a_k $$급수의 수렴에 대한 정확한 정의는 다음과 같다.
급수 $\sum_{k=1}^{\infty} a_k$ 의 $n$항까지의 부분합을 $S_n$로 나타내자. 이때, 수열 {$S_n$}가 어떤 실수 S로 수렴할 때 급수가 수렴한다고 정의한다. 급수가 수렴하지 않으면 발산한다.
다음과 같은 3개의 무한급수의 합을 그래프로 표현하고 무한급수 값이 제대로 수렴하는지 확인하여라. 이때, n은 무한대가 아닌 100, 1,000, 10,000 등으로 제한하여 실험적으로 수렴성을 확인해 보자.
import math
import numpy as np
import matplotlib.pyplot as plt
def my_fuc1(x):
return 1/(math.factorial(x))
def my_fuc2(x):
return 1/2**(x-1)
def my_fuc3(x):
return 1/x**2
n = 10000
series1_sum = np.zeros(n)
series2_sum = np.zeros(n)
series3_sum = np.zeros(n)
for i in range(n):
if i==0:
series1_sum[i] = my_fuc1(i+1)
series2_sum[i] = my_fuc2(i+1)
series3_sum[i] = my_fuc3(i+1)
else:
series1_sum[i] = series1_sum[i-1] + my_fuc1(i+1)
series2_sum[i] = series2_sum[i-1] + my_fuc2(i+1)
series3_sum[i] = series3_sum[i-1] + my_fuc3(i+1)
x = np.linspace(1, n, n)
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series1_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k!}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series2_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{2^k}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series3_sum[:], '.-')
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k^2}$")
plt.xlabel("n")
plt.ylabel("Sum")
plt.ylim(0,3)
plt.grid()
plt.show()
x = np.linspace(1, n, n)
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series1_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.xlim(-1,100)
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k!}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series2_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.xlim(-1,100)
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{2^k}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series3_sum[:], '.-')
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k^2}$")
plt.xlabel("n")
plt.ylabel("Sum")
plt.xlim(-1,100)
plt.ylim(0,3)
plt.grid()
plt.show()
x = np.linspace(1, n, n)
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series1_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.xlim(-10,1000)
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k!}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series2_sum[:], '.-')
plt.xlabel('n')
plt.ylabel("Sum")
plt.xlim(-10,1000)
plt.ylim(0,3)
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{2^k}$")
plt.grid()
plt.figure(figsize=(6, 3), dpi=100)
plt.plot(x[:], series3_sum[:], '.-')
plt.title(r"$\sum_{k=1}^{n}\quad\frac{1}{k^2}$")
plt.xlabel("n")
plt.ylabel("Sum")
plt.xlim(-10,1000)
plt.ylim(0,3)
plt.grid()
plt.show()