1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
\documentclass[12pt]{article}
\usepackage{amsmath}
\title{Python Workshop\\Problems and Exercises}
\author{Asokan Pichai\\Prabhu Ramachandran}
\begin{document}
\maketitle
\section{Matrices and Arrays \& 2D Plotting}
\subsection{Matrices and Arrays}
\subsubsection{Basic Numpy}
\begin{verbatim}
# Simple array math example
>>> import numpy as np
>>> a = np.array([1,2,3,4])
>>> b = np.arange(2,6)
>>> b
array([2,3,4,5])
>>> a*2 + b + 1 # Basic math!
array([5, 8, 11, 14])
# Pi and e are defined.
>>> x = np.linspace(0.0, 10.0, 1000)
>>> x *= 2*np.pi/10 # inplace.
# apply functions to array.
>>> y = np.sin(x)
>>> z = np.exp(y)
>>> x = np.array([1., 2, 3, 4])
>>> np.size(x)
4
>>> x.dtype # What is a.dtype?
dtype('float64')
>>> x.shape
(4,)
>>> print np.rank(x), x.itemsize
1 8
>>> x[0] = 10
>>> print x[0], x[-1]
10.0 4.0
>>> a = np.array([[ 0, 1, 2, 3],
... [10,11,12,13]])
>>> a.shape # (rows, columns)
(2, 4)
# Accessing and setting values
>>> a[1,3]
13
>>> a[1,3] = -1
>>> a[1] # The second row
array([10,11,12,-1])
>>> b=np.array([[0,2,4,2],[1,2,3,4]])
>>> np.add(a,b,a)
>>> np.sum(x,axis=1)
>>> np.greater(a,4)
>>> np.sqrt(a)
\end{verbatim}
\subsubsection{Array Creation}
\begin{verbatim}
>>> np.array([2,3,4])
array([2, 3, 4])
>>> np.linspace(0, 2, 4)
array([0.,0.6666667,1.3333333,2.])
>>>np.ones([2,2])
array([[ 1., 1.],
[ 1., 1.]])
>>>a = np.array([[1,2,3],[4,5,6]])
>>>np.ones_like(a)
array([[1, 1, 1],
[1, 1, 1]])
\end{verbatim}
\subsubsection{Slicing, Striding Arrays}
\begin{verbatim}
>>> a = np.array([[1,2,3], [4,5,6],
[7,8,9]])
>>> a[0,1:3]
array([2, 3])
>>> a[1:,1:]
array([[5, 6],
[8, 9]])
>>> a[:,2]
array([3, 6, 9])
>>> a[...,2]
array([3, 6, 9])
>>> a[0::2,0::2]
array([[1, 3],
[7, 9]])
# Slices are references to the
# same memory!
\end{verbatim}
\subsubsection{Random Numbers}
\begin{verbatim}
>>> np.random.rand(3,2)
array([[ 0.96276665, 0.77174861],
[ 0.35138557, 0.61462271],
[ 0.16789255, 0.43848811]])
>>> np.random.randint(1,100)
42
\end{verbatim}
\subsubsection{Problem Set}
\begin{verbatim}
>>> from scipy import misc
>>> A=misc.imread(name)
>>> misc.imshow(A)
\end{verbatim}
\begin{enumerate}
\item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $.
\item Scale the image to 50\%.
\item Introduce some random noise.
\item Smooth the image using a mean filter.
\\\small{Each element in the array is replaced by mean of all the neighbouring elements}
\\\small{How fast does your code run?}
\end{enumerate}
\subsection{2D Plotting}
\subsubsection{Basic 2D Plotting}
\begin{verbatim}
$ ipython -pylab
>>> x = linspace(0, 2*pi, 1000)
>>> plot(x, sin(x))
>>> plot(x, sin(x), 'ro')
>>> xlabel(r'$\chi$', color='g')
# LaTeX markup!
>>> ylabel(r'sin($\chi$)', color='r')
>>> title('Simple figure', fontsize=20)
>>> savefig('/tmp/test.eps')
\end{verbatim}
\subsubsection{Tweaking plots}
\begin{verbatim}
# Set properties of objects:
>>> l, = plot(x, sin(x))
# Why "l,"?
>>> setp(l, linewidth=2.0, color='r')
>>> l.set_linewidth(2.0)
>>> draw() # Redraw.
>>> setp(l) # Print properties.
>>> clf() # Clear figure.
>>> close() # Close figure.
\end{verbatim}
\subsubsection{Working with text}
\begin{verbatim}
>>> w = arange(-2,2,.1)
>>> plot(w,exp(-(w*w))*cos)
>>> ylabel('$f(\omega)$')
>>> xlabel('$\omega$')
>>> title(r"$f(\omega)=e^{-\omega^2}
cos({\omega^2})$")
>>> annotate('maxima',xy=(0, 1),
xytext=(1, 0.8),
arrowprops=dict(
facecolor='black',
shrink=0.05))
\end{verbatim}
\subsubsection{Legends}
\begin{verbatim}
>>> x = linspace(0, 2*np.pi, 1000)
>>> plot(x, cos(5*x), 'r--',
label='cosine')
>>> plot(x, sin(5*x), 'g--',
label='sine')
>>> legend()
# Or use:
>>> legend(['cosine', 'sine'])
\end{verbatim}
\subsubsection{Multiple figures}
\begin{verbatim}
>>> figure(1)
>>> plot(x, sin(x))
>>> figure(2)
>>> plot(x, tanh(x))
>>> figure(1)
>>> title('Easy as 1,2,3')
\end{verbatim}
\subsubsection{Problem Set}
\begin{enumerate}
\item Write a function that plots any regular n-gon given n.
\item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
$k=2.5, 3.5$ and $4$ in the same plot.
\item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
kx(1-x)$. Plot the successive iterates of this process.
\item Plot this using a cobweb plot as follows:
\begin{enumerate}
\item Start at $(x_0, 0)$
\item Draw line to $(x_i, f(x_i))$;
\item Set $x_{i+1} = f(x_i)$
\item Draw line to $(x_i, x_i)$
\item Repeat from 2 for as long as you want
\end{enumerate}
\end{enumerate}
\section{Advanced Numpy}
\subsection{Broadcasting}
\begin{verbatim}
>>> a = np.arange(4)
>>> b = np.arange(5)
>>> a+b #Does this work?
>>> a+3
>>> c=np.array([3])
>>> a+c #Works!
>>> b+c #But how?
>>> a.shape, b.shape, c.shape
>>> a = np.arange(4)
>>> a+3
array([3, 4, 5, 6])
>>> x = np.ones((3, 5))
>>> y = np.ones(8)
>>> (x[..., None] + y).shape
(3, 5, 8)
\end{verbatim}
\subsection{Copies \& Views}
\begin{verbatim}
>>> a = np.array([[1,2,3],[4,5,6]])
>>> b = a
>>> b is a
>>> b[0,0]=0; print a
>>> c = a.view()
>>> c is a
>>> c.base is a
>>> c.flags.owndata
>>> d = a.copy()
>>> d.base is a
>>> d.flags.owndata
>>> a = np.arange(1,9)
>>> a.shape=3,3
>>> b = a[0,1:3]
>>> c = a[0::2,0::2]
>>> a.flags.owndata
>>> b.flags.owndata
>>> b.base
>>> c.base is a
>>> b = a[np.array([0,1,2])]
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> b.flags.owndata
>>> abool=np.greater(a,2)
>>> c = a[abool]
>>> c.flags.owndata
\end{verbatim}
\section{Scipy}
\subsection{Linear Algebra}
\begin{verbatim}
>>> import scipy as sp
>>> from scipy import linalg
>>> A=sp.mat(np.arange(1,10))
>>> A.shape=3,3
>>> linalg.inv(A)
>>> linalg.det(A)
>>> linalg.norm(A)
>>> linalg.expm(A) #logm
>>> linalg.sinm(A) #cosm, tanm, ...
>>> A = sp.mat(np.arange(1,10))
>>> A.shape=3,3
>>> linalg.lu(A)
>>> linalg.eig(A)
>>> linalg.eigvals(A)
\end{verbatim}
\subsection{Solving Linear Equations}
\begin{align*}
3x + 2y - z & = 1 \\
2x - 2y + 4z & = -2 \\
-x + \frac{1}{2}y -z & = 0
\end{align*}
To Solve this,
\begin{verbatim}
>>> A = sp.mat([[3,2,-1],[2,-2,4]
,[-1,1/2,-1]])
>>> B = sp.mat([[1],[-2],[0]])
>>> linalg.solve(A,B)
\end{verbatim}
\subsection{Integrate}
\subsubsection{Quadrature}
Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
\begin{verbatim}
>>> def f(x):
return np.sin(x)+x**2
>>> integrate.quad(f, 0, 1)
\end{verbatim}
\subsubsection{ODE Integration}
Numerically solve ODEs\\
\begin{align*}
\frac{dx}{dt} &=-e^{-t}x^2\\
x(0) &=2
\end{align*}
\begin{verbatim}
>>> def dx_dt(x,t):
... return -np.exp(-t)*x**2
>>> x=integrate.odeint(dx_dt, 2, t)
>>> plt.plot(x,t)
\end{verbatim}
\subsection{Interpolation}
\subsubsection{1D Interpolation}
\begin{verbatim}
>>> from scipy import interpolate
>>> interpolate.interp1d?
>>> x = np.arange(0,2*np.pi,np.pi/4)
>>> y = np.sin(x)
>>> fl = interpolate.interp1d(
x,y,kind='linear')
>>> fc = interpolate.interp1d(
x,y,kind='cubic')
>>> fl(np.pi/3)
>>> fc(np.pi/3)
\end{verbatim}
\subsubsection{Splines}
Plot the Cubic Spline of $sin(x)$
\begin{verbatim}
>>> x = np.arange(0,2*np.pi,np.pi/4)
>>> y = np.sin(x)
>>> tck = interpolate.splrep(x,y)
>>> X = np.arange(0,2*np.pi,np.pi/50)
>>> Y = interpolate.splev(X,tck,der=0)
>>> plt.plot(x,y,'o',x,y,X,Y)
>>> plt.show()
\end{verbatim}
\subsection{Signal \& Image Processing}
Applying a simple median filter
\begin{verbatim}
>>> from scipy import signal, ndimage
>>> from scipy import lena
>>> A=lena().astype('float32')
>>> B=signal.medfilt2d(A)
>>> imshow(B)
\end{verbatim}
Zooming an array - uses spline interpolation
\begin{verbatim}
>>> b=ndimage.zoom(A,0.5)
>>> imshow(b)
\end{verbatim}
\section{3D Data Visualization}
\subsection{Using mlab}
\begin{verbatim}
>>> from enthought.mayavi import mlab
>>> mlab.test_<TAB>
>>> mlab.test_contour3d()
>>> mlab.test_contour3d??
\end{verbatim}
\subsubsection{Plotting Functions}
\begin{verbatim}
>>> from numpy import *
>>> t = linspace(0, 2*pi, 50)
>>> u = cos(t)*pi
>>> x, y, z = sin(u), cos(u), sin(t)
>>> x = mgrid[-3:3:100j,-3:3:100j]
>>> z = sin(x*x + y*y)
>>> mlab.surf(x, y, z)
\end{verbatim}
\subsubsection{Large 2D Data}
\begin{verbatim}
>>> mlab.mesh(x, y, z)
>>> phi, theta = numpy.mgrid[0:pi:20j,
... 0:2*pi:20j]
>>> x = sin(phi)*cos(theta)
>>> y = sin(phi)*sin(theta)
>>> z = cos(phi)
>>> mlab.mesh(x, y, z,
... representation=
... 'wireframe')
\end{verbatim}
\subsubsection{Large 3D Data}
\begin{verbatim}
>>> x, y, z = ogrid[-5:5:64j,
... -5:5:64j,
... -5:5:64j]
>>> mlab.contour3d(x*x*0.5 + y*y +
z*z*2)
>>> mlab.test_quiver3d()
\end{verbatim}
\subsection{Motivational Problem}
Atmospheric data of temperature over the surface of the earth. Let temperature ($T$) vary linearly with height ($z$)\\
$T = 288.15 - 6.5z$
\begin{verbatim}
lat = linspace(-89, 89, 37)
lon = linspace(0, 360, 37)
z = linspace(0, 100, 11)
\end{verbatim}
\begin{verbatim}
x, y, z = mgrid[0:360:37j,-89:89:37j,
0:100:11j]
t = 288.15 - 6.5*z
mlab.contour3d(x, y, z, t)
mlab.outline()
mlab.colorbar()
\end{verbatim}
\subsection{Lorenz equation}
\begin{eqnarray*}
\frac{d x}{dt} &=& s (y-x)\\
\frac{d y}{d t} &=& rx -y -xz\\
\frac{d z}{d t} &=& xy - bz\\
\end{eqnarray*}
Let $s=10,$
$r=28,$
$b=8./3.$
\begin{verbatim}
x, y, z = mgrid[-50:50:20j,-50:50:20j,
-10:60:20j]
\end{verbatim}
\end{document}
|