# HG changeset patch # User Santosh G. Vattam # Date 1268061333 -19800 # Node ID 682b6f66fe117e3ed1d5ac9d5d7bf8869173b021 # Parent 2299700a8b970240659900bbabcd0a1358f46862 Updated for day1 of GRD workshop. diff -r 2299700a8b97 -r 682b6f66fe11 day1/day1quiz1.tex --- a/day1/day1quiz1.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/day1quiz1.tex Mon Mar 08 20:45:33 2010 +0530 @@ -55,6 +55,7 @@ \item Name: \item University/College/Company: \item Student/Teacher/Professional: + \item Field of interest/study: \end{itemize} \end{frame} @@ -204,17 +205,17 @@ In []: marks = [10, 20, 30, 50, 55, 75, 83] \end{lstlisting} -Given the above marks, how will you calculate the \alert{mean} and -\alert{standard deviation}? +Given the above marks, how will you calculate the \alert{mean}?%% and +%% \alert{standard deviation}? \end{frame} -\begin{frame}[fragile] -\frametitle{\incqno } -\begin{lstlisting} -In []: marks = [10, 20, 30, 50, 55, - 75, 83] -\end{lstlisting} -How will you convert the list \texttt{marks} to an \alert{array}? -\end{frame} +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: marks = [10, 20, 30, 50, 55, +%% 75, 83] +%% \end{lstlisting} +%% How will you convert the list \texttt{marks} to an \alert{array}? +%% \end{frame} \end{document} diff -r 2299700a8b97 -r 682b6f66fe11 day1/day1quiz2.tex --- a/day1/day1quiz2.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/day1quiz2.tex Mon Mar 08 20:45:33 2010 +0530 @@ -58,6 +58,7 @@ \item Name: \item University/College/Company: \item Student/Teacher/Professional: + \item Field of interest/study: \end{itemize} \end{frame} @@ -170,10 +171,10 @@ \texttt{Ax = b}? \end{frame} -\begin{frame} -\frametitle{\incqno } -What command will you use if you wish to integrate a system of ODEs? -\end{frame} +%% \begin{frame} +%% \frametitle{\incqno } +%% What command will you use if you wish to integrate a system of ODEs? +%% \end{frame} \begin{frame} \frametitle{\incqno } diff -r 2299700a8b97 -r 682b6f66fe11 day1/session1.tex --- a/day1/session1.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session1.tex Mon Mar 08 20:45:33 2010 +0530 @@ -154,24 +154,24 @@ \frametitle{Checklist} \begin{enumerate} \item IPython - \item Editor + \item Editor - we recommend \alert{scite} \item Data files: \begin{itemize} - \item \typ{sslc1.txt} - \item \typ{pendulum.txt} - \item \typ{points.txt} - \item \typ{pos.txt} - \item \typ{holmes.txt} + \item \typ{sslc/sslc1.txt} + \item \typ{pendulum/pendulum.txt} + \item \typ{motion-plot/pos.txt} + \item \typ{word-freq/holmes.txt} + \item \typ{anagrams/anag.txt} \end{itemize} \item Python scripts: \begin{itemize} - \item \typ{sslc_allreg.py} - \item \typ{sslc_science.py} + \item \typ{sslc/sslc_allreg.py} + \item \typ{sslc/sslc_science.py} \end{itemize} \item Images \begin{itemize} - \item \typ{lena.png} - \item \typ{smoothing.gif} + \item \typ{lena/lena.png} + \item \typ{smoothing/smoothing.gif} \end{itemize} \end{enumerate} \end{frame} @@ -210,8 +210,6 @@ >>> ^D(Ctrl-D) $ \end{lstlisting} %$ -\alert{Note the change in the prompt}\\ -\alert{IPython provides many additional features}\\ \end{frame} \section{Starting up Ipython} @@ -231,13 +229,16 @@ In []: ^D(Ctrl-D) Do you really want to exit([y]/n)? y \end{lstlisting} -\alert{IPython is an improved interpreter}\\ -\alert{Recommended for use in Scientific Computing work}\\ +An alternative to IPython is bpython \end{frame} \section{Ipython with magic} \begin{frame}[fragile] \frametitle{Let the magic begin \ldots} +\begin{itemize} +\item \alert{IPython is an improved interpreter}\\ +\item \alert{Recommended for Scientific and Computing work}\\ +\end{itemize} \begin{block}{} \begin{lstlisting} $ ipython -pylab @@ -397,6 +398,17 @@ In []: close() \end{lstlisting} +Supported formats to store images: +\begin{itemize} +\item png +\item eps - Easy to embed in Latex files +\item emf +\item pdf +\item ps +\item raw +\item rgba +\item svg +\end{itemize} \end{frame} \section{Multiple plots} @@ -459,16 +471,23 @@ \end{frame} \begin{frame}[fragile] -\frametitle{Axes lengths} -\emphbar{Get the axes limits} +\frametitle{Getting axes lengths} \begin{lstlisting} -In []: xmin, xmax = xlim() -In []: ymin, ymax = ylim() +In []: orig_xmin, orig_xmax = xlim() +In []: orig_ymin, orig_ymax = ylim() \end{lstlisting} -\emphbar{Set the axes limits} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Set the axes limits} \begin{lstlisting} -In []: xlim(xmin, 2*pi) -In []: ylim(ymin-0.2, ymax+0.2) +In []: up_xmin = orig_xmin +In []: up_xmax = 2*pi +In []: up_ymin = ymin-0.2 +In []: up_ymax = ymax+0.2 + +In []: xlim(up_xmin, up_xmax) +In []: ylim(up_ymin, up_ymax) \end{lstlisting} \end{frame} @@ -516,40 +535,55 @@ \end{frame} \begin{frame}[fragile] -\frametitle{Saving Commands} -Save commands of review problem into file -\begin{itemize} -\item Use \typ{\%hist} command of IPython -\item Identify the required line numbers -\item Then, use \typ{\%save} command of IPython -\end{itemize} +\frametitle{Command History} +Use the \typ{\%hist} \alert{magic} command of IPython \typ{In []: \%hist}\\ -\typ{In []: \%save four_plot.py} \alert{\typ{16 18-27}} +This displays all the commands typed in so far aka Command History. \begin{block}{Careful about errors!} - \kwrd{\%hist} will contain the errors as well,\\ - so be careful while selecting line numbers. + \kwrd{\%hist} will contain the errors as well.\\ +\end{block} +\begin{block}{Magic Commands?} + Magic commands are commands provided by IPython to make our life easier. \end{block} \end{frame} +\begin{frame}[fragile] + \frametitle{Saving commands into script} +Use the \typ{\%save} \alert{magic} command of IPython +\begin{block}{} +\typ{In []: \%save script_name line_numbers} +\end{block} +Line numbers can be specified individually separated by commas or as a range separated by a dash.\\ +\begin{block}{} +\typ{In []: \%save four_plot.py} \alert{\typ{16 18-27}} \\ +\end{block} +This saves from the history the commands entered on line numbers \alert{16, 18, 19, 20, \ldots 27} +\end{frame} + \begin{frame} \frametitle{Python Scripts\ldots} - This is called a Python Script. +Now, four\_plot.py is called a Python Script. \begin{itemize} - \item run the script in IPython using \typ{\%run -i four_plot.py}\\ + \item run the script in IPython using \typ{\%run four_plot.py}\\ \end{itemize} +\pause +\alert{\typ{NameError: name 'linspace' is not defined}} +\begin{block}{} +To avoid this, run using \alert{\typ{\%run -i four_plot.py}}\\ +\end{block} \end{frame} \begin{frame}[fragile] \frametitle{What did we learn?} \begin{itemize} \item Starting up IPython - \item \kwrd{\%hist} - History of commands - \item \kwrd{\%save} - Saving commands - \item Running a script using \kwrd{\%run -i} \item Creating simple plots. \item Adding labels and legends. \item Annotating plots. \item Changing the looks: size, linewidth + \item \kwrd{\%hist} - History of commands + \item \kwrd{\%save} - Saving commands + \item Running a script using \kwrd{\%run -i} \end{itemize} \end{frame} diff -r 2299700a8b97 -r 682b6f66fe11 day1/session2.tex --- a/day1/session2.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session2.tex Mon Mar 08 20:45:33 2010 +0530 @@ -210,6 +210,9 @@ \begin{lstlisting} In []: p = [ 2, 3, 5, 7] +In []: p[1] +Out[]: 3 + In []: p[0]+p[1]+p[-1] Out[]: 12 \end{lstlisting} @@ -301,19 +304,12 @@ \begin{lstlisting} In []: tsq = [] -In []: len(l) -Out[]: 9 - -In []: len(t) -Out[]: 9 - In []: for time in t: ....: tsq.append(time*time) ....: ....: -In []: plot(l, tsq) -Out[]: [] +In []: print len(l), len(t), len(tsq) \end{lstlisting} This gives \kwrd{tsq} which is the list of squares of \typ{t} values. \end{frame} @@ -327,7 +323,6 @@ ....: ....: - In []: print tsq, len(tsq) In []: plot(l, tsq) \end{lstlisting} \end{frame} @@ -343,26 +338,23 @@ \alert{Data is usually present in a file!} \\ Lets look at the \typ{pendulum.txt} file. \begin{lstlisting} -$ cat pendulum.txt +In []: cd circulate/pendulum/ +In []: cat pendulum.txt 1.0000e-01 6.9004e-01 1.1000e-01 6.9497e-01 1.2000e-01 7.4252e-01 1.3000e-01 7.5360e-01 -1.4000e-01 8.3568e-01 -1.5000e-01 8.6789e-01 \end{lstlisting} %$ \ldots -\begin{block}{Windows users:} - C:> type pendulum.txt -\end{block} \end{frame} \begin{frame}[fragile] \frametitle{Reading \typ{pendulum.txt}} \begin{itemize} + \item File contains L vs. T values + \item First Column - L values + \item Second Column - T values \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} diff -r 2299700a8b97 -r 682b6f66fe11 day1/session3.tex --- a/day1/session3.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session3.tex Mon Mar 08 20:45:33 2010 +0530 @@ -73,7 +73,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Statistics]{Python for Science and Engg: Statistics} +\title[Statistics]{Python for Scienc and Engg: Statistics} \author[FOSSEE] {FOSSEE} @@ -152,15 +152,8 @@ \end{lstlisting} \end{frame} -\begin{frame} - \frametitle{Computing mean ``g''} - \begin{block}{Exercise} - Obtain the mean of ``g'' - \end{block} -\end{frame} - \begin{frame}[fragile] - \frametitle{Mean ``g''} + \frametitle{Mean ``g'' - Classical method} \begin{lstlisting} In []: total = 0 In []: for g in g_list: @@ -173,7 +166,7 @@ \end{frame} \begin{frame}[fragile] - \frametitle{Mean ``g''} + \frametitle{Mean ``g'' - Slightly improved method} \begin{lstlisting} In []: g_mean = sum(g_list) / len(g_list) In []: print 'Mean: ', g_mean @@ -181,7 +174,7 @@ \end{frame} \begin{frame}[fragile] - \frametitle{Mean ``g''} + \frametitle{Mean ``g'' - One liner} \begin{lstlisting} In []: g_mean = mean(g_list) In []: print 'Mean: ', g_mean @@ -215,7 +208,7 @@ \item Region Code \item Roll Number \item Name - \item Marks of 5 subjects: English, Hindi, Maths, Science, Social + \item Marks of 5 subjects: SLang, Flang Maths, Science, Social \item Total marks \item Pass/Fail (P/F) \item Withheld (W) @@ -257,30 +250,33 @@ \subsection{Data processing} \begin{frame}[fragile] \frametitle{File reading and parsing \ldots} +\emphbar{Reading files line by line is the same as we had done with the pendulum example.} + \begin{lstlisting} for record in open('sslc1.txt'): fields = record.split(';') \end{lstlisting} -\begin{block}{} -\centerline{Recall pendulum example!} -\end{block} \end{frame} \subsection{Dictionaries} \begin{frame}[fragile] \frametitle{Dictionaries: Introduction} \begin{itemize} - \item lists index: 0 \ldots n - \item dictionaries index using strings + \item Lists index using integers\\ +Recall \typ{p = [2, 3, 5, 7]} and\\ +\typ{p[1]} is equal to \typ{3} + \item Dictionaries index using strings \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Dictionaries \ldots} \begin{lstlisting} -In []: d = {'jpg' : 'image file', +In []: d = {'png' : 'image file', 'txt' : 'text file', - 'py' : 'python code'} + 'py' : 'python code' + 'java': 'bad code', + 'cpp': 'complex code'} In []: d['txt'] Out[]: 'text file' @@ -293,7 +289,7 @@ In []: 'py' in d Out[]: True -In []: 'cpp' in d +In []: 'jpg' in d Out[]: False \end{lstlisting} \end{frame} @@ -366,6 +362,14 @@ \subsection{Visualizing data} \begin{frame}[fragile] + \frametitle{Pie Chart} + \begin{lstlisting} + pie(science.values()) + \end{lstlisting} +\includegraphics[height=2in, interpolate=true]{data/science_nolabel} +\end{frame} + +\begin{frame}[fragile] \frametitle{Pie chart} \small \begin{lstlisting} @@ -408,13 +412,6 @@ \subsection{Obtaining statistics} \begin{frame}[fragile] \frametitle{Obtaining statistics} - \begin{block}{Exercise} - Obtain the mean of Math scores - \end{block} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Obtaining statistics} \begin{lstlisting} print 'Mean: ', mean(math_scores) diff -r 2299700a8b97 -r 682b6f66fe11 day1/session4.tex --- a/day1/session4.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session4.tex Mon Mar 08 20:45:33 2010 +0530 @@ -164,7 +164,7 @@ array([[ 1., 0.], [ 0., 1.]]) \end{lstlisting} -Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} +Also available \alert{\typ{zeros, zeros_like}} \end{small} \end{frame} @@ -182,8 +182,6 @@ Out[]: 23 In []: c[1,2] Out[]: 23 -In []: c[1] -Out[]: array([21, 22, 23]) \end{lstlisting} \end{frame} @@ -191,6 +189,9 @@ \frametitle{Changing elements} \begin{small} \begin{lstlisting} +In []: c[1] +Out[]: array([21, 22, 23]) + In []: c[1,1] = -22 In []: c Out[]: @@ -206,7 +207,7 @@ [31, 32, 33]]) \end{lstlisting} \end{small} -How to change one \alert{column}? +How to access one \alert{column}? \end{frame} \begin{frame}[fragile] @@ -294,23 +295,28 @@ \end{frame} \begin{frame}[fragile] - \frametitle{Slicing \& Striding Exercises} + \frametitle{Elementary image processing} \begin{small} \begin{lstlisting} In []: a = imread('lena.png') In []: imshow(a) Out[]: + \end{lstlisting} + \end{small} +\typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\ +\typ{imshow} renders the array as an image. +\end{frame} - \end{lstlisting} -\end{small} +\begin{frame}[fragile] +\frametitle{Slicing \& Striding Exercises} \begin{itemize} \item Crop the image to get the top-left quarter \item Crop the image to get only the face \item Resize image to half by dropping alternate pixels \end{itemize} + \end{frame} - \begin{frame}[fragile] \frametitle{Solutions} \begin{small} @@ -345,14 +351,6 @@ \end{frame} \begin{frame}[fragile] - \frametitle{Sum of all elements} - \begin{lstlisting} -In []: sum(a) -Out[]: 12 - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] \frametitle{Matrix Addition} \begin{lstlisting} In []: b = array([[3,2,-1,5], @@ -410,11 +408,16 @@ \end{frame} \begin{frame}[fragile] -\frametitle{Determinant} +\frametitle{Determinant and sum of all elements} \begin{lstlisting} In []: det(a) Out[]: 80.0 \end{lstlisting} + \begin{lstlisting} +In []: sum(a) +Out[]: 12 + \end{lstlisting} + \end{frame} %%use S=array(X,Y) @@ -467,7 +470,8 @@ \section{Least Squares Fit} \begin{frame}[fragile] \frametitle{$L$ vs. $T^2$ - Scatter} -\vspace{-0.15in} +Linear trend visible. +\vspace{-0.1in} \begin{figure} \includegraphics[width=4in]{data/L-Tsq-points} \end{figure} @@ -475,37 +479,24 @@ \begin{frame}[fragile] \frametitle{$L$ vs. $T^2$ - Line} -\vspace{-0.15in} +This line does not make any mathematical sense. +\vspace{-0.1in} \begin{figure} \includegraphics[width=4in]{data/L-Tsq-Line} \end{figure} \end{frame} \begin{frame}[fragile] -\frametitle{$L$ vs. $T^2$ } \frametitle{$L$ vs. $T^2$ - Least Square Fit} -\vspace{-0.15in} +This is what our intention is. +\vspace{-0.1in} \begin{figure} \includegraphics[width=4in]{data/least-sq-fit} \end{figure} \end{frame} -\begin{frame} -\frametitle{Least Square Fit Curve} -\begin{center} -\begin{itemize} -\item $L \alpha T^2$ -\item Best Fit Curve $\rightarrow$ Linear - \begin{itemize} - \item Least Square Fit - \end{itemize} -\item \typ{lstsq()} -\end{itemize} -\end{center} -\end{frame} - \begin{frame}[fragile] -\frametitle{\typ{lstsq}} +\frametitle{Matrix Formulation} \begin{itemize} \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ \item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, where $T_{sq}$ is @@ -535,11 +526,11 @@ \frametitle{Getting $L$ and $T^2$} %If you \alert{closed} IPython after session 2 \begin{lstlisting} -In []: l = [] +In []: L = [] In []: t = [] In []: for line in open('pendulum.txt'): .... point = line.split() - .... l.append(float(point[0])) + .... L.append(float(point[0])) .... t.append(float(point[1])) .... .... @@ -549,7 +540,7 @@ \begin{frame}[fragile] \frametitle{Getting $L$ and $T^2$ \dots} \begin{lstlisting} -In []: l = array(l) +In []: L = array(L) In []: t = array(t) \end{lstlisting} \alert{\typ{In []: tsq = t*t}} @@ -558,7 +549,7 @@ \begin{frame}[fragile] \frametitle{Generating $A$} \begin{lstlisting} -In []: A = array([l, ones_like(l)]) +In []: A = array([L, ones_like(L)]) In []: A = A.T \end{lstlisting} %% \begin{itemize} @@ -586,13 +577,15 @@ \frametitle{Least Square Fit Line \ldots} We get the points of the line from \typ{coef} \begin{lstlisting} -In []: Tline = coef[0]*l + coef[1] +In []: Tline = coef[0]*L + coef[1] + +In []: Tline.shape \end{lstlisting} \begin{itemize} -\item Now plot \typ{Tline} vs. \typ{l}, to get the Least squares fit line. +\item Now plot \typ{Tline} vs. \typ{L}, to get the Least squares fit line. \end{itemize} \begin{lstlisting} -In []: plot(l, Tline) +In []: plot(L, Tline) \end{lstlisting} \end{frame} diff -r 2299700a8b97 -r 682b6f66fe11 day1/session5.tex --- a/day1/session5.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session5.tex Mon Mar 08 20:45:33 2010 +0530 @@ -151,14 +151,17 @@ \begin{columns} \column{0.5\textwidth} \hspace*{-0.5in} - \includegraphics[height=2in, interpolate=true]{data/points} + \includegraphics[height=2in, interpolate=true]{data/triangle} \column{0.45\textwidth} \begin{block}{Line between two points} \tiny \begin{lstlisting} -In []: x = [1, 5] -In []: y = [1, 4] +In []: x = [3, 1] +In []: y = [2, -3] +In []: z = [-2, 4] In []: plot(x, y) +In []: plot(y, z) +In []: plot(x, z) \end{lstlisting} \end{block} \end{columns} diff -r 2299700a8b97 -r 682b6f66fe11 day1/session6.tex --- a/day1/session6.tex Fri Mar 05 23:59:12 2010 +0530 +++ b/day1/session6.tex Mon Mar 08 20:45:33 2010 +0530 @@ -311,6 +311,25 @@ \end{center} \end{frame} +\begin{frame}[fragile] + \frametitle{Exercise Problem} + Find the root of the equation $x^2 - sin(x) + cos^2(x)$ nearest to $0$ +\end{frame} + +\begin{frame}[fragile] + \frametitle{Solution} + \begin{small} + \begin{lstlisting} + def f(x): + return x**2 - sin(x) + cos(x)*cos(x) + fsolve(f, 0) + \end{lstlisting} + \end{small} + \begin{center} +\includegraphics[height=2in, interpolate=true]{data/fsolve_tanx} + \end{center} +\end{frame} + %% \begin{frame}[fragile] %% \frametitle{Scipy Methods \dots} %% \begin{small}