mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-03 10:01:56 +08:00
2020-10-19 21:08:55
This commit is contained in:
122
docs/da/055.md
122
docs/da/055.md
@@ -10,19 +10,19 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%pylab inline
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Populating the interactive namespace from numpy and matplotlib
|
||||
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
heights = array([1.46, 1.79, 2.01, 1.75, 1.56, 1.69, 1.88, 1.76, 1.88, 1.78])
|
||||
|
||||
```
|
||||
@@ -31,7 +31,7 @@ heights = array([1.46, 1.79, 2.01, 1.75, 1.56, 1.69, 1.88, 1.76, 1.88, 1.78])
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
print 'mean, ', heights.mean()
|
||||
print 'min, ', heights.min()
|
||||
print 'max, ', heights.max()
|
||||
@@ -39,7 +39,7 @@ print 'standard deviation, ', heights.std()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
mean, 1.756
|
||||
min, 1.46
|
||||
max, 2.01
|
||||
@@ -51,7 +51,7 @@ standard deviation, 0.150811140172
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
import scipy.stats.stats as st
|
||||
|
||||
```
|
||||
@@ -60,7 +60,7 @@ import scipy.stats.stats as st
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
print 'median, ', st.nanmedian(heights) # 忽略nan值之后的中位数
|
||||
print 'mode, ', st.mode(heights) # 众数及其出现次数
|
||||
print 'skewness, ', st.skew(heights) # 偏度
|
||||
@@ -69,7 +69,7 @@ print 'and so many more...'
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
median, 1.77
|
||||
mode, (array([ 1.88]), array([ 2.]))
|
||||
skewness, -0.393524456473
|
||||
@@ -105,7 +105,7 @@ and so many more...
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.stats import norm
|
||||
|
||||
```
|
||||
@@ -121,7 +121,7 @@ from scipy.stats import norm
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
x_norm = norm.rvs(size=500)
|
||||
type(x_norm)
|
||||
|
||||
@@ -129,7 +129,7 @@ type(x_norm)
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
numpy.ndarray
|
||||
```
|
||||
|
||||
@@ -137,14 +137,14 @@ numpy.ndarray
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
h = hist(x_norm)
|
||||
print 'counts, ', h[0]
|
||||
print 'bin centers', h[1]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
counts, [ 7\. 21\. 42\. 97\. 120\. 91\. 64\. 38\. 17\. 3.]
|
||||
bin centers [-2.68067801 -2.13266147 -1.58464494 -1.0366284 -0.48861186 0.05940467
|
||||
0.60742121 1.15543774 1.70345428 2.25147082 2.79948735]
|
||||
@@ -230,7 +230,7 @@ khphoEtSI/4P1qxllG6H6EYAAAAASUVORK5CYII=
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
h = hist(x_norm, normed=True, bins=20)
|
||||
|
||||
```
|
||||
@@ -316,7 +316,7 @@ AAAASUVORK5CYII=
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
x_mean, x_std = norm.fit(x_norm)
|
||||
|
||||
print 'mean, ', x_mean
|
||||
@@ -324,7 +324,7 @@ print 'x_std, ', x_std
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
mean, -0.0426135499965
|
||||
x_std, 0.950754110144
|
||||
|
||||
@@ -334,7 +334,7 @@ x_std, 0.950754110144
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
h = hist(x_norm, normed=True, bins=20)
|
||||
|
||||
x = linspace(-3,3,50)
|
||||
@@ -512,7 +512,7 @@ KZGLiETc/wfoqVYxTpVDEwAAAABJRU5ErkJggg==
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.integrate import trapz
|
||||
|
||||
```
|
||||
@@ -521,7 +521,7 @@ from scipy.integrate import trapz
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
x1 = linspace(-2,2,108)
|
||||
p = trapz(norm.pdf(x1), x1)
|
||||
print '{:.2%} of the values lie between -2 and 2'.format(p)
|
||||
@@ -531,14 +531,14 @@ plot(x, norm.pdf(x), 'k-')
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
95.45% of the values lie between -2 and 2
|
||||
|
||||
```
|
||||
|
||||
Out[13]:
|
||||
|
||||
```
|
||||
```py
|
||||
[<matplotlib.lines.Line2D at 0x15cbb8d0>]
|
||||
```
|
||||
|
||||
@@ -751,7 +751,7 @@ FXhjjIlSVuCNMSZK/T8+DStxCxquAQAAAABJRU5ErkJggg==
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
p = plot(x, norm.pdf(x, loc=0, scale=1))
|
||||
p = plot(x, norm.pdf(x, loc=0.5, scale=2))
|
||||
p = plot(x, norm.pdf(x, loc=-0.5, scale=.5))
|
||||
@@ -1020,7 +1020,7 @@ AAAAAElFTkSuQmCC
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
p = plot(x, norm(loc=0, scale=1).pdf(x))
|
||||
p = plot(x, norm(loc=0.5, scale=2).pdf(x))
|
||||
p = plot(x, norm(loc=-0.5, scale=.5).pdf(x))
|
||||
@@ -1289,7 +1289,7 @@ AAAAAElFTkSuQmCC
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.stats import lognorm, t, dweibull
|
||||
|
||||
```
|
||||
@@ -1300,7 +1300,7 @@ from scipy.stats import lognorm, t, dweibull
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = linspace(0.01, 3, 100)
|
||||
|
||||
plot(x, lognorm.pdf(x, 1), label='s=1')
|
||||
@@ -1313,7 +1313,7 @@ legend()
|
||||
|
||||
Out[17]:
|
||||
|
||||
```
|
||||
```py
|
||||
<matplotlib.legend.Legend at 0x15781c88>
|
||||
```
|
||||
|
||||
@@ -1543,7 +1543,7 @@ OolJKaWikN7KRimlopAGd6WUikIa3JVSKgppcFdKqSikwV0ppaKQBnellIpCGtyVUioKaXBXSqko
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = linspace(0.01, 3, 100)
|
||||
|
||||
plot(x, dweibull.pdf(x, 1), label='s=1, constant failure rate')
|
||||
@@ -1556,7 +1556,7 @@ legend()
|
||||
|
||||
Out[18]:
|
||||
|
||||
```
|
||||
```py
|
||||
<matplotlib.legend.Legend at 0xaa9bc50>
|
||||
```
|
||||
|
||||
@@ -1881,7 +1881,7 @@ AAAAAElFTkSuQmCC
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = linspace(-3, 3, 100)
|
||||
|
||||
plot(x, t.pdf(x, 1), label='df=1')
|
||||
@@ -1895,7 +1895,7 @@ legend()
|
||||
|
||||
Out[19]:
|
||||
|
||||
```
|
||||
```py
|
||||
<matplotlib.legend.Legend at 0x164582e8>
|
||||
```
|
||||
|
||||
@@ -2312,7 +2312,7 @@ WVYNZRO8ZVlWDWUTvGVZVg1lE7xlWVYN9f98mteu43HACgAAAABJRU5ErkJggg==
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.stats import binom, poisson, randint
|
||||
|
||||
```
|
||||
@@ -2323,7 +2323,7 @@ from scipy.stats import binom, poisson, randint
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
high = 10
|
||||
low = -10
|
||||
|
||||
@@ -2423,7 +2423,7 @@ khpg2EtSAwx7SWqAYS9JDTDsJakBhr0kNcCwl6QG/H9rCjVZAnNsTgAAAABJRU5ErkJggg==
|
||||
|
||||
In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
num_trials = 60
|
||||
x = arange(num_trials)
|
||||
|
||||
@@ -2436,7 +2436,7 @@ legend()
|
||||
|
||||
Out[22]:
|
||||
|
||||
```
|
||||
```py
|
||||
<matplotlib.legend.Legend at 0x1738a198>
|
||||
```
|
||||
|
||||
@@ -2708,7 +2708,7 @@ aLJXSqk48P8B4O96y1bc/nYAAAAASUVORK5CYII=
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = arange(0,21)
|
||||
|
||||
plot(x, poisson(1).pmf(x), 'o-', label=r'$\lambda$=1')
|
||||
@@ -2721,7 +2721,7 @@ legend()
|
||||
|
||||
Out[23]:
|
||||
|
||||
```
|
||||
```py
|
||||
<matplotlib.legend.Legend at 0x1763e320>
|
||||
```
|
||||
|
||||
@@ -3008,7 +3008,7 @@ gg==
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.stats import rv_discrete
|
||||
|
||||
```
|
||||
@@ -3017,7 +3017,7 @@ from scipy.stats import rv_discrete
|
||||
|
||||
In [25]:
|
||||
|
||||
```
|
||||
```py
|
||||
xk = [1, 2, 3, 4, 5, 6]
|
||||
pk = [.3, .35, .25, .05, .025, .025]
|
||||
|
||||
@@ -3027,7 +3027,7 @@ pk = [.3, .35, .25, .05, .025, .025]
|
||||
|
||||
In [26]:
|
||||
|
||||
```
|
||||
```py
|
||||
loaded = rv_discrete(values=(xk, pk))
|
||||
|
||||
```
|
||||
@@ -3038,14 +3038,14 @@ loaded = rv_discrete(values=(xk, pk))
|
||||
|
||||
In [27]:
|
||||
|
||||
```
|
||||
```py
|
||||
loaded.rvs(size=2)
|
||||
|
||||
```
|
||||
|
||||
Out[27]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([3, 1])
|
||||
```
|
||||
|
||||
@@ -3053,7 +3053,7 @@ array([3, 1])
|
||||
|
||||
In [28]:
|
||||
|
||||
```
|
||||
```py
|
||||
samples = loaded.rvs(size=100)
|
||||
bins = linspace(.5,6.5,7)
|
||||
|
||||
@@ -3064,7 +3064,7 @@ stem(xk, loaded.pmf(xk), markerfmt='ro', linefmt='r-')
|
||||
|
||||
Out[28]:
|
||||
|
||||
```
|
||||
```py
|
||||
<Container object of 3 artists>
|
||||
```
|
||||
|
||||
@@ -3187,7 +3187,7 @@ iXLAm5klygFvZpYoB7yZWaIc8GZmifp/J4pqDI9mKMcAAAAASUVORK5CYII=
|
||||
|
||||
In [29]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.stats import norm
|
||||
from scipy.stats import ttest_ind, ttest_rel, ttest_1samp
|
||||
from scipy.stats import t
|
||||
@@ -3200,7 +3200,7 @@ from scipy.stats import t
|
||||
|
||||
In [30]:
|
||||
|
||||
```
|
||||
```py
|
||||
n1 = norm(loc=0.3, scale=1.0)
|
||||
n2 = norm(loc=0, scale=1.0)
|
||||
|
||||
@@ -3210,7 +3210,7 @@ n2 = norm(loc=0, scale=1.0)
|
||||
|
||||
In [31]:
|
||||
|
||||
```
|
||||
```py
|
||||
n1_samples = n1.rvs(size=100)
|
||||
n2_samples = n2.rvs(size=100)
|
||||
|
||||
@@ -3220,7 +3220,7 @@ n2_samples = n2.rvs(size=100)
|
||||
|
||||
In [32]:
|
||||
|
||||
```
|
||||
```py
|
||||
samples = hstack((n1_samples, n2_samples))
|
||||
|
||||
```
|
||||
@@ -3229,7 +3229,7 @@ samples = hstack((n1_samples, n2_samples))
|
||||
|
||||
In [33]:
|
||||
|
||||
```
|
||||
```py
|
||||
loc, scale = norm.fit(samples)
|
||||
n = norm(loc=loc, scale=scale)
|
||||
|
||||
@@ -3239,7 +3239,7 @@ n = norm(loc=loc, scale=scale)
|
||||
|
||||
In [34]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = linspace(-3,3,100)
|
||||
|
||||
hist([samples, n1_samples, n2_samples], normed=True)
|
||||
@@ -3251,7 +3251,7 @@ plot(x, n2.pdf(x), 'r-')
|
||||
|
||||
Out[34]:
|
||||
|
||||
```
|
||||
```py
|
||||
[<matplotlib.lines.Line2D at 0x17ca7278>]
|
||||
```
|
||||
|
||||
@@ -3544,7 +3544,7 @@ IYRwcpLIhRDCyUkiF0IIJ/d/XdaGnd6OVMIAAAAASUVORK5CYII=
|
||||
|
||||
In [35]:
|
||||
|
||||
```
|
||||
```py
|
||||
t_val, p = ttest_ind(n1_samples, n2_samples)
|
||||
|
||||
print 't = {}'.format(t_val)
|
||||
@@ -3552,7 +3552,7 @@ print 'p-value = {}'.format(p)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
t = 0.868384594123
|
||||
p-value = 0.386235148899
|
||||
|
||||
@@ -3566,7 +3566,7 @@ p-value = 0.386235148899
|
||||
|
||||
In [36]:
|
||||
|
||||
```
|
||||
```py
|
||||
pop_size = 35
|
||||
|
||||
pre_treat = norm(loc=0, scale=1)
|
||||
@@ -3578,7 +3578,7 @@ n0 = pre_treat.rvs(size=pop_size)
|
||||
|
||||
In [37]:
|
||||
|
||||
```
|
||||
```py
|
||||
effect = norm(loc=0.05, scale=0.2)
|
||||
eff = effect.rvs(size=pop_size)
|
||||
|
||||
@@ -3590,7 +3590,7 @@ n1 = n0 + eff
|
||||
|
||||
In [38]:
|
||||
|
||||
```
|
||||
```py
|
||||
loc, scale = norm.fit(n1)
|
||||
post_treat = norm(loc=loc, scale=scale)
|
||||
|
||||
@@ -3600,7 +3600,7 @@ post_treat = norm(loc=loc, scale=scale)
|
||||
|
||||
In [39]:
|
||||
|
||||
```
|
||||
```py
|
||||
fig = figure(figsize=(10,4))
|
||||
|
||||
ax1 = fig.add_subplot(1,2,1)
|
||||
@@ -3867,7 +3867,7 @@ SEBUWImIiIgERIWViIiISEBUWImIiIgE5P8B6WVRSESuXscAAAAASUVORK5CYII=
|
||||
|
||||
In [40]:
|
||||
|
||||
```
|
||||
```py
|
||||
t_val, p = ttest_ind(n0, n1)
|
||||
|
||||
print 't = {}'.format(t_val)
|
||||
@@ -3875,7 +3875,7 @@ print 'p-value = {}'.format(p)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
t = -0.347904839913
|
||||
p-value = 0.728986322039
|
||||
|
||||
@@ -3887,7 +3887,7 @@ p-value = 0.728986322039
|
||||
|
||||
In [41]:
|
||||
|
||||
```
|
||||
```py
|
||||
t_val, p = ttest_rel(n0, n1)
|
||||
|
||||
print 't = {}'.format(t_val)
|
||||
@@ -3895,7 +3895,7 @@ print 'p-value = {}'.format(p)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
t = -1.89564459709
|
||||
p-value = 0.0665336223673
|
||||
|
||||
@@ -3909,7 +3909,7 @@ p-value = 0.0665336223673
|
||||
|
||||
In [42]:
|
||||
|
||||
```
|
||||
```py
|
||||
my_t = t(pop_size) # 传入参数为自由度,这里自由度为50
|
||||
|
||||
p = plot(x, my_t.pdf(x), 'b-')
|
||||
|
||||
Reference in New Issue
Block a user