๋ณด๊ฐ๋ฒ(interpolation)์ด๋ ํต๊ณ์ ํน์ ์คํ์ ์ผ๋ก ๊ตฌํด์ง ๋ฐ์ดํฐ๋ค(xi)๋ก๋ถํฐ, ์ฃผ์ด์ง ๋ฐ์ดํฐ๋ฅผ ๋ง์กฑํ๋ ๊ทผ์ฌ ํจ์(f(x))๋ฅผ ๊ตฌํ๊ณ , ์ด ์์ ์ด์ฉํ์ฌ ์ฃผ์ด์ง ๋ณ์์ ๋ํ ํจ์ ๊ฐ์ ๊ตฌํ๋ ์ผ๋ จ์ ๊ณผ์ ์ ์๋ฏธํฉ๋๋ค. ์๋ฅผ ๋ค์ด, (0, 0), (1, 10), (2, 20)์ด ์ฃผ์ด์ก์ ๋, ์ด๋ค์ ๋ํ ๊ทผ์ฌ ํจ์๋ฅผ f(x) = 10x๋ก ๊ตฌํ๊ณ , 1.5์ ๋ํ ํจ์ ๊ฐ์ผ๋ก 15๋ฅผ ๊ตฌํ๋ ๊ฒ์ ๋๋ค.
์ฌ์ธ(sine) ํจ์์ ๊ฐ๊น์ด ์คํ ๋ฐ์ดํฐ๋ฅผ ์๊ฐํด ๋ณด๊ฒ ์ต๋๋ค. ๋ค์ ์์ ๋ NumPy, Matplotlib, SciPy๋ฅผ ์ข ํฉ์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
import numpy as np
from scipy.interpolate import interp1d
from matplotlib import pyplot as plt
# Cosine ํจ์๋ฅผ 0๋ถํฐ 10pi๊น์ง 20๊ฐ ๋ง๋ ๋ค.
x = np.linspace(0,10*np.pi, 20)
y = np.cos(x)
#interoperate ํจ์๋ก ๋ณด๊ฐ๋ฒ์ ์ ์ฉํ์ฌ linear(์ ํ๋ณด์ ) quadratic(๋ถ๋๋ฌ์ด ๋ณด์ ) ๋๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋ง๋ ๋ค
fl = interp1d(x,y,kind = 'linear')
fq = interp1d(x,y,kind = 'quadratic')
xint = np.linspace(x.min(), x.max(), 1000)
yintl = fl(xint)
yintq = fq(xint)
# Plot the data and the interpolation
plt.plot(xint, yintl, color = 'green', linewidth=2)
plt.plot(xint, yintq, color = 'red', linewidth=2)
plt.legend(['Linear','Quadratic'])
plt.plot(x,y,'o') #๊ฐ์ ์์น๋ฅผ ์ ์ผ๋ก ํํ
plt.ylim(-2,2)
plt.title('Interoperate')
plt.show()
6ํ์ ์ฝ๋๋ ์์์ 0๋ถํฐ 10*np.pi ๊น์ง ๊ท ๋ฑํ๊ฒ ๋๋์ด์ง 20๊ฐ ๊ฐ์ ๋ง๋ค์ด ๋ ๋๋ค. ์ฆ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ด ๋ง๋ค์ด ์ง๋๋ค.
[ 0. 1.65346982 3.30693964 4.96040945 6.61387927 8.26734909 9.92081891 11.57428872 13.22775854 14.88122836 16.53469818 18.18816799 19.84163781 21.49510763 23.14857745 24.80204727 26.45551708 28.1089869 29.76245672 31.41592654]
7ํ์ cosine ํจ์๋ฅผ ์คํ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ด ๊ตฌํด ์ง๋๋ค.
[ 1. -0.08257935 -0.9863613 0.24548549 0.94581724 -0.40169542 -0.87947375 0.54694816 0.78914051 -0.67728157 -0.67728157 0.78914051 0.54694816 -0.87947375 -0.40169542 0.94581724 0.24548549 -0.9863613 -0.08257935 1. ]
10ํ๊ณผ 11ํ์ SciPy interpolation(๋ณด๊ฐ๋ฒ)์ ์ฌ์ฉํ๋ ๋ถ๋ถ์ ๋๋ค. scipy.interpolate์ interp1d ํด๋์ค๋ ์ ํ ๋ณด๊ฐ๋ฒ์ ์ฌ์ฉํ์ฌ ์ฃผ์ด์ง ๋ฐ์ดํฐ๋ก ์ ์๋ ๋๋ฉ์ธ ๋ด ์ด๋์์๋ ํ๊ฐํ ์์๋ ๊ณ ์ ๋ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํจ์๋ฅผ ๋ง๋๋ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์ ๋๋ค. ์ด ํด๋์ค์ ์ธ์คํด์ค๋ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์ฑํ๋ 1-d ๋ฒกํฐ๋ฅผ ์ ๋ฌํ์ฌ ๋ง๋ค์ด์ง๋๋ค. interp1d ์์ ์ง์ํ๋ ๋ณด๊ฐ๋ฒ์ ์ข ๋ฅ๋ โlinearโ, โnearestโ, โzeroโ, โslinearโ, โquadraticโ, โcubicโ, โpreviousโ, โnextโ, where โzeroโ, โslinearโ, โquadraticโ, โcubicโ ๋ฑ์ด ์์ต๋๋ค.
13ํ, 14ํ์ xint ๋ฅผ x์ ์ต์๊ฐ ๋ถํฐ x์ ์ต๋๊ฐ๊น์ง 1000๊ฐ๋ก ์ธ๋ถํ ํ๊ณ linear, quadratic ๋ณด๊ฐ๋ฒ์ผ๋ก yint ๊ฐ์ ๊ตฌํ๋ ๋ถ๋ถ์ ๋๋ค.
18ํ ๋ถํฐ๋ matplotlib๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กฏํ๋ ๋ถ๋ถ์ ๋๋ค.
์์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ทธ๋ํ๊ฐ ์ถ๋ ฅ ๋ฉ๋๋ค.
์ฃผ์ด์ง ์ ๋ค์ ๊ฐ์ง๊ณ ๋ถ์์์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ธ์ต๋๋ค. X์ถ์ ์ด๋ ํ ์์น์์๋ Y๊ฐ์ ์์ ํ ์ ์์ต๋๋ค.