# HG changeset patch # User Santosh G. Vattam # Date 1256639768 -19800 # Node ID 7288d3867df2692019e403ebac4f1602b432eef8 # Parent a31b0e76c0fb9eee27b5c421d35846838d670152# Parent baf196170c082cebd31cd0fbb13e104029240453 Manual merge of branches. diff -r a31b0e76c0fb -r 7288d3867df2 day1/data/all_regions.png Binary file day1/data/all_regions.png has changed diff -r a31b0e76c0fb -r 7288d3867df2 day1/data/science.png Binary file day1/data/science.png has changed diff -r a31b0e76c0fb -r 7288d3867df2 day1/exercise/kwfreq.py --- a/day1/exercise/kwfreq.py Tue Oct 27 15:26:52 2009 +0530 +++ b/day1/exercise/kwfreq.py Tue Oct 27 16:06:08 2009 +0530 @@ -1,5 +1,5 @@ import keyword -f = open('/path/to/file') +f = open('amicable.py') freq = {} for line in f: @@ -7,8 +7,10 @@ for word in words: key = word.strip(',.!;?()[]: ') if keyword.iskeyword(key): - value = freq[key] - freq[key] = value + 1 + if key in freq: + freq[key] += 1 + else: + freq[key] = 1 print freq diff -r a31b0e76c0fb -r 7288d3867df2 day1/session1.tex --- a/day1/session1.tex Tue Oct 27 15:26:52 2009 +0530 +++ b/day1/session1.tex Tue Oct 27 16:06:08 2009 +0530 @@ -72,7 +72,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[]{Interactive Plotting} +\title[Interactive Plotting]{Python for Science and Engg: Interactive Plotting} \author[FOSSEE] {FOSSEE} @@ -161,7 +161,7 @@ \begin{columns} \column{0.25\textwidth} \hspace*{-0.5in} - \includegraphics[height=2in, interpolate=true]{data/firstplot} + \includegraphics[height=2in, interpolate=true]{data/firstplot} \column{0.8\textwidth} \begin{block}{} \small @@ -177,10 +177,14 @@ \begin{frame}[fragile] \frametitle{Walkthrough} -\begin{block}{\typ{linspace(start, stop, num)} } +\begin{block}{\typ{x = linspace(start, stop, num)} } returns \typ{num} evenly spaced points, in the interval [\typ{start}, \typ{stop}]. \end{block} -\vspace*{.5in} +\begin{lstlisting} +x[0] = start +x[num - 1] = end +\end{lstlisting} +\vspace*{.35in} \begin{block}{\typ{plot(x, y)}} plots \typ{x} and \typ{y} using default line style and color \end{block} @@ -221,7 +225,7 @@ In []: xlabel('y') In []: ylabel('sin(2y)') \end{lstlisting} -\emphbar{By default plots would be overlayed!} +\emphbar{By default plots would be overlaid!} \end{frame} \begin{frame}[fragile] @@ -243,45 +247,42 @@ \end{frame} \begin{frame}[fragile] -\frametitle{Changing Legend Placement} -\begin{columns} - \column{0.6\textwidth} +\frametitle{Legend Placement} + \begin{block}{} \small \begin{lstlisting} -In []: legend(['sin(2y)'], - loc='center') +In []: legend(['sin(2y)'], loc='center') \end{lstlisting} - \small \end{block} + +\begin{columns} + \column{0.6\textwidth} \includegraphics[height=2in, interpolate=true]{data/position} +\begin{lstlisting} +'best', 'right', 'center' +\end{lstlisting} \column{0.45\textwidth} \vspace{-0.2in} \begin{lstlisting} -Location String -=============== -'best' 'upper right' 'upper left' 'lower left' 'lower right' -'right' 'center left' 'center right' 'lower center' 'upper center' -'center' \end{lstlisting} \end{columns} \end{frame} \begin{frame}[fragile] -\frametitle{For placing legend at $(x,y)$} + \frametitle{For arbitrary location} \vspace*{-0.1in} \begin{lstlisting} In []: legend(['sin(2y)'], loc=(.8,.1)) -#(x,y) is position of lower-left -#corner of legend box. +# Specify south-east corner position \end{lstlisting} %\vspace*{-0.2in} \begin{center} @@ -299,7 +300,7 @@ \end{frame} \begin{frame}[fragile] -\frametitle{Plotting seprate figures} +\frametitle{Plotting separate figures} \begin{lstlisting} In []: figure(1) In []: plot(y, sin(y)) @@ -396,9 +397,10 @@ \begin{frame} \frametitle{Things we have learned} \begin{itemize} - \item Creating plots. - \item Handling labels and legends. - \item Changing properties of plot. + \item Creating simple plots. + \item Adding labels and legends. + \item Annotating plots. + \item Changing the looks: size, linewidth \end{itemize} \end{frame} \begin{frame}[fragile] diff -r a31b0e76c0fb -r 7288d3867df2 day1/session2.tex --- a/day1/session2.tex Tue Oct 27 15:26:52 2009 +0530 +++ b/day1/session2.tex Tue Oct 27 16:06:08 2009 +0530 @@ -73,7 +73,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Plotting using Python]{Plotting experimental data\\} +\title[Plotting using Python]{Python for Science and Egg. Plotting experimental data} \author[FOSSEE] {FOSSEE} @@ -126,16 +126,17 @@ \begin{frame} \frametitle{Why we didn't close the IPython??} \begin{itemize} - \item Because all the command history is lost - \item We can go back, edit, and re-execute our commands + \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} \end{frame} \begin{frame} \frametitle{But its impractical..} \begin{itemize} - \item Because we can't always keep running the IPython shell for days - \item And lets admit it, its a pain to go back and edit + \item We can't keep running IPython for days + \item And its a pain to go back and edit \end{itemize} And the solution is..\\ \begin{center} @@ -143,12 +144,12 @@ \end{center} \end{frame} -\section{Creating and running scripts} +\section{Scripts} \begin{frame}[fragile] \frametitle{Python Scripts} \begin{itemize} -\item Let us now put all the commands used in the review problem into a file. -\item The following commands of IPython help us do this. +\item Put all commands used in review problem into a file. +\item use hist command of IPython. \end{itemize} \begin{lstlisting} In []: %hist @@ -169,9 +170,8 @@ \end{frame} \begin{frame}[fragile] -\frametitle{How often do we plot analytical functions?} -Plotting experimental data is done more often and also more useful.\\ -Let us look at a small example: +\frametitle{Why would I plot f(x)?} +How often do we plot analytical functions?\\We plot experimental data more. \begin{lstlisting} In []: x = [0, 1, 2, 3] @@ -183,9 +183,15 @@ \end{frame} \begin{frame}[fragile] +\begin{figure} +\includegraphics[width=3.5in]{data/straightline.png} +\end{figure} +\end{frame} + +\begin{frame}[fragile] \frametitle{Plotting points} \begin{itemize} -\item What if we want to plot points! +\item What if we want to plot the points! \end{itemize} \begin{lstlisting} In []: clf() @@ -211,7 +217,7 @@ \section{Lists} \begin{frame}[fragile] - \frametitle{How to create?} + \frametitle{How to create the data?} What are \typ{x} and \typ{y} here??\\ \begin{center} \alert{\typ{lists!!}} @@ -233,6 +239,9 @@ \begin{frame}[fragile] \frametitle{List: Slicing} + \begin{block}{Remember\ldots} + \kwrd{In []: lst = [1,2,3,4,5]} + \end{block} \alert{\typ{list[initial:final:step]}} \begin{lstlisting} In []: lst[1:3] # A slice. @@ -244,7 +253,7 @@ \end{frame} \begin{frame}[fragile] - \frametitle{List concatenation and list methods} + \frametitle{List operations} \begin{lstlisting} In []: anthrlst = [6,7,8,9] In []: lnglst = lst + anthrlst @@ -318,7 +327,7 @@ \end{frame} \begin{frame}[fragile] -\frametitle{\texttt{for}} +\frametitle{More of \texttt{for}} \begin{itemize} \item Used to iterate over lists \item Let us look at another example. diff -r a31b0e76c0fb -r 7288d3867df2 day1/session3.tex --- a/day1/session3.tex Tue Oct 27 15:26:52 2009 +0530 +++ b/day1/session3.tex Tue Oct 27 16:06:08 2009 +0530 @@ -127,13 +127,19 @@ %% \end{frame} \begin{frame} + \frametitle{More on data processing} + \begin{block}{} + What do we do if we want to draw Pie charts for the data in a huge data file? + \end{block} +\end{frame} + + +\begin{frame} \frametitle{Statistical Analysis and Parsing} Read the data supplied in \emph{sslc1.txt} and obtain the following statistics: \begin{itemize} - \item Average total marks scored in each region - \item Subject wise average score of each region - \item \alert{??Subject wise average score for all regions combined??} - \item Find the subject wise standard deviation of scores for each region + \item Draw a pie chart representing the number of students who scored more than 90\% in Science per region. + \item Draw a pie chart representing the number of students who scored more than 90\% per subject(All regions combined). \end{itemize} \end{frame} @@ -142,7 +148,6 @@ Machinery Required - \begin{itemize} \item File reading and parsing - \item NumPy arrays - sum by rows and sum by coloumns \item Dictionaries \end{itemize} \end{frame} @@ -183,135 +188,122 @@ \begin{frame}[fragile] \frametitle{Dictionary - Building parsed data} \begin{itemize} - \item Let the parsed data be stored in dictionary \typ{data} - \item \begin{lstlisting} -data = {} # is an empty dictionary -\end{lstlisting} - \item Index of a dictionary is called a \emph{key} - \item \emph{Keys} of \typ{data} are strings - region codes - \item Value of a \emph{key} can be any Python object + \item Let the parsed data be stored in list of dictionaries. + \item d = \{\} is an empty dictionary \end{itemize} \end{frame} \begin{frame}[fragile] - \frametitle{Dictionary - Building parsed data...} + \frametitle{Dictionary - Building parsed data} +\begin{lstlisting} +ninety_percents = [{}, {}, {}, {}, {}] +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Dictionary - Building parsed data} \begin{itemize} - \item In this problem let the value of a \emph{key} be another dictionary. - \item This dictionary contains: - \begin{itemize} - \item 'marks': A \emph{List} of \emph{Lists} containing all marks - \item 'total': A \emph{List} of total marks of each student - \item 'P': Number of passes - \item 'F': Number of failures - \item 'W': Number of withdrawls - \end{itemize} + \item Index of a dictionary is called a \emph{key} + \item \emph{Keys} of these dictionaries are strings - region codes \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Dictionary - Building parsed data \ldots} - \small + \begin{itemize} + \item Value of a \emph{key} can be any legal Python value + \item In this problem let the value of a \emph{key} be another an integer + \item This dictionary contains: + \end{itemize} +'region code': Number of students who scored more than 90\% in this region for this subject +\end{frame} + +\begin{frame}[fragile] + \frametitle{Building parsed data \ldots} \begin{lstlisting} -data = {} +from pylab import * + +ninety_percents = [{}, {}, {}, {}, {}] + for record in open('sslc1.txt'): + record = record.strip() fields = record.split(';') - if fields[0] not in data: - data[fields[0]] = { - 'marks': [], - 'total': [], - 'P': 0, - 'F': 0, - 'W': 0 - } + + region_code = fields[0].strip() \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Dictionary - Building parsed data \ldots} + \frametitle{Building parsed data \ldots} + \small \begin{lstlisting} -marks = [] -for field in fields[3:8]: - score_str = field.strip() - score = 0 if score_str == 'AA' - or score_str == 'AAA' - or score_str == '' - else int(score_str) - marks.append(score) +for i, field in enumerate(fields[3:8]): -data[fields[0]]['marks'].append(marks) - \end{lstlisting} -\end{frame} + if region_code not in ninety_percents[i]: + ninety_percents[i][region_code] = 0 + + score_str = field.strip() -\begin{frame}[fragile] - \frametitle{Dictionary - Building parsed data \ldots} - \begin{lstlisting} -total = 0 if score_str == 'AA' - or score_str == 'AAA' - or score_str == '' - else int(fields[8]) -data[fields[0]]['total'].append(total) + score = 0 if score_str == 'AA' else + int(score_str) + if score > 90: + ninety_percents[i][region_code] += 1 \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Dictionary - Building parsed data \ldots} + \frametitle{Consolidating data} \begin{lstlisting} -pfw_key = fields[9] - or fields[10] - or 'F' -data[fields[0]][pfw_key] += 1 - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{NumPy arrays} - \centerline{\alert{But I lied!?!?!?}} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Calculations} - \begin{lstlisting} -for k in data: - data[k]['marks'] = array( - data[k]['marks']) - data[k]['total'] = array( - data[k]['total']) +subj_total = [] +for subject in ninety_percents: + subj_total.append(sum( + subject.values())) \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Calculations} + \frametitle{Pie charts} \small \begin{lstlisting} - data[k]['avg'] = average( - data[k]['total']) - marks = data[k]['marks'] - sub_avg = average(marks, axis=1) - sub_std = sqrt(sum(square( - sub_avg[:,newaxis] - marks), axis=0) / - len(marks)) - data[k]['sub_avg'] = sub_avg - data[k]['sub_std'] = sub_std +figure(1) +pie(ninety_percents[4].values(), + labels=ninety_percents[1].keys()) +title('Students scoring 90% and above + in science by region') +savefig('/tmp/science.png') + \end{lstlisting} +\begin{columns} + \column{5.25\textwidth} + \hspace*{1.1in} +\includegraphics[height=2in, interpolate=true]{data/science} + \column{0.8\textwidth} +\end{columns} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Pie charts} + \begin{lstlisting} +figure(2) +pie(subj_total, labels=['English', + 'Hindi', 'Maths', 'Science', + 'Social']) +title('Students scoring more than + 90% by subject(All regions + combined).') +savefig('/tmp/all_regions.png') \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{New Concepts} - \begin{itemize} - \item Dictionaries - \item Slicing lists - \item New type of conditional - \item NumPy arrays - \item Slicing NumPy arrays - \item NumPy array functions - square, average, sqrt - \end{itemize} + \frametitle{Pie charts} + \includegraphics[height=3in, interpolate=true]{data/all_regions} \end{frame} \begin{frame}[fragile] \frametitle{Dealing with data whole-sale} \begin{lstlisting} In []: for t in T: - ....: Tsq.append(t*t) + ....: TSq.append(t*t) \end{lstlisting} \begin{itemize} \item This is not very efficient @@ -321,7 +313,7 @@ \begin{lstlisting} In []: L = array(L) In []: T = array(T) -In []: Tsq = T*T +In []: TSq = T*T \end{lstlisting} \end{frame} @@ -409,7 +401,7 @@ \item Along with a lot of things, it returns the least squares solution \end{itemize} \begin{lstlisting} -In []: coef, res, r, s = lstsq(A,Tsq) +In []: coef, res, r, s = lstsq(A,TSq) \end{lstlisting} \end{frame} @@ -427,4 +419,14 @@ \end{lstlisting} \end{frame} +\begin{frame}[fragile] + \frametitle{What did we learn?} + \begin{itemize} + \item Dictionaries + \item Drawing pie charts + \item Arrays + \item Least Square fitting + \item Intro to Matrices + \end{itemize} +\end{frame} \end{document} diff -r a31b0e76c0fb -r 7288d3867df2 day1/session4.tex --- a/day1/session4.tex Tue Oct 27 15:26:52 2009 +0530 +++ b/day1/session4.tex Tue Oct 27 16:06:08 2009 +0530 @@ -255,7 +255,7 @@ \end{itemize} \begin{lstlisting} In []: x = 0 -In []: integrate.quad(sin(x)+x**2, 0, 1) +In []: quad(sin(x)+x**2, 0, 1) \end{lstlisting} \alert{\typ{error:}} \typ{First argument must be a callable function.} @@ -266,7 +266,7 @@ \begin{lstlisting} In []: def f(x): return sin(x)+x**2 -In []: integrate.quad(f, 0, 1) +In []: quad(f, 0, 1) \end{lstlisting} \begin{itemize} \item \typ{def} @@ -338,9 +338,13 @@ \begin{frame}[fragile] \frametitle{Quadrature \ldots} \begin{lstlisting} -In []: integrate.quad(f, 0, 1) +In []: quad(f, 0, 1) \end{lstlisting} Returns the integral and an estimate of the absolute error in the result. +\begin{itemize} +\item Use \typ{dblquad} for Double integrals +\item Use \typ{tplquad} for Triple integrals +\end{itemize} \end{frame} \subsection{ODEs} @@ -403,5 +407,25 @@ \end{lstlisting} \end{frame} +\begin{frame} + \frametitle{Things we have learned} + \begin{itemize} + \item + \item + \item Functions + \begin{itemize} + \item Definition + \item Calling + \item Default Arguments + \item Keyword Arguments + \end{itemize} + \item Integration + \begin{itemize} + \item Quadrature + \item ODEs + \end{itemize} + \end{itemize} +\end{frame} + \end{document}