다운로드
작성자: admin 작성일시: 2016-05-24 12:53:36 조회수: 4745 다운로드: 320
카테고리: Python 태그목록:

SciPy 시작하기

SciPy Subpackages

  • scipy.stats
    • 통계 Statistics
  • scipy.constants
    • 물리/수학 상수 Physical and mathematical constants
  • scipy.special
    • 수학 함수 Any special mathematical functions
  • scipy.linalg
    • 선형 대수 Linear algebra routines
  • scipy.interpolate
    • 보간 Interpolation
  • scipy.optimize
    • 최적화 Optimization
  • scipy.fftpack
    • Fast Fourier transforms

scipy.stats 통계

  • Random Variable
    • 확률 밀도 함수, 누적 확률 함수
    • 샘플 생성
    • Parameter Estimation (fitting)
  • Test

scipy.stats 에서 제공하는 확률 모형

  • random variable 사용 방법
    1. 파라미터를 주고 random variable object 생성
    2. method 사용
  • Common Method
    • rvs: 샘플 생성
    • pdf or pmf: Probability Density Function
    • cdf: Cumulative Distribution Function
    • stats: Return mean, variance, (Fisher’s) skew, or (Fisher’s) kurtosis
    • moment: non-central moments of the distribution
    • fit: parameter estimation
  • Common Parameters
    • parameter는 모형 마다 달라진다.
    • random_state: seed
    • size: 생성하려는 샘플의 shape
    • loc: 일반적으로 평균의 값
    • scale: 일반적으로 표준편차의 값
In:
rv = sp.stats.norm(loc=10, scale=10)
rv.rvs(size=(3,10), random_state=1)
Out:
array([[ 26.24345364,   3.88243586,   4.71828248,  -0.72968622,
         18.65407629, -13.01538697,  27.44811764,   2.38793099,
         13.19039096,   7.50629625],
       [ 24.62107937, -10.60140709,   6.77582796,   6.15945645,
         21.33769442,  -0.99891267,   8.27571792,   1.22141582,
         10.42213747,  15.82815214],
       [ -1.00619177,  21.4472371 ,  19.01590721,  15.02494339,
         19.00855949,   3.16272141,   8.77109774,   0.64230566,
          7.3211192 ,  15.30355467]])
In:
sns.distplot(rv.rvs(size=10000, random_state=1))
Out:
In:
xx = np.linspace(-40, 60, 1000)
pdf = rv.pdf(xx)
plt.plot(xx, pdf)
Out:
[]
In:
cdf = rv.cdf(xx)
plt.plot(xx, cdf)
Out:
[]

scipy.constants 상수

  • 특별 상수
    • scipy.pi
  • 기타 상수

    • scipy.constants.XXXX
  • 단위

    • yotta, zetta, exa, peta, tera, giga, mega, kilo, hecto, deka
    • deci, centi, milli, micro, nano, pico, femto, atto, zepto
    • lb, oz, degree
    • inch, foot, yard, mile, au, light_year, parsec
    • hectare, acre, gallon
    • mph, mach, knot
In:
sp.pi
Out:
3.141592653589793
In:
import scipy.constants
In:
sp.constants.c # speed of light
Out:
299792458.0

scipy.special 수학 함수

  • Gamma, Beta, Erf, Logit
  • Bessel, Legendre
In:
x = np.linspace(-3, 3, 1000)
y1 = sp.special.erf(x)
a = plt.subplot(211)
plt.plot(x, y1)
plt.title("erf")
a.xaxis.set_ticklabels([])
y2 = sp.special.expit(x)
plt.subplot(212)
plt.plot(x, y2)
plt.title("logistic")
Out:

scipy.linalg 선형대수

  • inv, pinv, det
In:
A = np.array([[1, 2],
              [3, 4]])
sp.linalg.inv(A)  
Out:
array([[-2. ,  1. ],
       [ 1.5, -0.5]])
In:
sp.linalg.det(A)  
Out:
-2.0

scipy.interpolate 보간

  • 자료 사이의 빠진 부분을 유추
  • 1차원 보간
  • 2차원 보간
In:
from scipy.interpolate import interp1d
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 10, num=41)
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'])
Out:
In:
x, y = np.mgrid[-1:1:20j, -1:1:20j]
z = (x+y) * np.exp(-6.0*(x*x+y*y))
plt.pcolormesh(x, y, z)
Out:
In:
xnew, ynew = np.mgrid[-1:1:100j, -1:1:100j]
tck = sp.interpolate.bisplrep(x, y, z, s=0)
znew = sp.interpolate.bisplev(xnew[:,0], ynew[0,:], tck)
plt.pcolormesh(xnew, ynew, znew)
Out: