|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 %Tutorial slides on Python. |
|
3 % |
|
4 % Author: FOSSEE |
|
5 % Copyright (c) 2009, FOSSEE, IIT Bombay |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[14pt,compress]{beamer} |
|
9 %\documentclass[draft]{beamer} |
|
10 %\documentclass[compress,handout]{beamer} |
|
11 %\usepackage{pgfpages} |
|
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
|
14 % Modified from: generic-ornate-15min-45min.de.tex |
|
15 \mode<presentation> |
|
16 { |
|
17 \usetheme{Warsaw} |
|
18 \useoutertheme{infolines} |
|
19 \setbeamercovered{transparent} |
|
20 } |
|
21 |
|
22 \usepackage[english]{babel} |
|
23 \usepackage[latin1]{inputenc} |
|
24 %\usepackage{times} |
|
25 \usepackage[T1]{fontenc} |
|
26 \usepackage{amsmath} |
|
27 |
|
28 % Taken from Fernando's slides. |
|
29 \usepackage{ae,aecompl} |
|
30 \usepackage{mathpazo,courier,euler} |
|
31 \usepackage[scaled=.95]{helvet} |
|
32 |
|
33 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
34 |
|
35 \usepackage{listings} |
|
36 \lstset{language=Python, |
|
37 basicstyle=\ttfamily\bfseries, |
|
38 commentstyle=\color{red}\itshape, |
|
39 stringstyle=\color{darkgreen}, |
|
40 showstringspaces=false, |
|
41 keywordstyle=\color{blue}\bfseries} |
|
42 |
|
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
44 % Macros |
|
45 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
46 \newcommand{\emphbar}[1] |
|
47 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
48 {#1} |
|
49 \end{beamercolorbox} |
|
50 } |
|
51 \newcounter{time} |
|
52 \setcounter{time}{0} |
|
53 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
54 |
|
55 \newcommand{\typ}[1]{\lstinline{#1}} |
|
56 |
|
57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
58 |
|
59 %%% This is from Fernando's setup. |
|
60 % \usepackage{color} |
|
61 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
62 % % Use and configure listings package for nicely formatted code |
|
63 % \usepackage{listings} |
|
64 % \lstset{ |
|
65 % language=Python, |
|
66 % basicstyle=\small\ttfamily, |
|
67 % commentstyle=\ttfamily\color{blue}, |
|
68 % stringstyle=\ttfamily\color{orange}, |
|
69 % showstringspaces=false, |
|
70 % breaklines=true, |
|
71 % postbreak = \space\dots |
|
72 % } |
|
73 |
|
74 |
|
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
76 % Title page |
|
77 \title[Arrays]{Python for Science and Engg: Arrays} |
|
78 |
|
79 \author[FOSSEE] {FOSSEE} |
|
80 |
|
81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
82 \date[] {SciPy 2010, Tutorials} |
|
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
84 |
|
85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
86 %\logo{\pgfuseimage{iitmlogo}} |
|
87 |
|
88 |
|
89 %% Delete this, if you do not want the table of contents to pop up at |
|
90 %% the beginning of each subsection: |
|
91 \AtBeginSubsection[] |
|
92 { |
|
93 \begin{frame}<beamer> |
|
94 \frametitle{Outline} |
|
95 \tableofcontents[currentsection,currentsubsection] |
|
96 \end{frame} |
|
97 } |
|
98 |
|
99 \AtBeginSection[] |
|
100 { |
|
101 \begin{frame}<beamer> |
|
102 \frametitle{Outline} |
|
103 \tableofcontents[currentsection,currentsubsection] |
|
104 \end{frame} |
|
105 } |
|
106 |
|
107 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
108 % the following command: |
|
109 %\beamerdefaultoverlayspecification{<+->} |
|
110 |
|
111 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
112 |
|
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
114 % DOCUMENT STARTS |
|
115 \begin{document} |
|
116 |
|
117 \begin{frame} |
|
118 \titlepage |
|
119 \end{frame} |
|
120 |
|
121 \begin{frame} |
|
122 \frametitle{Outline} |
|
123 \tableofcontents |
|
124 % \pausesections |
|
125 \end{frame} |
|
126 |
|
127 \section{Matrices} |
|
128 |
|
129 \begin{frame} |
|
130 \frametitle{Matrices: Introduction} |
|
131 \alert{All matrix operations are done using \kwrd{arrays}} |
|
132 \end{frame} |
|
133 |
|
134 \begin{frame}[fragile] |
|
135 \frametitle{Matrices: Initializing} |
|
136 \begin{lstlisting} |
|
137 In []: c = array([[11,12,13], |
|
138 [21,22,23], |
|
139 [31,32,33]]) |
|
140 |
|
141 In []: c |
|
142 Out[]: |
|
143 array([[11, 12, 13], |
|
144 [21, 22, 23], |
|
145 [31, 32, 33]]) |
|
146 \end{lstlisting} |
|
147 \end{frame} |
|
148 |
|
149 \begin{frame}[fragile] |
|
150 \frametitle{Initializing some special matrices} |
|
151 \begin{small} |
|
152 \begin{lstlisting} |
|
153 In []: ones((3,5)) |
|
154 Out[]: |
|
155 array([[ 1., 1., 1., 1., 1.], |
|
156 [ 1., 1., 1., 1., 1.], |
|
157 [ 1., 1., 1., 1., 1.]]) |
|
158 |
|
159 In []: ones_like([1, 2, 3, 4]) |
|
160 Out[]: array([1, 1, 1, 1]) |
|
161 |
|
162 In []: identity(2) |
|
163 Out[]: |
|
164 array([[ 1., 0.], |
|
165 [ 0., 1.]]) |
|
166 \end{lstlisting} |
|
167 Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} |
|
168 \end{small} |
|
169 \end{frame} |
|
170 |
|
171 |
|
172 \begin{frame}[fragile] |
|
173 \frametitle{Accessing elements} |
|
174 \begin{small} |
|
175 \begin{lstlisting} |
|
176 In []: c |
|
177 Out[]: |
|
178 array([[11, 12, 13], |
|
179 [21, 22, 23], |
|
180 [31, 32, 33]]) |
|
181 |
|
182 In []: c[1][2] |
|
183 Out[]: 23 |
|
184 In []: c[1,2] |
|
185 Out[]: 23 |
|
186 |
|
187 In []: c[1] |
|
188 Out[]: array([21, 22, 23]) |
|
189 \end{lstlisting} |
|
190 \end{small} |
|
191 \end{frame} |
|
192 |
|
193 \begin{frame}[fragile] |
|
194 \frametitle{Changing elements} |
|
195 \begin{small} |
|
196 \begin{lstlisting} |
|
197 In []: c[1,1] = -22 |
|
198 In []: c |
|
199 Out[]: |
|
200 array([[ 11, 12, 13], |
|
201 [ 21, -22, 23], |
|
202 [ 31, 32, 33]]) |
|
203 |
|
204 In []: c[1] = 0 |
|
205 In []: c |
|
206 Out[]: |
|
207 array([[11, 12, 13], |
|
208 [ 0, 0, 0], |
|
209 [31, 32, 33]]) |
|
210 \end{lstlisting} |
|
211 \end{small} |
|
212 How do you access one \alert{column}? |
|
213 \end{frame} |
|
214 |
|
215 \begin{frame}[fragile] |
|
216 \frametitle{Slicing} |
|
217 \begin{small} |
|
218 \begin{lstlisting} |
|
219 In []: c[:,1] |
|
220 Out[]: array([12, 0, 32]) |
|
221 |
|
222 In []: c[1,:] |
|
223 Out[]: array([0, 0, 0]) |
|
224 |
|
225 In []: c[0:2,:] |
|
226 Out[]: |
|
227 array([[11, 12, 13], |
|
228 [ 0, 0, 0]]) |
|
229 |
|
230 In []: c[1:3,:] |
|
231 Out[]: |
|
232 array([[ 0, 0, 0], |
|
233 [31, 32, 33]]) |
|
234 \end{lstlisting} |
|
235 \end{small} |
|
236 \end{frame} |
|
237 |
|
238 \begin{frame}[fragile] |
|
239 \frametitle{Slicing \ldots} |
|
240 \begin{small} |
|
241 \begin{lstlisting} |
|
242 In []: c[:2,:] |
|
243 Out[]: |
|
244 array([[11, 12, 13], |
|
245 [ 0, 0, 0]]) |
|
246 |
|
247 In []: c[1:,:] |
|
248 Out[]: |
|
249 array([[ 0, 0, 0], |
|
250 [31, 32, 33]]) |
|
251 |
|
252 In []: c[1:,:2] |
|
253 Out[]: |
|
254 array([[ 0, 0], |
|
255 [31, 32]]) |
|
256 \end{lstlisting} |
|
257 |
|
258 \end{small} |
|
259 \end{frame} |
|
260 |
|
261 \begin{frame}[fragile] |
|
262 \frametitle{Striding} |
|
263 \begin{small} |
|
264 \begin{lstlisting} |
|
265 In []: c[::2,:] |
|
266 Out[]: |
|
267 array([[11, 12, 13], |
|
268 [31, 32, 33]]) |
|
269 |
|
270 In []: c[:,::2] |
|
271 Out[]: |
|
272 array([[11, 13], |
|
273 [ 0, 0], |
|
274 [31, 33]]) |
|
275 |
|
276 In []: c[::2,::2] |
|
277 Out[]: |
|
278 array([[11, 13], |
|
279 [31, 33]]) |
|
280 \end{lstlisting} |
|
281 \end{small} |
|
282 \end{frame} |
|
283 |
|
284 \begin{frame}[fragile] |
|
285 \frametitle{Shape of a matrix} |
|
286 \begin{lstlisting} |
|
287 In []: c |
|
288 Out[]: |
|
289 array([[11, 12, 13], |
|
290 [ 0, 0, 0], |
|
291 [31, 32, 33]]) |
|
292 |
|
293 In []: c.shape |
|
294 Out[]: (3, 3) |
|
295 \end{lstlisting} |
|
296 \emphbar{Shape specifies shape or dimensions of a matrix} |
|
297 \end{frame} |
|
298 |
|
299 \begin{frame}[fragile] |
|
300 \frametitle{Elementary image processing} |
|
301 \begin{small} |
|
302 \begin{lstlisting} |
|
303 In []: a = imread('lena.png') |
|
304 |
|
305 In []: imshow(a) |
|
306 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc> |
|
307 \end{lstlisting} |
|
308 \end{small} |
|
309 \typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\ |
|
310 \typ{imshow} renders the array as an image. |
|
311 \end{frame} |
|
312 |
|
313 \begin{frame}[fragile] |
|
314 \frametitle{Slicing \& Striding Exercises} |
|
315 \begin{itemize} |
|
316 \item Crop the image to get the top-left quarter |
|
317 \item Crop the image to get only the face |
|
318 \item Resize image to half by dropping alternate pixels |
|
319 \end{itemize} |
|
320 |
|
321 \end{frame} |
|
322 \begin{frame}[fragile] |
|
323 \frametitle{Solutions} |
|
324 \begin{small} |
|
325 \begin{lstlisting} |
|
326 In []: imshow(a[:256,:256]) |
|
327 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
|
328 |
|
329 In []: imshow(a[200:400,200:400]) |
|
330 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
|
331 |
|
332 In []: imshow(a[::2,::2]) |
|
333 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
|
334 \end{lstlisting} |
|
335 \end{small} |
|
336 \end{frame} |
|
337 |
|
338 \begin{frame}[fragile] |
|
339 \frametitle{Transpose of a Matrix} |
|
340 \begin{lstlisting} |
|
341 In []: a = array([[ 1, 1, 2, -1], |
|
342 ...: [ 2, 5, -1, -9], |
|
343 ...: [ 2, 1, -1, 3], |
|
344 ...: [ 1, -3, 2, 7]]) |
|
345 |
|
346 In []: a.T |
|
347 Out[]: |
|
348 array([[ 1, 2, 2, 1], |
|
349 [ 1, 5, 1, -3], |
|
350 [ 2, -1, -1, 2], |
|
351 [-1, -9, 3, 7]]) |
|
352 \end{lstlisting} |
|
353 \end{frame} |
|
354 |
|
355 \begin{frame}[fragile] |
|
356 \frametitle{Matrix Addition} |
|
357 \begin{lstlisting} |
|
358 In []: b = array([[3,2,-1,5], |
|
359 [2,-2,4,9], |
|
360 [-1,0.5,-1,-7], |
|
361 [9,-5,7,3]]) |
|
362 In []: a + b |
|
363 Out[]: |
|
364 array([[ 4. , 3. , 1. , 4. ], |
|
365 [ 4. , 3. , 3. , 0. ], |
|
366 [ 1. , 1.5, -2. , -4. ], |
|
367 [ 10. , -8. , 9. , 10. ]]) |
|
368 \end{lstlisting} |
|
369 \end{frame} |
|
370 |
|
371 \begin{frame}[fragile] |
|
372 \frametitle{Elementwise Multiplication} |
|
373 \begin{lstlisting} |
|
374 In []: a*b |
|
375 Out[]: |
|
376 array([[ 3. , 2. , -2. , -5. ], |
|
377 [ 4. , -10. , -4. , -81. ], |
|
378 [ -2. , 0.5, 1. , -21. ], |
|
379 [ 9. , 15. , 14. , 21. ]]) |
|
380 |
|
381 \end{lstlisting} |
|
382 \end{frame} |
|
383 |
|
384 \begin{frame}[fragile] |
|
385 \frametitle{Matrix Multiplication} |
|
386 \begin{lstlisting} |
|
387 In []: dot(a, b) |
|
388 Out[]: |
|
389 array([[ -6. , 6. , -6. , -3. ], |
|
390 [-64. , 38.5, -44. , 35. ], |
|
391 [ 36. , -13.5, 24. , 35. ], |
|
392 [ 58. , -26. , 34. , -15. ]]) |
|
393 \end{lstlisting} |
|
394 \end{frame} |
|
395 |
|
396 \begin{frame}[fragile] |
|
397 \frametitle{Inverse of a Matrix} |
|
398 \begin{lstlisting} |
|
399 |
|
400 \end{lstlisting} |
|
401 \begin{small} |
|
402 \begin{lstlisting} |
|
403 In []: inv(a) |
|
404 Out[]: |
|
405 array([[-0.5 , 0.55, -0.15, 0.7 ], |
|
406 [ 0.75, -0.5 , 0.5 , -0.75], |
|
407 [ 0.5 , -0.15, -0.05, -0.1 ], |
|
408 [ 0.25, -0.25, 0.25, -0.25]]) |
|
409 \end{lstlisting} |
|
410 \end{small} |
|
411 \emphbar{Try this: \typ{I = dot(a, inv(a))}} |
|
412 \end{frame} |
|
413 |
|
414 \begin{frame}[fragile] |
|
415 \frametitle{Determinant and sum of all elements} |
|
416 \begin{lstlisting} |
|
417 In []: det(a) |
|
418 Out[]: 80.0 |
|
419 \end{lstlisting} |
|
420 \begin{lstlisting} |
|
421 In []: sum(a) |
|
422 Out[]: 12 |
|
423 \end{lstlisting} |
|
424 |
|
425 \end{frame} |
|
426 |
|
427 %%use S=array(X,Y) |
|
428 \begin{frame}[fragile] |
|
429 \frametitle{Eigenvalues and Eigen Vectors} |
|
430 \begin{small} |
|
431 \begin{lstlisting} |
|
432 In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) |
|
433 |
|
434 In []: eig(e) |
|
435 Out[]: |
|
436 (array([-1., 8., -1.]), |
|
437 array([[-0.74535599, 0.66666667, -0.1931126 ], |
|
438 [ 0.2981424 , 0.33333333, -0.78664085], |
|
439 [ 0.59628479, 0.66666667, 0.58643303]])) |
|
440 |
|
441 In []: eigvals(e) |
|
442 Out[]: array([-1., 8., -1.]) |
|
443 \end{lstlisting} |
|
444 \end{small} |
|
445 \end{frame} |
|
446 |
|
447 \begin{frame}[fragile] |
|
448 \frametitle{Computing Norms} |
|
449 \begin{lstlisting} |
|
450 In []: norm(e) |
|
451 Out[]: 8.1240384046359608 |
|
452 \end{lstlisting} |
|
453 \end{frame} |
|
454 |
|
455 \begin{frame}[fragile] |
|
456 \frametitle{Singular Value Decomposition} |
|
457 \begin{small} |
|
458 \begin{lstlisting} |
|
459 In []: svd(e) |
|
460 Out[]: |
|
461 (array( |
|
462 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
|
463 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
|
464 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
|
465 array([ 8., 1., 1.]), |
|
466 array([[-0.66666667, -0.33333333, -0.66666667], |
|
467 [-0. , 0.89442719, -0.4472136 ], |
|
468 [-0.74535599, 0.2981424 , 0.59628479]])) |
|
469 \end{lstlisting} |
|
470 \end{small} |
|
471 \end{frame} |
|
472 |
|
473 \section{Summary} |
|
474 \begin{frame} |
|
475 \frametitle{What did we learn?} |
|
476 \begin{itemize} |
|
477 \item Matrices |
|
478 \begin{itemize} |
|
479 \item Initializing |
|
480 \item Accessing elements |
|
481 \item Slicing and Striding |
|
482 \item Transpose |
|
483 \item Addition |
|
484 \item Multiplication |
|
485 \item Inverse of a matrix |
|
486 \item Determinant |
|
487 \item Eigenvalues and Eigen vector |
|
488 \item Singular Value Decomposition |
|
489 \end{itemize} |
|
490 \item Least Square Curve fitting |
|
491 \end{itemize} |
|
492 \end{frame} |
|
493 |
|
494 \end{document} |
|
495 |
|
496 %% Questions for Quiz %% |
|
497 %% ------------------ %% |
|
498 |
|
499 \begin{frame}[fragile] |
|
500 \frametitle{\incqno } |
|
501 \begin{lstlisting} |
|
502 In []: a = array([[1, 2], |
|
503 [3, 4]]) |
|
504 In []: a[1,0] = 0 |
|
505 \end{lstlisting} |
|
506 What is the resulting array? |
|
507 \end{frame} |
|
508 |
|
509 \begin{frame}[fragile] |
|
510 \frametitle{\incqno } |
|
511 \begin{lstlisting} |
|
512 In []: x = array(([1,2,3,4], |
|
513 [2,3,4,5])) |
|
514 In []: x[-2][-3] = 4 |
|
515 In []: print x |
|
516 \end{lstlisting} |
|
517 What will be printed? |
|
518 \end{frame} |
|
519 |
|
520 %% \begin{frame}[fragile] |
|
521 %% \frametitle{\incqno } |
|
522 %% \begin{lstlisting} |
|
523 %% In []: x = array([[1,2,3,4], |
|
524 %% [3,4,2,5]]) |
|
525 %% \end{lstlisting} |
|
526 %% What is the \lstinline+shape+ of this array? |
|
527 %% \end{frame} |
|
528 |
|
529 \begin{frame}[fragile] |
|
530 \frametitle{\incqno } |
|
531 \begin{lstlisting} |
|
532 In []: x = array([[1,2,3,4]]) |
|
533 \end{lstlisting} |
|
534 How to change \lstinline+x+ to \lstinline+array([[1,2,0,4]])+? |
|
535 \end{frame} |
|
536 |
|
537 \begin{frame}[fragile] |
|
538 \frametitle{\incqno } |
|
539 \begin{lstlisting} |
|
540 In []: x = array([[1,2,3,4], |
|
541 [3,4,2,5]]) |
|
542 \end{lstlisting} |
|
543 How do you get the following slice of \lstinline+x+? |
|
544 \begin{lstlisting} |
|
545 array([[2,3], |
|
546 [4,2]]) |
|
547 \end{lstlisting} |
|
548 \end{frame} |
|
549 |
|
550 \begin{frame}[fragile] |
|
551 \frametitle{\incqno } |
|
552 \begin{lstlisting} |
|
553 In []: x = array([[9,18,27], |
|
554 [30,60,90], |
|
555 [14,7,1]]) |
|
556 \end{lstlisting} |
|
557 What is the output of \lstinline+x[::3,::3]+ |
|
558 \end{frame} |
|
559 |
|
560 |
|
561 \begin{frame}[fragile] |
|
562 \frametitle{\incqno } |
|
563 \begin{lstlisting} |
|
564 In []: a = array([[1, 2], |
|
565 [3, 4]]) |
|
566 \end{lstlisting} |
|
567 How do you get the transpose of this array? |
|
568 \end{frame} |
|
569 |
|
570 \begin{frame}[fragile] |
|
571 \frametitle{\incqno } |
|
572 \begin{lstlisting} |
|
573 In []: a = array([[1, 2], |
|
574 [3, 4]]) |
|
575 In []: b = array([[1, 1], |
|
576 [2, 2]]) |
|
577 In []: a*b |
|
578 \end{lstlisting} |
|
579 What does this produce? |
|
580 \end{frame} |
|
581 |
|
582 \begin{frame} |
|
583 \frametitle{\incqno } |
|
584 What command do you use to find the inverse of a matrix and its |
|
585 eigenvalues? |
|
586 \end{frame} |
|
587 |
|
588 %% \begin{frame} |
|
589 %% \frametitle{\incqno } |
|
590 %% The file \lstinline+datafile.txt+ contains 3 columns of data. What |
|
591 %% command will you use to read the entire data file into an array? |
|
592 %% \end{frame} |
|
593 |
|
594 %% \begin{frame} |
|
595 %% \frametitle{\incqno } |
|
596 %% If the contents of the file \lstinline+datafile.txt+ is read into an |
|
597 %% $N\times3$ array called \lstinline+data+, how would you obtain the third |
|
598 %% column of this data? |
|
599 %% \end{frame} |
|
600 |