diff options
Diffstat (limited to 'day1/session2.tex')
-rw-r--r-- | day1/session2.tex | 321 |
1 files changed, 96 insertions, 225 deletions
diff --git a/day1/session2.tex b/day1/session2.tex index 75ce2c5..e63b3be 100644 --- a/day1/session2.tex +++ b/day1/session2.tex @@ -78,7 +78,7 @@ \author[FOSSEE group] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {SciPy 2010, Introductory tutorials,\\Day 1, Session 2} +\date[] {SciPy.in 2010, Tutorials} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -133,7 +133,7 @@ In []: time = [0, 1, 2, 3] In []: distance = [7, 11, 15, 19] -In []: plot(time,distance) +In []: plot(time, distance) Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>] In []: xlabel('time') @@ -219,50 +219,16 @@ Out[]: 12 \end{frame} \begin{frame}[fragile] - \frametitle{List: Slicing} - \begin{block}{Remember\ldots} - \kwrd{In []: p = [ 2, 3, 5, 7]} - \end{block} +\frametitle{List: Appending elements} \begin{lstlisting} -In []: p[1:3] -Out[]: [3, 5] -\end{lstlisting} -\emphbar{A slice} -\begin{lstlisting} -In []: p[0:-1] -Out[]: [2, 3, 5] -In []: p[::2] -Out[]: [2, 5] -\end{lstlisting} -\alert{\typ{list[initial:final:step]}} -\end{frame} - -\begin{frame}[fragile] - \frametitle{List: Slicing} - What is the output of the following? -\begin{lstlisting} -In []: p[1::2] - -In []: p[1:-1:2] -\end{lstlisting} -\end{frame} - - -%% more on list slicing -\begin{frame}[fragile] -\frametitle{List operations} -\begin{lstlisting} -In []: b = [ 11, 13, 17] -In []: c = p + b - -In []: c -Out[]: [2, 3, 5, 7, 11, 13, 17] - In []: p.append(11) In []: p Out[]: [ 2, 3, 5, 7, 11] + +In []: b = [11, 13, 17] +In []: p.append(b) +Out[]: [ 2, 3, 5, 7, 11, [11, 13, 17]] \end{lstlisting} -Question: Does \typ{c} change now that \typ{p} is changed? %\inctime{10} \end{frame} @@ -288,95 +254,7 @@ $L$ & $T$ & $T^2$ \\ \hline \end{small}\\ \alert{$L \alpha T^2$} \end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Lets use lists} -\begin{lstlisting} -In []: L = [0.1, 0.2, 0.3, 0.4, 0.5, - 0.6, 0.7, 0.8, 0.9] - -In []: t = [0.69, 0.90, 1.19, - 1.30, 1.47, 1.58, - 1.77, 1.83, 1.94] -\end{lstlisting} -\alert{Gotcha}: Make sure \typ{L} and \typ{t} have the same number -of elements - -\begin{lstlisting} -In []: print len(L), len(t) -\end{lstlisting} - -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting $L$ vs $T^2$} -\begin{itemize} -\item We must square each of the values in \typ{t} -\item How do we do it? -\item We use a \kwrd{for} loop to iterate over \typ{t} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Plotting $L$ vs $T^2$} -\begin{lstlisting} -In []: tsq = [] - -In []: for time in t: - ....: tsq.append(time*time) - ....: - ....: - -\end{lstlisting} -This gives \typ{tsq} which is the list of squares of \typ{t} values. -\begin{lstlisting} -In []: print len(L), len(t), len(tsq) -Out[]: 9 9 9 -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{How do you exit the \texttt{for} loop?} - Hitting the ``ENTER'' key twice returns the cursor to the previous indentation level - \begin{lstlisting} - In []: for time in t: - ....: tsq.append(time*time) - ....: - ....: - - In []: plot(L, tsq) - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\begin{figure} -\includegraphics[width=3.5in]{data/L-TSq-limited.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{What about larger data sets?} -\alert{Data is usually present in a file!} \\ -Lets look at the \typ{pendulum.txt} file. -\begin{lstlisting} -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 -\end{lstlisting} %$ -\ldots -\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 -\end{itemize} +Our data is present in \typ{pendulum.txt}. Let's look at it. \end{frame} \begin{frame}[fragile] @@ -398,132 +276,127 @@ In []: ? \end{lstlisting} \end{frame} - \begin{frame}[fragile] -\frametitle{Plotting from \typ{pendulum.txt}} -Open a new script and save as \typ{pendulum_plot.py} -\begin{lstlisting} -L = [] -t = [] -for line in open('pendulum.txt'): - point = line.split() - L.append(float(point[0])) - t.append(float(point[1])) -tsq = [] -for time in t: - tsq.append(time*time) -plot(L, tsq, '.') -\end{lstlisting} -\end{frame} - -\begin{frame} -\frametitle{Save and run} +\frametitle{Looking at \typ{pendulum.txt}} +\begin{lstlisting} +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 +\end{lstlisting} %$ +\ldots \begin{itemize} - \item Save as \typ{pendulum\_plot.py} - \item Run using \kwrd{\%run -i pendulum\_plot.py} + \item File contains L vs.\ T values + \item First Column -- L values + \item Second Column -- T values \end{itemize} \end{frame} \begin{frame}[fragile] -\begin{figure} -\includegraphics[width=3.5in]{data/L-Tsq.png} -\end{figure} + \frametitle{\typ{loadtxt}} + \begin{itemize} + \item We shall use the \typ{loadtxt} command to load data + \item Let's use \typ{primes.txt} file learn to use it + \item \typ{primes.txt} has a single column + \item Then, we shall use it for \typ{pendulum.txt} -- 2 cols + \end{itemize} \end{frame} \begin{frame}[fragile] - \frametitle{Reading files \ldots} -\typ{for line in open('pendulum.txt'):} -\begin{itemize} -\item opening file `\typ{pendulum.txt}' -\item reading the file line by line -\item \typ{line} is a \kwrd{string} -\end{itemize} +\frametitle{What's in \typ{primes.txt}?} +Lets look at the \typ{primes.txt} file. +\begin{lstlisting} +In []: cat primes.txt +2 +3 +5 +7 +11 +13 +. +. +. +\end{lstlisting} %$ \end{frame} -\section{Strings} - -\begin{frame}[fragile] -\frametitle{Strings} -Anything within ``quotes'' is a string! -\begin{lstlisting} -' This is a string ' -" This too! " -""" This one too! """ -''' And one more! ''' -\end{lstlisting} -\end{frame} \begin{frame}[fragile] -\frametitle{Strings} -Why so many? -\begin{lstlisting} -' "Do or do not. No try." said Yoda.' -" ' is a mighty lonely quote." -\end{lstlisting} -The triple quoted ones can span multiple lines! + \frametitle{\typ{loadtxt} \ldots} + \begin{lstlisting} +In []: primes = loadtxt('primes.txt') +In []: primes +Out[]: array([ 2., 3., 5., 7., 11., 13., + 17., 19., 23., 29., 31., + 37., 41., 43., 47., 53., + 59., 61., 67., 71., 73., + 79., 83., 89., 97.]) + \end{lstlisting} -\begin{lstlisting} -""" The quick brown -fox jumped over - the lazy dingbat. -""" -\end{lstlisting} \end{frame} \begin{frame}[fragile] -\frametitle{Strings and \typ{split()}} + \frametitle{Reading \typ{pendulum.txt}} \begin{lstlisting} -In []: greet = 'hello world' - -In []: greet.split() -Out[]: ['hello', 'world'] +In []: pend = loadtxt('pendulum.txt') +In []: pend \end{lstlisting} -This is what happens with \typ{line} - \begin{lstlisting} -In []: line = '1.20 7.42' - -In []: point = line.split() + \begin{itemize} + \item \typ{pend} is 2 Dimensional. + \item We don't \alert{yet} know how to handle it. + \item We obtain 1D sequences using \typ{unpack=True} + \end{itemize} -In []: point -Out[]: ['1.20', '7.42'] + \begin{lstlisting} +In []: L, T = loadtxt('pendulum.txt', + unpack=True) +In []: print L, T +In []: print len(L), len(T) +Out[]: 90 90 \end{lstlisting} \end{frame} \begin{frame}[fragile] -\frametitle{Getting floats from strings} - \begin{lstlisting} -In []: type(point[0]) -Out[]: <type 'str'> - \end{lstlisting} -But, we need floating point numbers - \begin{lstlisting} -In []: t = float(point[0]) - -In []: type(t) -Out[]: <type 'float'> - \end{lstlisting} +\frametitle{Plotting $L$ vs $T^2$} +\begin{itemize} +\item We must square each of the values in \typ{T} +\item How to do it? +\item We use a \kwrd{for} loop to iterate over \typ{T} +\end{itemize} \end{frame} \begin{frame}[fragile] -\frametitle{Let's review the code} +\frametitle{Plotting $L$ vs $T^2$} \begin{lstlisting} -L = [] -t = [] -for line in open('pendulum.txt'): - point = line.split() - L.append(float(point[0])) - t.append(float(point[1])) -tsq = [] -for time in t: - tsq.append(time*time) -plot(L, tsq, '.') +In []: tsq = [] + +In []: for time in T: + ....: tsq.append(time*time) + ....: + ....: + \end{lstlisting} +\alert{Hit ``ENTER'' key twice, to get out of \typ{for} loop}. + +\begin{lstlisting} +In []: print len(tsq) +Out[]: 90 +\end{lstlisting} +\typ{tsq} is a \kwrd{list} of squares of \typ{T} values. \end{frame} + \begin{frame}[fragile] +\frametitle{Plotting $L$ vs $T^2$ \ldots} + +\begin{lstlisting} +In []: plot(L, tsq, '.') +\end{lstlisting} + \begin{figure} -\includegraphics[width=3.5in]{data/L-Tsq.png} + \includegraphics[width=3.5in]{data/L-Tsq.png} \end{figure} + \end{frame} \section {Summary} @@ -533,9 +406,7 @@ plot(L, tsq, '.') \item Plot attributes and plotting points \item Lists \item \kwrd{for} - \item Reading files - \item Tokenizing - \item Strings + \item Loading data from files \end{itemize} \end{frame} |