%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['mat'] `%matplotlib` prevents importing * from pylab and numpy
random.random(4)
array([ 0.09580512, 0.92183154, 0.58505331, 0.70484202])
random.random( (4,4) )
array([[ 0.70456357, 0.6430362 , 0.8208286 , 0.25123117], [ 0.34583649, 0.25375133, 0.75113203, 0.65106539], [ 0.8120159 , 0.04777626, 0.56607977, 0.92883318], [ 0.42414279, 0.66161015, 0.2885714 , 0.44186809]])
mat = random.random((4,4))
sig = random.random(100)
plot(sig)
[<matplotlib.lines.Line2D at 0x7ff13244c2d0>]
sig = (random.random(10000)*2) - 1
plot(sig)
[<matplotlib.lines.Line2D at 0x7ff132363350>]
mean(sig)
-0.0048591879564845236
random.random(10000).mean()
0.49947780352343579
random.random(100000).mean()
0.49891685052226675
random.random(10000000).mean()
0.49999400685838408
randint(1, 7, 10)
array([1, 2, 2, 1, 6, 6, 5, 6, 1, 3])
randint(1, 21, 10)
array([20, 2, 2, 14, 1, 6, 4, 14, 11, 12])
randint(1, 7, 10).mean()
3.1000000000000001
randint(1, 7, 1000).mean()
3.4849999999999999
die_throws = randint(1, 7, 1000)
from scipy.stats import mode
mode(die_throws)
(array([ 2.]), array([ 190.]))
1000/6
166.66666666666666
sig = array ([13, 18, 13, 14, 13, 16, 14, 21, 13])
sig.mean()
15.0
sig.median()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-158-3bcafa1ac3fb> in <module>() ----> 1 sig.median() AttributeError: 'numpy.ndarray' object has no attribute 'median'
mean(sig), median(sig)
(15.0, 14.0)
sort(sig)
array([13, 13, 13, 13, 14, 14, 16, 18, 21])
mode(sig)
(array([ 13.]), array([ 4.]))
hist(sig);
mode(sig)
(array([ 13.]), array([ 4.]))
sig = r_[13, 18, 13, 14, 13, 16, 14, 21, 13, 18, 18, 18]
mode(sig)
(array([ 13.]), array([ 4.]))
normal()
-1.274271224198872
sig = normal(size=100)
plot(sig)
[<matplotlib.lines.Line2D at 0x7ff132300450>]
mat = normal(size=(128,128))
subplot(121)
imshow(mat)
colorbar()
mat = random.random(size=(128,128))
subplot(122)
imshow(mat)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7ff13043c758>
#from numpy.random import *
randint(1,7, 10)
die = randint(1,7, 10)
die
hist(die)
(array([ 1., 0., 4., 0., 0., 1., 0., 2., 0., 2.]), array([ 2. , 2.4, 2.8, 3.2, 3.6, 4. , 4.4, 4.8, 5.2, 5.6, 6. ]), <a list of 10 Patch objects>)
die = randint(1,7, 10)
hist(die);
hist(die, bins= (arange(7)+0.5))
(array([ 0., 3., 2., 0., 4., 1.]), array([ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5]), <a list of 6 Patch objects>)
die = randint(1,7, 100)
hist(die, bins= (arange(7)+0.5))
(array([ 20., 18., 18., 11., 18., 15.]), array([ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5]), <a list of 6 Patch objects>)
die = randint(1,7, 10000)
hist(die, bins= (arange(7)+0.5));
r = normal(size=10)
hist(r);
r = normal(size=100)
hist(r);
r = normal(0, 1, 10000)
hist(r);
hist(r, 31);
r = normal(0, 5, 10000)
hist(r, 31);
hist(r);
hist(r, 30);
r = normal(0, 5, 10000)
type(r)
numpy.ndarray
hist(r, 31)
print(r.std(), r.var())
5.03420913002 25.3432615648
r = exponential(1, 10000)
hist(r, 30);
r = exponential(2, 10000)
hist(r, 30);
r = exponential(10, 10000)
hist(r, 30);
r = exponential(1, 10000)
hist(r, 30);
r = triangular(0,0, 5, 10000)
hist(r);
hist(r,30);
r = triangular(left=1, mode=3, right=9, size=10000)
hist(r);
hist(r,31);
r = gamma(1, 2, 10000)
hist(r,30);
r = gamma(5, 1, 10000)
hist(r,30);
r = gamma(50, 1, 10000)
hist(r,30);
r = poisson(0.5, 10000)
hist(r,30);
r = poisson(10, 10000)
hist(r,30);
binomial(1000, 1.0/6.0)
171
100.0/6
16.666666666666668
print(binomial(10000, 1.0/6.0))
r = binomial(10000, 1.0/6.0, 10000)
1634
hist(r,30);
hist(sig);
from scipy import stats
quantiles = linspace(-0.5,1.5, 100)
plot(quantiles, stats.uniform.pdf(quantiles))
ylim((0, 1.1))
(0, 1.1)
quantiles = linspace(-4.5,4.5, 1024)
plot(quantiles, stats.norm.pdf(quantiles))
ylim((0, 1.1))
(0, 1.1)
quantiles = arange(50)
plot(quantiles, stats.poisson.pmf(quantiles, 0.2), 'o-')
plot(quantiles, stats.poisson.pmf(quantiles, 5), 'o-')
plot(quantiles, stats.poisson.pmf(quantiles, 20), 'o-')
legend(['$\lambda$ = 0.2','$\lambda$ = 5','$\lambda$ = 20'])
<matplotlib.legend.Legend at 0x7ff12bef0ad0>
By: Andrés Cabrera mantaraya36@gmail.com For MAT course MAT 201A at UCSB
This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/