diff options
author | Santosh G. Vattam | 2009-10-31 01:20:28 +0530 |
---|---|---|
committer | Santosh G. Vattam | 2009-10-31 01:20:28 +0530 |
commit | 79b878309603adbf49b7fac9bd4f2fa3379da065 (patch) | |
tree | 2ff657d8338ce425bec9e0a88b29a34cfee2ad4b | |
parent | da363caede7e7f4fbdf50218e46432a94b8555c0 (diff) | |
download | workshops-79b878309603adbf49b7fac9bd4f2fa3379da065.tar.gz workshops-79b878309603adbf49b7fac9bd4f2fa3379da065.tar.bz2 workshops-79b878309603adbf49b7fac9bd4f2fa3379da065.zip |
Loads of changes done.
-rw-r--r-- | day1/session2.tex | 140 | ||||
-rw-r--r-- | day1/session4.tex | 212 |
2 files changed, 219 insertions, 133 deletions
diff --git a/day1/session2.tex b/day1/session2.tex index 150a1a3..0473129 100644 --- a/day1/session2.tex +++ b/day1/session2.tex @@ -1,4 +1,4 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Tutorial slides on Python. % % Author: FOSSEE @@ -124,12 +124,9 @@ \end{frame} \begin{frame} -\frametitle{Why we didn't close the IPython??} -\begin{itemize} - \item IPython provides a convenient feature - \item To go back, edit, and re-run commands - \item But when you close, this is lost -\end{itemize} +\frametitle{Why we didn't close IPython?} + IPython provides a convenient feature to go back, edit, and re-run commands.\\ + \alert{But when you close, all this is lost.} \end{frame} \begin{frame} @@ -148,7 +145,7 @@ And the solution is..\\ \begin{frame}[fragile] \frametitle{Python Scripts} \begin{itemize} -\item Put all commands used in review problem into a file. +\item Put commands used in review problem into file. \item use hist command of IPython. \end{itemize} \begin{lstlisting} @@ -161,17 +158,18 @@ And the solution is..\\ \frametitle{Python Scripts\ldots} \begin{itemize} \item Open a new file in an \alert{editor} - \item Copy and paste required lines from the output of \typ{\%hist -n} + \item Copy and paste from the output of \typ{\%hist -n} \item Save the file as \typ{sine_plot.py} \end{itemize} \begin{itemize} - \item run the file in IPython using \typ{\%run sine_plot.py}\\ + \item run the file in IPython using \typ{\%run -i sine_plot.py}\\ \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Why would I plot f(x)?} How often do we plot analytical functions?\\We plot experimental data more. +\begin{small} \begin{lstlisting} In []: x = [0, 1, 2, 3] @@ -179,14 +177,21 @@ In []: y = [7, 11, 15, 19] In []: plot(x, y) Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>] + +In []: xlabel('X') +Out[]: <matplotlib.text.Text object at 0x986e9ac> + +In []: ylabel('Y') +Out[]: <matplotlib.text.Text object at 0x98746ec> \end{lstlisting} +\end{small} \end{frame} \begin{frame}[fragile] \begin{figure} \includegraphics[width=3.5in]{data/straightline.png} \end{figure} -\alert{Is this what you have??} +\alert{Is this what you have?} \end{frame} \begin{frame}[fragile] @@ -197,11 +202,11 @@ Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>] \begin{lstlisting} In []: clf() - In []: plot(L, TSq, 'o') + In []: plot(x, y, 'o') Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>] In []: clf() - In []: plot(L, TSq, '.') + In []: plot(x, y, '.') Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>] \end{lstlisting} \end{frame} @@ -216,8 +221,8 @@ Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>] \begin{frame}[fragile] \frametitle{Additional Plotting Attributes} \begin{itemize} - \item \kwrd{'o'} - Dots - \item \kwrd{'.'} - Smaller Dots + \item \kwrd{'o'} - Filled circles + \item \kwrd{'.'} - Small Dots \item \kwrd{'-'} - Lines \item \kwrd{'- -'} - Dashed lines \end{itemize} @@ -226,14 +231,14 @@ Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>] \section{Lists} \begin{frame}[fragile] \frametitle{How to create the data?} -What were \typ{x} and \typ{y}??\\ +What were \typ{x} and \typ{y}?\\ \begin{center} \alert{\typ{lists!!}} \end{center} \begin{lstlisting} In []: mtlist = [] #Empty List -In []: lst = [1,2,3,4,5] +In []: lst = [ 1, 2, 3, 4, 5] \end{lstlisting} \end{frame} @@ -248,9 +253,8 @@ Out[]: 8 \begin{frame}[fragile] \frametitle{List: Slicing} \begin{block}{Remember\ldots} - \kwrd{In []: lst = [1,2,3,4,5]} + \kwrd{In []: lst = [ 1, 2, 3, 4, 5]} \end{block} -\alert{\typ{list[initial:final:step]}} \begin{lstlisting} In []: lst[1:3] # A slice. Out[]: [2, 3] @@ -258,12 +262,13 @@ Out[]: [2, 3] In []: lst[1:-1] Out[]: [2, 3] \end{lstlisting} +\alert{\typ{list[initial:final]}} \end{frame} \begin{frame}[fragile] \frametitle{List operations} \begin{lstlisting} -In []: anthrlst = [6,7,8,9] +In []: anthrlst = [ 6, 7, 8, 9] In []: lnglst = lst + anthrlst In []: lnglst @@ -271,7 +276,7 @@ Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In []: lst.append(6) In []: lst -Out[]: [1, 2, 3, 4, 5, 6] +Out[]: [ 1, 2, 3, 4, 5, 6] \end{lstlisting} %\inctime{10} \end{frame} @@ -332,7 +337,7 @@ In []: for t in T: In []: plot(L, TSq) Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>] \end{lstlisting} -This gives the list \kwrd{TSq} which is the list of squares of T values. +This gives \kwrd{TSq} which is the list of squares of T values. \end{frame} \begin{frame}[fragile] @@ -342,31 +347,11 @@ This gives the list \kwrd{TSq} which is the list of squares of T values. \end{frame} \begin{frame}[fragile] -\frametitle{More of \texttt{for}} -\begin{itemize} -\item Used to iterate over lists -\item Let us look at another example. -\end{itemize} -\begin{lstlisting} -In []: lst = [1,2,3,4,5,6] -In []: for num in lst: - ....: print num, num*num - ....: -1 1 -2 4 -3 9 -4 16 -5 25 -6 36 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{What about larger data sets??} +\frametitle{What about larger data sets?} \alert{Data is usually present in a file!} \\ -Lets look at the pendulum.txt file. +Lets look at the \typ{pendulum.txt} file. \begin{lstlisting} -$cat data/pendulum.txt +$ cat pendulum.txt 1.0000e-01 6.9004e-01 1.1000e-01 6.9497e-01 1.2000e-01 7.4252e-01 @@ -378,18 +363,16 @@ $cat data/pendulum.txt \end{frame} \begin{frame}[fragile] -\frametitle{Reading pendulum.txt} +\frametitle{Reading \typ{pendulum.txt}} \begin{itemize} - \item We now wish to repeat the plot using the values from a file - \item Given a file containing L vs. T values - \item Column1 - L; Column2 - T - \item Read the file - \item Plot points for L vs. $T^2$ + \item Let us generate a plot from the data file + \item File contains L vs. T values + \item L - Column1; T - Column2 \end{itemize} \end{frame} \begin{frame}[fragile] -\frametitle{Reading pendulum.txt} +\frametitle{Reading \typ{pendulum.txt}} \begin{lstlisting} In []: L = [] In []: T = [] @@ -400,12 +383,12 @@ In []: for line in open('pendulum.txt'): \end{lstlisting} \begin{itemize} \item We now have two lists L and T -\item Now, Repeat previous steps for plotting +\item Now, repeat previous steps for plotting \end{itemize} \end{frame} \begin{frame}[fragile] -\frametitle{Plotting from pendulum.txt} +\frametitle{Plotting from \typ{pendulum.txt}} \begin{lstlisting} In []: TSq = [] @@ -426,9 +409,9 @@ In []: plot(L, TSq, '.') \frametitle{Reading files \ldots} \typ{In []: for line in open('pendulum.txt'):} \begin{itemize} -\item opening file `pendulum.txt' -\item iterating through the file by reading each line into variable \typ{line} -\item \typ{line} is a \kwrd{string} variable +\item opening file `\typ{pendulum.txt}' +\item reading the file line by line +\item \typ{line} is a \kwrd{string} \end{itemize} \end{frame} @@ -447,9 +430,9 @@ Anything within ``quotes'' is a string! \begin{frame}[fragile] \frametitle{Strings and \typ{split()}} \begin{lstlisting} -In []: line = 'hello world' +In []: greet = 'hello world' -In []: line.split() +In []: greet.split() Out[]: ['hello', 'world'] \end{lstlisting} This is what happens with \typ{line} @@ -476,13 +459,42 @@ Out[]: <type 'float'> \end{lstlisting} \end{frame} +\begin{frame}[fragile] +\frametitle{Let's review the code} +\begin{small} +\begin{lstlisting} +In []: L = [] +In []: T = [] +In []: for line in open('pendulum.txt'): + .... points = line.split() + .... L.append(float(points[0])) + .... T.append(float(points[1])) + +In []: TSq = [] + +In []: for t in T: + ....: TSq.append(t*t) + +In []: plot(L, TSq, '.') +\end{lstlisting} +\end{small} +\end{frame} + +\begin{frame}[fragile] +\begin{figure} +\includegraphics[width=3.5in]{data/L-Tsq.png} +\end{figure} +\end{frame} + \section {Summary} -\begin{frame} -\frametitle{Summary} -So what did we learn in this session?? +\begin{frame}[fragile] +\frametitle{What did we learn?} \begin{itemize} - \item Creating and running Python scripts - \item Plotting points and Plotting attributes + \item \kwrd{\%hist -n} + \item Python scripts + \item \kwrd{\%run -i} + \item Plotting points + \item Plot attributes \item Lists \item \kwrd{for} \item Reading files diff --git a/day1/session4.tex b/day1/session4.tex index 4c0e913..7b1cde3 100644 --- a/day1/session4.tex +++ b/day1/session4.tex @@ -128,24 +128,24 @@ \begin{frame} \frametitle{Matrices: Introduction} -We looked at the Van der Monde matrix in the previous session,\\ -let us now look at matrices in a little more detail. +Let us now look at matrices in detail.\\ +\alert{All matrix operations are done using \kwrd{arrays}} \end{frame} \subsection{Initializing} \begin{frame}[fragile] \frametitle{Matrices: Initializing} \begin{lstlisting} -In []: A = matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +In []: A = array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) In []: A Out[]: -matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) \end{lstlisting} \end{frame} @@ -154,19 +154,19 @@ matrix([[ 1, 1, 2, -1], \begin{frame}[fragile] \frametitle{Transpose of a Matrix} \begin{lstlisting} -In []: linalg.transpose(A) +In []: A.T Out[]: -matrix([[ 1, 2, 2, 1], - [ 1, 5, 1, -3], - [ 2, -1, -1, 2], - [-1, -9, 3, 7]]) +array([[ 1, 2, 2, 1], + [ 1, 5, 1, -3], + [ 2, -1, -1, 2], + [-1, -9, 3, 7]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Sum of all elements} \begin{lstlisting} -In []: linalg.sum(A) +In []: sum(A) Out[]: 12 \end{lstlisting} \end{frame} @@ -174,41 +174,56 @@ Out[]: 12 \begin{frame}[fragile] \frametitle{Matrix Addition} \begin{lstlisting} -In []: B = matrix([[3,2,-1,5], - [2,-2,4,9], - [-1,0.5,-1,-7], - [9,-5,7,3]]) -In []: linalg.add(A,B) +In []: B = array([[3,2,-1,5], + [2,-2,4,9], + [-1,0.5,-1,-7], + [9,-5,7,3]]) +In []: A + B Out[]: -matrix([[ 4. , 3. , 1. , 4. ], - [ 4. , 3. , 3. , 0. ], - [ 1. , 1.5, -2. , -4. ], - [ 10. , -8. , 9. , 10. ]]) +array([[ 4. , 3. , 1. , 4. ], + [ 4. , 3. , 3. , 0. ], + [ 1. , 1.5, -2. , -4. ], + [ 10. , -8. , 9. , 10. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] +\frametitle{Elementwise Multiplication} +\begin{lstlisting} +In []: A*B +Out[]: +array([[ 3. , 2. , -2. , -5. ], + [ 4. , -10. , -4. , -81. ], + [ -2. , 0.5, 1. , -21. ], + [ 9. , 15. , 14. , 21. ]]) + +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] \frametitle{Matrix Multiplication} \begin{lstlisting} -In []: linalg.multiply(A, B) +In []: dot(A,B) Out[]: -matrix([[ 3. , 2. , -2. , -5. ], - [ 4. , -10. , -4. , -81. ], - [ -2. , 0.5, 1. , -21. ], - [ 9. , 15. , 14. , 21. ]]) +array([[ -6. , 6. , -6. , -3. ], + [-64. , 38.5, -44. , 35. ], + [ 36. , -13.5, 24. , 35. ], + [ 58. , -26. , 34. , -15. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Inverse of a Matrix} +\begin{lstlisting} +In []: inv(A) +\end{lstlisting} \begin{small} \begin{lstlisting} -In []: linalg.inv(A) Out[]: -matrix([[-0.5 , 0.55, -0.15, 0.7 ], - [ 0.75, -0.5 , 0.5 , -0.75], - [ 0.5 , -0.15, -0.05, -0.1 ], - [ 0.25, -0.25, 0.25, -0.25]]) +array([[-0.5 , 0.55, -0.15, 0.7 ], + [ 0.75, -0.5 , 0.5 , -0.75], + [ 0.5 , -0.15, -0.05, -0.1 ], + [ 0.25, -0.25, 0.25, -0.25]]) \end{lstlisting} \end{small} \end{frame} @@ -217,24 +232,24 @@ matrix([[-0.5 , 0.55, -0.15, 0.7 ], \frametitle{Determinant} \begin{lstlisting} In []: det(A) -Out[66]: 80.0 +Out[]: 80.0 \end{lstlisting} \end{frame} \begin{frame}[fragile] -\frametitle{Eigen Values and Eigen Matrix} +\frametitle{Eigenvalues and Eigen Vectors} \begin{small} \begin{lstlisting} -In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]]) +In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) -In []: linalg.eig(E) +In []: eig(E) Out[]: (array([-1., 8., -1.]), - matrix([[-0.74535599, 0.66666667, -0.1931126 ], + array([[-0.74535599, 0.66666667, -0.1931126 ], [ 0.2981424 , 0.33333333, -0.78664085], [ 0.59628479, 0.66666667, 0.58643303]])) -In []: linalg.eigvals(E) +In []: eigvals(E) Out[]: array([-1., 8., -1.]) \end{lstlisting} \end{small} @@ -243,23 +258,23 @@ Out[]: array([-1., 8., -1.]) \begin{frame}[fragile] \frametitle{Computing Norms} \begin{lstlisting} -In []: linalg.norm(E) +In []: norm(E) Out[]: 8.1240384046359608 \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Single Value Decomposition} + \frametitle{Singular Value Decomposition} \begin{small} \begin{lstlisting} -In [76]: linalg.svd(E) -Out[76]: -(matrix( +In []: svd(E) +Out[]: +(array( [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), array([ 8., 1., 1.]), - matrix([[-0.66666667, -0.33333333, -0.66666667], + array([[-0.66666667, -0.33333333, -0.66666667], [-0. , 0.89442719, -0.4472136 ], [-0.74535599, 0.2981424 , 0.59628479]])) \end{lstlisting} @@ -289,12 +304,12 @@ Solution: \frametitle{Solving using Matrices} Let us now look at how to solve this using \kwrd{matrices} \begin{lstlisting} - In []: A = matrix([[3,2,-1], - [2,-2,4], - [-1, 0.5, -1]]) - In []: b = matrix([[1], [-2], [0]]) - In []: x = linalg.solve(A, b) - In []: Ax = dot(A, x) + In []: A = array([[3,2,-1], + [2,-2,4], + [-1, 0.5, -1]]) + In []: b = array([[1], [-2], [0]]) + In []: x = solve(A, b) + In []: Ax = dot(A,x) \end{lstlisting} \end{frame} @@ -314,9 +329,9 @@ array([[ 1.], \begin{lstlisting} In []: Ax Out[]: -matrix([[ 1.00000000e+00], - [ -2.00000000e+00], - [ 2.22044605e-16]]) +array([[ 1.00000000e+00], + [ -2.00000000e+00], + [ 2.22044605e-16]]) \end{lstlisting} \begin{block}{} The last term in the matrix is actually \alert{0}!\\ @@ -332,7 +347,74 @@ Out[]: True \subsection{Exercises} \begin{frame}[fragile] -\frametitle{Problem Set 4: Problem 4.1} +\frametitle{Problem 1} +Given the matrix:\\ +\begin{center} +\begin{bmatrix} +-2 & 2 & 3\\ + 2 & 1 & 6\\ +-1 &-2 & 0\\ +\end{bmatrix} +\end{center} +Find: +\begin{itemize} + \item[i] Transpose + \item[ii]Inverse + \item[iii]Determinant + \item[iv] Eigenvalues and Eigen vectors + \item[v] Singular Value decomposition +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 2} +Given +\begin{center} +A = +\begin{bmatrix} +-3 & 1 & 5 \\ +1 & 0 & -2 \\ +5 & -2 & 4 \\ +\end{bmatrix} +, B = +\begin{bmatrix} +0 & 9 & -12 \\ +-9 & 0 & 20 \\ +12 & -20 & 0 \\ +\end{bmatrix} +\end{center} +Find: +\begin{itemize} + \item[i] Sum of A and B + \item[ii]Elementwise Product of A and B + \item[iii] Matrix product of A and B +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +Sum: +\begin{bmatrix} +-3 & 10 & 7 \\ +-8 & 0 & 18 \\ +17 & -22 & 4 \\ +\end{bmatrix} +,\\ Elementwise Product: +\begin{bmatrix} +0 & 9 & -60 \\ +-9 & 0 & -40 \\ +60 & 40 & 0 \\ +\end{bmatrix} +,\\ Matrix product: +\begin{bmatrix} +51 & -127 & 56 \\ +-24 & 49 & -12 \\ +66 & -35 & -100 \\ +\end{bmatrix} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 3} Solve the set of equations: \begin{align*} x + y + 2z -w & = 3\\ @@ -345,26 +427,18 @@ Solve the set of equations: \begin{frame}[fragile] \frametitle{Solution} -Solution: -\begin{lstlisting} +Use \kwrd{solve()} \begin{align*} x & = -5\\ y & = 2\\ z & = 3\\ w & = 0\\ \end{align*} -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Problem 4.2} - \end{frame} \section{Summary} \begin{frame} - \frametitle{Summary} -So what did we learn?? + \frametitle{What did we learn??} \begin{itemize} \item Matrices \begin{itemize} @@ -373,9 +447,9 @@ So what did we learn?? \item Multiplication \item Inverse of a matrix \item Determinant - \item Eigen values and Eigen matrix + \item Eigenvalues and Eigen vector \item Norms - \item Single Value Decomposition + \item Singular Value Decomposition \end{itemize} \item Solving linear equations \end{itemize} |