In [391]:
import numpy as np
In [392]:
### Data Processing Using Arrays
In [393]:
points = np.arange(-5, 5 ,0.01)
In [394]:
xs, ys = np.meshgrid(points, points)
In [395]:
xs, ys, xs.shape, ys.shape
Out[395]:
In [396]:
nx, ny = (3, 2)
In [397]:
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
x, y
Out[397]:
In [398]:
xv, yv = np.meshgrid(x ,y)
xv, yv
Out[398]:
In [399]:
import matplotlib.pyplot as plt
In [400]:
z = np.sqrt(xs ** 2 + ys ** 2)
z.shape
Out[400]:
In [401]:
%matplotlib inline
In [402]:
plt.imshow(z, cmap=plt.cm.gray); plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")
Out[402]:
In [403]:
### Expressing Conditional Logic as Array Operations
In [404]:
xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
In [405]:
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
In [406]:
cond = np.array([True, False, True, True, False])
In [407]:
result = [(x if c else y) for x,y,c in zip(xarr, yarr, cond)]
result
Out[407]:
In [408]:
result = np.where(cond, xarr, yarr)
In [409]:
result
Out[409]:
In [410]:
arr = np.random.randn(4,4)
arr
Out[410]:
In [411]:
np.where(arr > 0, 2, -2)
Out[411]:
In [412]:
np.where(arr > 0, 2, arr)
Out[412]:
In [413]:
cond1 = np.array([True, False, True, True, False])
cond2 = np.array([True, True, False, True, True])
In [414]:
np.where(cond1&cond2, 0, np.where(cond1, 1, np.where(cond2, 2, 3)))
Out[414]:
In [415]:
1 * (cond1&~cond2) + 2 * (cond2&~cond1) + 3 * ~(cond1|cond2)
Out[415]:
In [416]:
cond.any(),cond.all()
Out[416]:
In [417]:
### Mathematical and Statistical Methods
In [418]:
arr = np.random.randn(5, 4)
In [419]:
arr.mean(), np.mean(arr)
Out[419]:
In [420]:
arr.sum(), np.sum(arr)
Out[420]:
In [421]:
arr.mean(1), arr.mean(axis = 1)
Out[421]:
In [422]:
arr = np.array([[0 ,1, 2], [3, 4, 5], [6, 7, 8]])
In [423]:
arr.cumsum(0), arr.cumsum(1)
Out[423]:
In [424]:
arr.cumprod(0), arr.cumprod(1)
Out[424]:
In [425]:
np.argmax(arr), np.argmin(arr)
Out[425]:
In [426]:
np.std(arr), np.var(arr)
Out[426]:
In [427]:
### Sorting
In [428]:
arr = np.random.randn(8)
arr
Out[428]:
In [429]:
arr.sort()
arr
Out[429]:
In [430]:
arr = np.random.randn(8, 3)
sort_arr = np.sort(np.sort(arr, 0), 1)
sort_arr
Out[430]:
In [431]:
np.sort(np.sort(arr, 0), 1) == np.sort(np.sort(arr, 1), 0)
Out[431]:
In [432]:
arr1 = np.array([[3,1],[2,4]])
arr2 = np.array([[3,1],[2,4]])
In [433]:
arr1.sort(0);arr1.sort(1)
arr2.sort(1);arr2.sort(0)
arr1, arr2
Out[433]:
In [434]:
### Unique and other set logic
In [435]:
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
np.unique(names)
Out[435]:
In [436]:
sorted(set(names))
Out[436]:
In [437]:
values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])
Out[437]:
In [438]:
'''
unique(x) Compute the sorted, unique elements in x
intersect1d(x, y) Compute the sorted, common elements in x and y
union1d(x, y) Compute the sorted union of elements
in1d(x, y) Compute a boolean array indicating whether each element of x is contained in y
setdiff1d(x, y) Set difference, elements in x that are not in y
setxor1d(x, y) Set symmetric differences; elements that are in either of the arrays, but not both
'''
Out[438]:
In [439]:
### Linear Algebra
In [440]:
x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
In [441]:
x.dot(y)
Out[441]:
In [442]:
np.dot(x,y)
Out[442]:
In [443]:
np.dot(x, np.ones(3))
Out[443]:
In [444]:
from numpy.linalg import inv, qr
In [445]:
X = np.random.randn(5, 5)
X
Out[445]:
In [446]:
mat = X.T.dot(X)
mat
Out[446]:
In [447]:
inv(mat)
Out[447]:
In [448]:
mat.dot(inv(mat))
Out[448]:
In [449]:
q, r = qr(mat)
q, r
Out[449]:
In [450]:
q.T, inv(q)
Out[450]:
In [451]:
A = np.array([[1, 0.5], [0.5, 1]])
In [452]:
# eigenvalue 1.5, 0.5
for i in range(10):
q, r = qr(A)
A = r.dot(q)
q, r = qr(A)
A, q ,r
Out[452]:
In [453]:
value, vector = np.linalg.eig(np.array([[1, 0.5], [0.5, 1]]))
value, vector
Out[453]:
In [454]:
np.linalg.det(vector)
Out[454]:
In [455]:
np.array([[1, 0.5], [0.5, 1]]).dot(vector[:,1]), 0.5*(vector[:,1])
Out[455]:
In [456]:
np.array([[1, 0.5], [0.5, 1]]).dot(vector[:,0]), 1.5*(vector[:,0])
Out[456]:
In [457]:
value, vector = np.linalg.eig(np.array([[1.5, 0], [0, 0.5]]))
value, vector
Out[457]:
In [458]:
np.linalg.det(vector)
Out[458]:
In [459]:
np.diag(np.array([[1, 0.5], [0.5, 1]]))
Out[459]:
In [460]:
np.diag(np.array([1, 1]))
Out[460]:
In [461]:
# Compute the sum of the diagonal elements
np.trace(np.array([[1, 0.5], [0.5, 1]]))
Out[461]:
In [462]:
# Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``:
a = np.array([[3,1], [1,2]])
b = np.array([9,8])
x = np.linalg.solve(a, b)
x
Out[462]:
In [463]:
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
A = np.vstack([x, np.ones(len(x))]).T
A
Out[463]:
In [464]:
p = np.linalg.lstsq(A, y)[0]
m, c
Out[464]:
In [465]:
import matplotlib.pyplot as plt
plt.plot(x, y, 'o', label='Original data', markersize=10)
plt.plot(x, m*x+c, 'r', label='Fitted line')
plt.legend()
plt.show()
In [466]:
### Random Number Generation
In [467]:
samples = np.random.normal(size = (4, 4))
samples
Out[467]:
In [468]:
from random import normalvariate
In [469]:
N = 1000000
In [470]:
%timeit samples = [normalvariate(0, 1) for _ in range(0, N)]
In [471]:
%timeit np.random.normal(size=N)
In [472]:
'''
seed Seed the random number generator
permutation Return a random permutation of a sequence, or return a permuted range
shuffle Randomly permute a sequence in place
rand Draw samples from a uniform distribution
randint Draw random integers from a given low-to-high range
randn Draw samples from a normal distribution with mean 0 and standard deviation 1 (MATLAB-like interface)
binomial Draw samples a binomial distribution
normal Draw samples from a normal (Gaussian) distribution
beta Draw samples from a beta distribution
chisquare Draw samples from a chi-square distribution
gamma Draw samples from a gamma distribution
uniform Draw samples from a uniform [0, 1) distribution
'''
Out[472]:
In [473]:
# Built-in method
import random
position = 0
walk = [position]
steps = 1000
for i in range(0, steps):
step = 1 if random.randint(0, 1) else -1
position += step
walk.append(position)
In [474]:
# numpy method
nsteps = 1000
draws = np.random.randint(0, 2, size=nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()
In [475]:
walk, walk.min(), walk.max()
Out[475]:
In [478]:
(np.abs(walk) >= 10).argmax()
Out[478]:
In [480]:
nwalks = 5000; nsteps = 1000
In [481]:
draws = np.random.randint(0, 2, size=(nwalks, nsteps))
In [482]:
steps = np.where(draws > 0, 1, -1)
In [483]:
walks = steps.cumsum(1)
In [484]:
walks
Out[484]:
In [485]:
walks.max(), walks.min()
Out[485]:
In [486]:
hits30 = (np.abs(walks) >= 30).any(1)
In [487]:
hits30
Out[487]:
In [488]:
hits30.sum()
Out[488]:
In [491]:
walks[hits30].shape
Out[491]:
In [494]:
crossing_times = np.abs(walks[hits30] >= 30).argmax(1)
In [495]:
crossing_times
Out[495]:
In [498]:
crossing_times.shape
Out[498]:
In [499]:
crossing_times.mean()
Out[499]:
In [ ]: