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[Matrices \& Curve Fitting]{Python for Science and Engg: Matrices |
|
78 \& Least Squares Fit} |
|
79 |
|
80 \author[FOSSEE] {FOSSEE} |
|
81 |
|
82 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
83 \date[] {SciPy 2010, Introductory tutorials\\Day 1, Session 4} |
|
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
85 |
|
86 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
87 %\logo{\pgfuseimage{iitmlogo}} |
|
88 |
|
89 |
|
90 %% Delete this, if you do not want the table of contents to pop up at |
|
91 %% the beginning of each subsection: |
|
92 \AtBeginSubsection[] |
|
93 { |
|
94 \begin{frame}<beamer> |
|
95 \frametitle{Outline} |
|
96 \tableofcontents[currentsection,currentsubsection] |
|
97 \end{frame} |
|
98 } |
|
99 |
|
100 \AtBeginSection[] |
|
101 { |
|
102 \begin{frame}<beamer> |
|
103 \frametitle{Outline} |
|
104 \tableofcontents[currentsection,currentsubsection] |
|
105 \end{frame} |
|
106 } |
|
107 |
|
108 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
109 % the following command: |
|
110 %\beamerdefaultoverlayspecification{<+->} |
|
111 |
|
112 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
113 |
|
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
115 % DOCUMENT STARTS |
|
116 \begin{document} |
|
117 |
|
118 \begin{frame} |
|
119 \titlepage |
|
120 \end{frame} |
|
121 |
|
122 \begin{frame} |
|
123 \frametitle{Outline} |
|
124 \tableofcontents |
|
125 % \pausesections |
|
126 \end{frame} |
|
127 |
|
128 \section{Matrices} |
|
129 |
|
130 \begin{frame} |
|
131 \frametitle{Matrices: Introduction} |
|
132 \alert{All matrix operations are done using \kwrd{arrays}} |
|
133 \end{frame} |
|
134 |
|
135 \begin{frame}[fragile] |
|
136 \frametitle{Matrices: Initializing} |
|
137 \begin{lstlisting} |
|
138 In []: c = array([[11,12,13], |
|
139 [21,22,23], |
|
140 [31,32,33]]) |
|
141 |
|
142 In []: c |
|
143 Out[]: |
|
144 array([[11, 12, 13], |
|
145 [21, 22, 23], |
|
146 [31, 32, 33]]) |
|
147 \end{lstlisting} |
|
148 \end{frame} |
|
149 |
|
150 \begin{frame}[fragile] |
|
151 \frametitle{Initializing some special matrices} |
|
152 \begin{small} |
|
153 \begin{lstlisting} |
|
154 In []: ones((3,5)) |
|
155 Out[]: |
|
156 array([[ 1., 1., 1., 1., 1.], |
|
157 [ 1., 1., 1., 1., 1.], |
|
158 [ 1., 1., 1., 1., 1.]]) |
|
159 |
|
160 In []: ones_like([1, 2, 3, 4]) |
|
161 Out[]: array([1, 1, 1, 1]) |
|
162 |
|
163 In []: identity(2) |
|
164 Out[]: |
|
165 array([[ 1., 0.], |
|
166 [ 0., 1.]]) |
|
167 \end{lstlisting} |
|
168 Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} |
|
169 \end{small} |
|
170 \end{frame} |
|
171 |
|
172 |
|
173 \begin{frame}[fragile] |
|
174 \frametitle{Accessing elements} |
|
175 \begin{small} |
|
176 \begin{lstlisting} |
|
177 In []: c |
|
178 Out[]: |
|
179 array([[11, 12, 13], |
|
180 [21, 22, 23], |
|
181 [31, 32, 33]]) |
|
182 |
|
183 In []: c[1][2] |
|
184 Out[]: 23 |
|
185 In []: c[1,2] |
|
186 Out[]: 23 |
|
187 |
|
188 In []: c[1] |
|
189 Out[]: array([21, 22, 23]) |
|
190 \end{lstlisting} |
|
191 \end{small} |
|
192 \end{frame} |
|
193 |
|
194 \begin{frame}[fragile] |
|
195 \frametitle{Changing elements} |
|
196 \begin{small} |
|
197 \begin{lstlisting} |
|
198 In []: c[1,1] = -22 |
|
199 In []: c |
|
200 Out[]: |
|
201 array([[ 11, 12, 13], |
|
202 [ 21, -22, 23], |
|
203 [ 31, 32, 33]]) |
|
204 |
|
205 In []: c[1] = 0 |
|
206 In []: c |
|
207 Out[]: |
|
208 array([[11, 12, 13], |
|
209 [ 0, 0, 0], |
|
210 [31, 32, 33]]) |
|
211 \end{lstlisting} |
|
212 \end{small} |
|
213 How do you access one \alert{column}? |
|
214 \end{frame} |
|
215 |
|
216 \begin{frame}[fragile] |
|
217 \frametitle{Slicing} |
|
218 \begin{small} |
|
219 \begin{lstlisting} |
|
220 In []: c[:,1] |
|
221 Out[]: array([12, 0, 32]) |
|
222 |
|
223 In []: c[1,:] |
|
224 Out[]: array([0, 0, 0]) |
|
225 |
|
226 In []: c[0:2,:] |
|
227 Out[]: |
|
228 array([[11, 12, 13], |
|
229 [ 0, 0, 0]]) |
|
230 |
|
231 In []: c[1:3,:] |
|
232 Out[]: |
|
233 array([[ 0, 0, 0], |
|
234 [31, 32, 33]]) |
|
235 \end{lstlisting} |
|
236 \end{small} |
|
237 \end{frame} |
|
238 |
|
239 \begin{frame}[fragile] |
|
240 \frametitle{Slicing \ldots} |
|
241 \begin{small} |
|
242 \begin{lstlisting} |
|
243 In []: c[:2,:] |
|
244 Out[]: |
|
245 array([[11, 12, 13], |
|
246 [ 0, 0, 0]]) |
|
247 |
|
248 In []: c[1:,:] |
|
249 Out[]: |
|
250 array([[ 0, 0, 0], |
|
251 [31, 32, 33]]) |
|
252 |
|
253 In []: c[1:,:2] |
|
254 Out[]: |
|
255 array([[ 0, 0], |
|
256 [31, 32]]) |
|
257 \end{lstlisting} |
|
258 |
|
259 \end{small} |
|
260 \end{frame} |
|
261 |
|
262 \begin{frame}[fragile] |
|
263 \frametitle{Striding} |
|
264 \begin{small} |
|
265 \begin{lstlisting} |
|
266 In []: c[::2,:] |
|
267 Out[]: |
|
268 array([[11, 12, 13], |
|
269 [31, 32, 33]]) |
|
270 |
|
271 In []: c[:,::2] |
|
272 Out[]: |
|
273 array([[11, 13], |
|
274 [ 0, 0], |
|
275 [31, 33]]) |
|
276 |
|
277 In []: c[::2,::2] |
|
278 Out[]: |
|
279 array([[11, 13], |
|
280 [31, 33]]) |
|
281 \end{lstlisting} |
|
282 \end{small} |
|
283 \end{frame} |
|
284 |
|
285 \begin{frame}[fragile] |
|
286 \frametitle{Shape of a matrix} |
|
287 \begin{lstlisting} |
|
288 In []: c |
|
289 Out[]: |
|
290 array([[11, 12, 13], |
|
291 [ 0, 0, 0], |
|
292 [31, 32, 33]]) |
|
293 |
|
294 In []: c.shape |
|
295 Out[]: (3, 3) |
|
296 \end{lstlisting} |
|
297 \emphbar{Shape specifies shape or dimensions of a matrix} |
|
298 \end{frame} |
|
299 |
|
300 \begin{frame}[fragile] |
|
301 \frametitle{Elementary image processing} |
|
302 \begin{small} |
|
303 \begin{lstlisting} |
|
304 In []: a = imread('lena.png') |
|
305 |
|
306 In []: imshow(a) |
|
307 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc> |
|
308 \end{lstlisting} |
|
309 \end{small} |
|
310 \typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\ |
|
311 \typ{imshow} renders the array as an image. |
|
312 \end{frame} |
|
313 |
|
314 \begin{frame}[fragile] |
|
315 \frametitle{Slicing \& Striding Exercises} |
|
316 \begin{itemize} |
|
317 \item Crop the image to get the top-left quarter |
|
318 \item Crop the image to get only the face |
|
319 \item Resize image to half by dropping alternate pixels |
|
320 \end{itemize} |
|
321 |
|
322 \end{frame} |
|
323 \begin{frame}[fragile] |
|
324 \frametitle{Solutions} |
|
325 \begin{small} |
|
326 \begin{lstlisting} |
|
327 In []: imshow(a[:256,:256]) |
|
328 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
|
329 |
|
330 In []: imshow(a[200:400,200:400]) |
|
331 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
|
332 |
|
333 In []: imshow(a[::2,::2]) |
|
334 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
|
335 \end{lstlisting} |
|
336 \end{small} |
|
337 \end{frame} |
|
338 |
|
339 \begin{frame}[fragile] |
|
340 \frametitle{Transpose of a Matrix} |
|
341 \begin{lstlisting} |
|
342 In []: a = array([[ 1, 1, 2, -1], |
|
343 ...: [ 2, 5, -1, -9], |
|
344 ...: [ 2, 1, -1, 3], |
|
345 ...: [ 1, -3, 2, 7]]) |
|
346 |
|
347 In []: a.T |
|
348 Out[]: |
|
349 array([[ 1, 2, 2, 1], |
|
350 [ 1, 5, 1, -3], |
|
351 [ 2, -1, -1, 2], |
|
352 [-1, -9, 3, 7]]) |
|
353 \end{lstlisting} |
|
354 \end{frame} |
|
355 |
|
356 \begin{frame}[fragile] |
|
357 \frametitle{Matrix Addition} |
|
358 \begin{lstlisting} |
|
359 In []: b = array([[3,2,-1,5], |
|
360 [2,-2,4,9], |
|
361 [-1,0.5,-1,-7], |
|
362 [9,-5,7,3]]) |
|
363 In []: a + b |
|
364 Out[]: |
|
365 array([[ 4. , 3. , 1. , 4. ], |
|
366 [ 4. , 3. , 3. , 0. ], |
|
367 [ 1. , 1.5, -2. , -4. ], |
|
368 [ 10. , -8. , 9. , 10. ]]) |
|
369 \end{lstlisting} |
|
370 \end{frame} |
|
371 |
|
372 \begin{frame}[fragile] |
|
373 \frametitle{Elementwise Multiplication} |
|
374 \begin{lstlisting} |
|
375 In []: a*b |
|
376 Out[]: |
|
377 array([[ 3. , 2. , -2. , -5. ], |
|
378 [ 4. , -10. , -4. , -81. ], |
|
379 [ -2. , 0.5, 1. , -21. ], |
|
380 [ 9. , 15. , 14. , 21. ]]) |
|
381 |
|
382 \end{lstlisting} |
|
383 \end{frame} |
|
384 |
|
385 \begin{frame}[fragile] |
|
386 \frametitle{Matrix Multiplication} |
|
387 \begin{lstlisting} |
|
388 In []: dot(a, b) |
|
389 Out[]: |
|
390 array([[ -6. , 6. , -6. , -3. ], |
|
391 [-64. , 38.5, -44. , 35. ], |
|
392 [ 36. , -13.5, 24. , 35. ], |
|
393 [ 58. , -26. , 34. , -15. ]]) |
|
394 \end{lstlisting} |
|
395 \end{frame} |
|
396 |
|
397 \begin{frame}[fragile] |
|
398 \frametitle{Inverse of a Matrix} |
|
399 \begin{lstlisting} |
|
400 |
|
401 \end{lstlisting} |
|
402 \begin{small} |
|
403 \begin{lstlisting} |
|
404 In []: inv(a) |
|
405 Out[]: |
|
406 array([[-0.5 , 0.55, -0.15, 0.7 ], |
|
407 [ 0.75, -0.5 , 0.5 , -0.75], |
|
408 [ 0.5 , -0.15, -0.05, -0.1 ], |
|
409 [ 0.25, -0.25, 0.25, -0.25]]) |
|
410 \end{lstlisting} |
|
411 \end{small} |
|
412 \emphbar{Try this: \typ{I = dot(a, inv(a))}} |
|
413 \end{frame} |
|
414 |
|
415 \begin{frame}[fragile] |
|
416 \frametitle{Determinant and sum of all elements} |
|
417 \begin{lstlisting} |
|
418 In []: det(a) |
|
419 Out[]: 80.0 |
|
420 \end{lstlisting} |
|
421 \begin{lstlisting} |
|
422 In []: sum(a) |
|
423 Out[]: 12 |
|
424 \end{lstlisting} |
|
425 |
|
426 \end{frame} |
|
427 |
|
428 %%use S=array(X,Y) |
|
429 \begin{frame}[fragile] |
|
430 \frametitle{Eigenvalues and Eigen Vectors} |
|
431 \begin{small} |
|
432 \begin{lstlisting} |
|
433 In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) |
|
434 |
|
435 In []: eig(e) |
|
436 Out[]: |
|
437 (array([-1., 8., -1.]), |
|
438 array([[-0.74535599, 0.66666667, -0.1931126 ], |
|
439 [ 0.2981424 , 0.33333333, -0.78664085], |
|
440 [ 0.59628479, 0.66666667, 0.58643303]])) |
|
441 |
|
442 In []: eigvals(e) |
|
443 Out[]: array([-1., 8., -1.]) |
|
444 \end{lstlisting} |
|
445 \end{small} |
|
446 \end{frame} |
|
447 |
|
448 \begin{frame}[fragile] |
|
449 \frametitle{Computing Norms} |
|
450 \begin{lstlisting} |
|
451 In []: norm(e) |
|
452 Out[]: 8.1240384046359608 |
|
453 \end{lstlisting} |
|
454 \end{frame} |
|
455 |
|
456 \begin{frame}[fragile] |
|
457 \frametitle{Singular Value Decomposition} |
|
458 \begin{small} |
|
459 \begin{lstlisting} |
|
460 In []: svd(e) |
|
461 Out[]: |
|
462 (array( |
|
463 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
|
464 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
|
465 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
|
466 array([ 8., 1., 1.]), |
|
467 array([[-0.66666667, -0.33333333, -0.66666667], |
|
468 [-0. , 0.89442719, -0.4472136 ], |
|
469 [-0.74535599, 0.2981424 , 0.59628479]])) |
|
470 \end{lstlisting} |
|
471 \end{small} |
|
472 \end{frame} |
|
473 |
|
474 \section{Least Squares Fit} |
|
475 \begin{frame}[fragile] |
|
476 \frametitle{$L$ vs. $T^2$ - Scatter} |
|
477 Linear trend visible. |
|
478 \vspace{-0.1in} |
|
479 \begin{figure} |
|
480 \includegraphics[width=4in]{data/L-Tsq-points} |
|
481 \end{figure} |
|
482 \end{frame} |
|
483 |
|
484 \begin{frame}[fragile] |
|
485 \frametitle{$L$ vs. $T^2$ - Line} |
|
486 This line does not make any mathematical sense. |
|
487 \vspace{-0.1in} |
|
488 \begin{figure} |
|
489 \includegraphics[width=4in]{data/L-Tsq-Line} |
|
490 \end{figure} |
|
491 \end{frame} |
|
492 |
|
493 \begin{frame}[fragile] |
|
494 \frametitle{$L$ vs. $T^2$ - Least Square Fit} |
|
495 This is what our intention is. |
|
496 \vspace{-0.1in} |
|
497 \begin{figure} |
|
498 \includegraphics[width=4in]{data/least-sq-fit} |
|
499 \end{figure} |
|
500 \end{frame} |
|
501 |
|
502 \begin{frame}[fragile] |
|
503 \frametitle{Matrix Formulation} |
|
504 \begin{itemize} |
|
505 \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ |
|
506 \item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, where $T_{sq}$ is |
|
507 $\begin{bmatrix} |
|
508 T^2_1 \\ |
|
509 T^2_2 \\ |
|
510 \vdots\\ |
|
511 T^2_N \\ |
|
512 \end{bmatrix}$ |
|
513 , A is |
|
514 $\begin{bmatrix} |
|
515 L_1 & 1 \\ |
|
516 L_2 & 1 \\ |
|
517 \vdots & \vdots\\ |
|
518 L_N & 1 \\ |
|
519 \end{bmatrix}$ |
|
520 and p is |
|
521 $\begin{bmatrix} |
|
522 m\\ |
|
523 c\\ |
|
524 \end{bmatrix}$ |
|
525 \item We need to find $p$ to plot the line |
|
526 \end{itemize} |
|
527 \end{frame} |
|
528 |
|
529 \begin{frame}[fragile] |
|
530 \frametitle{Getting $L$ and $T^2$} |
|
531 %If you \alert{closed} IPython after session 2 |
|
532 \begin{lstlisting} |
|
533 In []: L = [] |
|
534 In []: t = [] |
|
535 In []: for line in open('pendulum.txt'): |
|
536 .... point = line.split() |
|
537 .... L.append(float(point[0])) |
|
538 .... t.append(float(point[1])) |
|
539 .... |
|
540 .... |
|
541 \end{lstlisting} |
|
542 \end{frame} |
|
543 |
|
544 \begin{frame}[fragile] |
|
545 \frametitle{Getting $L$ and $T^2$ \dots} |
|
546 \begin{lstlisting} |
|
547 In []: L = array(L) |
|
548 In []: t = array(t) |
|
549 \end{lstlisting} |
|
550 \alert{\typ{In []: tsq = t*t}} |
|
551 \end{frame} |
|
552 |
|
553 \begin{frame}[fragile] |
|
554 \frametitle{Generating $A$} |
|
555 \begin{lstlisting} |
|
556 In []: A = array([L, ones_like(L)]) |
|
557 In []: A = A.T |
|
558 \end{lstlisting} |
|
559 %% \begin{itemize} |
|
560 %% \item A is also called a Van der Monde matrix |
|
561 %% \item It can also be generated using \typ{vander} |
|
562 %% \end{itemize} |
|
563 %% \begin{lstlisting} |
|
564 %% In []: A = vander(L, 2) |
|
565 %% \end{lstlisting} |
|
566 \end{frame} |
|
567 |
|
568 \begin{frame}[fragile] |
|
569 \frametitle{\typ{lstsq} \ldots} |
|
570 \begin{itemize} |
|
571 \item Now use the \typ{lstsq} function |
|
572 \item Along with a lot of things, it returns the least squares solution |
|
573 \end{itemize} |
|
574 \begin{lstlisting} |
|
575 In []: result = lstsq(A,tsq) |
|
576 In []: coef = result[0] |
|
577 \end{lstlisting} |
|
578 \end{frame} |
|
579 |
|
580 \begin{frame}[fragile] |
|
581 \frametitle{Least Square Fit Line \ldots} |
|
582 We get the points of the line from \typ{coef} |
|
583 \begin{lstlisting} |
|
584 In []: Tline = coef[0]*L + coef[1] |
|
585 |
|
586 In []: Tline.shape |
|
587 \end{lstlisting} |
|
588 \begin{itemize} |
|
589 \item Now plot \typ{Tline} vs. \typ{L}, to get the Least squares fit line. |
|
590 \end{itemize} |
|
591 \begin{lstlisting} |
|
592 In []: plot(L, Tline, 'r') |
|
593 |
|
594 In []: plot(L, tsq, 'o') |
|
595 \end{lstlisting} |
|
596 \end{frame} |
|
597 |
|
598 \begin{frame}[fragile] |
|
599 \frametitle{Least Squares Fit} |
|
600 \vspace{-0.15in} |
|
601 \begin{figure} |
|
602 \includegraphics[width=4in]{data/least-sq-fit} |
|
603 \end{figure} |
|
604 \end{frame} |
|
605 |
|
606 \section{Summary} |
|
607 \begin{frame} |
|
608 \frametitle{What did we learn?} |
|
609 \begin{itemize} |
|
610 \item Matrices |
|
611 \begin{itemize} |
|
612 \item Initializing |
|
613 \item Accessing elements |
|
614 \item Slicing and Striding |
|
615 \item Transpose |
|
616 \item Addition |
|
617 \item Multiplication |
|
618 \item Inverse of a matrix |
|
619 \item Determinant |
|
620 \item Eigenvalues and Eigen vector |
|
621 \item Singular Value Decomposition |
|
622 \end{itemize} |
|
623 \item Least Square Curve fitting |
|
624 \end{itemize} |
|
625 \end{frame} |
|
626 |
|
627 \end{document} |
|
628 |
|
629 %% Questions for Quiz %% |
|
630 %% ------------------ %% |
|
631 |
|
632 \begin{frame}[fragile] |
|
633 \frametitle{\incqno } |
|
634 \begin{lstlisting} |
|
635 In []: a = array([[1, 2], |
|
636 [3, 4]]) |
|
637 In []: a[1,0] = 0 |
|
638 \end{lstlisting} |
|
639 What is the resulting array? |
|
640 \end{frame} |
|
641 |
|
642 \begin{frame}[fragile] |
|
643 \frametitle{\incqno } |
|
644 \begin{lstlisting} |
|
645 In []: x = array(([1,2,3,4], |
|
646 [2,3,4,5])) |
|
647 In []: x[-2][-3] = 4 |
|
648 In []: print x |
|
649 \end{lstlisting} |
|
650 What will be printed? |
|
651 \end{frame} |
|
652 |
|
653 %% \begin{frame}[fragile] |
|
654 %% \frametitle{\incqno } |
|
655 %% \begin{lstlisting} |
|
656 %% In []: x = array([[1,2,3,4], |
|
657 %% [3,4,2,5]]) |
|
658 %% \end{lstlisting} |
|
659 %% What is the \lstinline+shape+ of this array? |
|
660 %% \end{frame} |
|
661 |
|
662 \begin{frame}[fragile] |
|
663 \frametitle{\incqno } |
|
664 \begin{lstlisting} |
|
665 In []: x = array([[1,2,3,4]]) |
|
666 \end{lstlisting} |
|
667 How to change \lstinline+x+ to \lstinline+array([[1,2,0,4]])+? |
|
668 \end{frame} |
|
669 |
|
670 \begin{frame}[fragile] |
|
671 \frametitle{\incqno } |
|
672 \begin{lstlisting} |
|
673 In []: x = array([[1,2,3,4], |
|
674 [3,4,2,5]]) |
|
675 \end{lstlisting} |
|
676 How do you get the following slice of \lstinline+x+? |
|
677 \begin{lstlisting} |
|
678 array([[2,3], |
|
679 [4,2]]) |
|
680 \end{lstlisting} |
|
681 \end{frame} |
|
682 |
|
683 \begin{frame}[fragile] |
|
684 \frametitle{\incqno } |
|
685 \begin{lstlisting} |
|
686 In []: x = array([[9,18,27], |
|
687 [30,60,90], |
|
688 [14,7,1]]) |
|
689 \end{lstlisting} |
|
690 What is the output of \lstinline+x[::3,::3]+ |
|
691 \end{frame} |
|
692 |
|
693 |
|
694 \begin{frame}[fragile] |
|
695 \frametitle{\incqno } |
|
696 \begin{lstlisting} |
|
697 In []: a = array([[1, 2], |
|
698 [3, 4]]) |
|
699 \end{lstlisting} |
|
700 How do you get the transpose of this array? |
|
701 \end{frame} |
|
702 |
|
703 \begin{frame}[fragile] |
|
704 \frametitle{\incqno } |
|
705 \begin{lstlisting} |
|
706 In []: a = array([[1, 2], |
|
707 [3, 4]]) |
|
708 In []: b = array([[1, 1], |
|
709 [2, 2]]) |
|
710 In []: a*b |
|
711 \end{lstlisting} |
|
712 What does this produce? |
|
713 \end{frame} |
|
714 |
|
715 \begin{frame} |
|
716 \frametitle{\incqno } |
|
717 What command do you use to find the inverse of a matrix and its |
|
718 eigenvalues? |
|
719 \end{frame} |
|
720 |
|
721 %% \begin{frame} |
|
722 %% \frametitle{\incqno } |
|
723 %% The file \lstinline+datafile.txt+ contains 3 columns of data. What |
|
724 %% command will you use to read the entire data file into an array? |
|
725 %% \end{frame} |
|
726 |
|
727 %% \begin{frame} |
|
728 %% \frametitle{\incqno } |
|
729 %% If the contents of the file \lstinline+datafile.txt+ is read into an |
|
730 %% $N\times3$ array called \lstinline+data+, how would you obtain the third |
|
731 %% column of this data? |
|
732 %% \end{frame} |
|
733 |
|