수치미분/적분 다 scipy에 있다! 수치적분 -> integrated
In [ ]:
uniform
: A uniform continuous random variable.norm
: A normal continuous random variable.beta
: A beta continuous random variable.gamma
: A gamma continuous random variable.t
: A Student’s T continuous random variable.chi2
: A chi-squared continuous random variable.f
: An F continuous random variable.multivariate_normal
: A multivariate normal random variable.dirichlet
: A Dirichlet random variable.wishart
: A Wishart random variable.bernoulli
: A Bernoulli discrete random variable.binom
: A binomial discrete random variable.boltzmann
: A Boltzmann (Truncated Discrete Exponential) random variable.rvs
: 샘플 생성 (random value sampling) => 실제로 많이 쓸것이다. 100 하면 100개 뽑아줌.pdf
or pmf
: Probability Density Functioncdf
: Cumulative Distribution Functionstats
: Return mean, variance, (Fisher’s) skew, or (Fisher’s) kurtosismoment
: non-central moments of the distributionfit
: parameter estimation => 어떤 샘플이 있을때,(정규분포라면), 중앙값 평균, 계산해준다.random_state
: seedsize
: 생성하려는 샘플의 shapeloc
: 일반적으로 평균의 값scale
: 일반적으로 표준편차의 값
In [7]:
rv = sp.stats.norm(loc=10, scale=10) # 정규분포는 노말이고, loc, scale은 선택이다. location = 평균, scale 은 표준편차?
rv.rvs(size=(3,10), random_state=1) # rvs = 실제 샘플 생성. (3x10) , random_state => seed값임.
Out[7]:
In [2]:
sns.distplot(rv.rvs(size=10000, random_state=1))
Out[2]:
In [5]:
xx = np.linspace(-40, 60, 1000)
pdf = rv.pdf(xx)
plt.plot(xx, pdf) # 확률밀도함수를 그렸다!
Out[5]:
In [6]:
cdf = rv.cdf(xx)
plt.plot(xx, cdf)
Out[6]:
In [ ]:
sp.pi
In [ ]:
import scipy.constants
In [ ]:
sp.constants.c # speed of light
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")
In [ ]:
A = np.array([[1, 2],
[3, 4]])
sp.linalg.inv(A)
In [ ]:
sp.linalg.det(A)
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'])
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)
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)
In [ ]:
from scipy import optimize
In [ ]:
def f(x):
return x**2 + 10*np.sin(x)
x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))
In [ ]:
result = optimize.minimize(f, 4)
print(result)
x0 = result['x']
x0
In [ ]:
plt.plot(x, f(x));
plt.hold(True)
plt.scatter(x0, f(x0), s=200)
In [ ]:
x1 = optimize.minimize(sixhump, (1, 1))['x']
x2 = optimize.minimize(sixhump, (-1, -1))['x']
print(x1, x2)
In [ ]:
time_step = 0.02
period = 5.
time_vec = np.arange(0, 20, time_step)
sig = np.sin(2 * np.pi / period * time_vec) + 0.5 * np.random.randn(time_vec.size)
plt.plot(sig)
In [ ]:
import scipy.fftpack
sample_freq = sp.fftpack.fftfreq(sig.size, d=time_step)
sig_fft = sp.fftpack.fft(sig)
pidxs = np.where(sample_freq > 0)
freqs, power = sample_freq[pidxs], np.abs(sig_fft)[pidxs]
freq = freqs[power.argmax()]
plt.stem(freqs[:50], power[:50])
plt.xlabel('Frequency [Hz]')
plt.ylabel('plower')
In [12]:
rv = sp.stats.norm(10,10)
In [13]:
x = rv.rvs(100)
In [14]:
x
Out[14]:
In [18]:
x_avg, x_std = sp.stats.norm.fit(x)
In [38]:
x_std
Out[38]:
In [37]:
x_graph = rv.pdf(x)
In [41]:
sns.distplot(x)
Out[41]:
In [27]:
y = rv.rvs(30, 100)
y
Out[27]:
In [17]:
y.mean()
Out[17]:
In [35]:
y_avg, y_std = sp.stats.norm.fit(y)
In [31]:
y_graph = sp.stats.norm(y_avg, y_std)
In [32]:
y_graph
Out[32]:
In [56]:
aaa_list = []
for _ in range(30):
aaa = rv.rvs(100)
aaa_list.append(aaa.mean())
aaa_list
Out[56]:
In [57]:
sns.distplot(aaa_list)
Out[57]:
In [52]:
sns.distplot(sp.stats.norm(10,10).rvs((50,100)).mean(axis=1));
In [ ]: