◎Python - Numpy 를 사용하여 데이터 피팅
#curve_fit code (figure2)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#최소자승법사용
def func(x, a, b, c):
return a*np.exp(-(x-b)**2/(2*c**2))
x = np.linspace(0,10,100)
y = func(x, 1, 5, 2)
yn = y + 0.2 * np.random.normal(size=len(x))
popt, pcov = curve_fit(func, x, yn)
cur_y = func(x, popt[0], popt[1], popt[2])
#그래프그리기
g1, = plt.plot(x, y, 'k')
g2, = plt.plot(x, yn, 'b.')
g3, = plt.plot(x, cur_y, 'r')
plt.legend([g1,g3], ['Funtion', 'Best fit'], loc = 2)
plt.axis([-2,12,-0.5,1.5])
plt.show()
◎Python - Numpy를 이용하여 함수의 해 도출
#code (figure5)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
def findIntersection(func1, func2, x0):
return fsolve(lambda x : func1(x) - func2(x), x0)
funky = lambda x : np.cos(x/5) * np.sin(x /2)
line = lambda x : 0.01 * x - 0.5
x = np.linspace(0,45,10000)
result = findIntersection(funky, line, [15,20,30,35,40,45])
funky_v = funky(x)
line_v = line(x)
#그래프그리기
g1, = plt.plot(x, funky_v, 'b')
g2, = plt.plot(x, line_v , 'k')
g3, = plt.plot(result, line(result), 'ro')
plt.legend([g1,g2], ['Funky func', 'Line func'], loc = 3)
plt.axis([0,45,-1.0,1.0])
plt.show()
'Python' 카테고리의 다른 글
Facebook Network에서 Community Detection (0) | 2013.06.15 |
---|---|
사회관계망 네트워크의 상호성 및 팔로어 수/친구 수 분포 분석 (0) | 2013.04.29 |
python 파일명 및 파일 확장자 확인하기 (0) | 2013.04.04 |
python 디렉토리(폴더) 안애 파일 목록 불러오기 (0) | 2013.04.04 |
python 스트링 값 비교 ( string compare) (0) | 2013.04.04 |