diff options
Diffstat (limited to 'advanced_python/slides')
-rw-r--r-- | advanced_python/slides/arrays.tex | 177 | ||||
-rw-r--r-- | advanced_python/slides/modules.tex | 27 | ||||
-rw-r--r-- | advanced_python/slides/plotting.tex | 152 |
3 files changed, 195 insertions, 161 deletions
diff --git a/advanced_python/slides/arrays.tex b/advanced_python/slides/arrays.tex index 7b829f9..b565e72 100644 --- a/advanced_python/slides/arrays.tex +++ b/advanced_python/slides/arrays.tex @@ -7,9 +7,9 @@ \item Much faster than arrays \end{itemize} \begin{lstlisting} - a1 = array([1,2,3,4]) + In []: a1 = array([1,2,3,4]) a1 # 1-D - a2 = array([[1,2,3,4],[5,6,7,8]]) + In []: a2 = array([[1,2,3,4],[5,6,7,8]]) a2 # 2-D \end{lstlisting} \end{frame} @@ -17,39 +17,38 @@ \begin{frame}[fragile] \frametitle{\texttt{arange} and \texttt{shape}} \begin{lstlisting} - ar1 = arange(1, 5) - - ar2 = arange(1, 9) - print ar2 - ar2.shape = 2, 4 - print ar2 + In []: ar1 = arange(1, 5) + In []: ar2 = arange(1, 9) + In []: print ar2 + In []: ar2.shape = 2, 4 + In []: print ar2 \end{lstlisting} \begin{itemize} \item \texttt{linspace} and \texttt{loadtxt} also returned arrays \end{itemize} \begin{lstlisting} - ar1.shape - ar2.shape + In []: ar1.shape + In []: ar2.shape \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Special methods} \begin{lstlisting} - identity(3) + In []: identity(3) \end{lstlisting} \begin{itemize} \item array of shape (3, 3) with diagonals as 1s, rest 0s \end{itemize} \begin{lstlisting} - zeros((4,5)) + In []: zeros((4,5)) \end{lstlisting} \begin{itemize} \item array of shape (4, 5) with all 0s \end{itemize} \begin{lstlisting} - a = zeros_like([1.5, 1, 2, 3]) - print a, a.dtype + In []: a = zeros_like([1.5, 1, 2, 3]) + In []: print a, a.dtype \end{lstlisting} \begin{itemize} \item An array with all 0s, with similar shape and dtype as argument @@ -61,17 +60,17 @@ \begin{frame}[fragile] \frametitle{Operations on arrays} \begin{lstlisting} - a1 - a1 * 2 - a1 + In []: a1 + In []: a1 * 2 + In []: a1 \end{lstlisting} \begin{itemize} \item The array is not changed; New array is returned \end{itemize} \begin{lstlisting} - a1 + 3 - a1 - 7 - a1 / 2.0 + In []: a1 + 3 + In []: a1 - 7 + In []: a1 / 2.0 \end{lstlisting} \end{frame} @@ -81,25 +80,25 @@ \item Like lists, we can assign the new array, the old name \end{itemize} \begin{lstlisting} - a1 = a1 + 2 - a1 + In []: a1 = a1 + 2 + In []: a1 \end{lstlisting} \begin{itemize} \item \alert{Beware of Augmented assignment!} \end{itemize} \begin{lstlisting} - a, b = arange(1, 5), arange(1, 5) - print a, a.dtype, b, b.dtype - a = a/2.0 - b /= 2.0 - print a, a.dtype, b, b.dtype + In []: a, b = arange(1, 5), arange(1, 5) + In []: print a, a.dtype, b, b.dtype + In []: a = a/2.0 + In []: b /= 2.0 + In []: print a, a.dtype, b, b.dtype \end{lstlisting} \begin{itemize} \item Operations on two arrays; element-wise \end{itemize} \begin{lstlisting} - a1 + a1 - a1 * a2 + In []: a1 + a1 + In []: a1 * a2 \end{lstlisting} \end{frame} @@ -108,24 +107,24 @@ \begin{frame}[fragile] \frametitle{Accessing \& changing elements} \begin{lstlisting} - A = array([12, 23, 34, 45, 56]) + In []: A = array([12, 23, 34, 45, 56]) - C = array([[11, 12, 13, 14, 15], + In []: C = array([[11, 12, 13, 14, 15], [21, 22, 23, 24, 25], [31, 32, 33, 34, 35], [41, 42, 43, 44, 45], [51, 52, 53, 54, 55]]) - A[2] - C[2, 3] + In []: A[2] + In []: C[2, 3] \end{lstlisting} \begin{itemize} \item Indexing starts from 0 \item Assign new values, to change elements \end{itemize} \begin{lstlisting} - A[2] = -34 - C[2, 3] = -34 + In []: A[2] = -34 + In []: C[2, 3] = -34 \end{lstlisting} \end{frame} @@ -135,49 +134,49 @@ \item Indexing works just like with lists \end{itemize} \begin{lstlisting} - C[2] - C[4] - C[-1] + In []: C[2] + In []: C[4] + In []: C[-1] \end{lstlisting} \begin{itemize} \item Change the last row into all zeros \end{itemize} \begin{lstlisting} - C[-1] = [0, 0, 0, 0, 0] + In []: C[-1] = [0, 0, 0, 0, 0] \end{lstlisting} OR \begin{lstlisting} - C[-1] = 0 + In []: C[-1] = 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Accessing columns} \begin{lstlisting} - C[:, 2] - C[:, 4] - C[:, -1] + In []: C[:, 2] + In []: C[:, 4] + In []: C[:, -1] \end{lstlisting} \begin{itemize} \item The first parameter is replaced by a \texttt{:} to specify we require all elements of that dimension \end{itemize} \begin{lstlisting} - C[:, -1] = 0 + In []: C[:, -1] = 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Slicing} \begin{lstlisting} - I = imread('squares.png') - imshow(I) + In []: I = imread('squares.png') + In []: imshow(I) \end{lstlisting} \begin{itemize} \item The image is just an array \end{itemize} \begin{lstlisting} - print I, I.shape + In []: print I, I.shape \end{lstlisting} \begin{enumerate} \item Get the top left quadrant of the image @@ -191,14 +190,14 @@ \item Slicing works just like with lists \end{itemize} \begin{lstlisting} - C[0:3, 2] - C[2, 0:3] - C[2, :3] + In []:C[0:3, 2] + In []: C[2, 0:3] + In []:C[2, :3] \end{lstlisting} \begin{lstlisting} - imshow(I[:150, :150]) + In []: imshow(I[:150, :150]) - imshow(I[75:225, 75:225]) + In []: imshow(I[75:225, 75:225]) \end{lstlisting} \end{frame} @@ -211,15 +210,15 @@ \item The idea is similar to striding in lists \end{itemize} \begin{lstlisting} - C[0:5:2, 0:5:2] - C[::2, ::2] - C[1::2, ::2] + In []: C[0:5:2, 0:5:2] + In []: C[::2, ::2] + In []: C[1::2, ::2] \end{lstlisting} \begin{itemize} \item Now, the image can be shrunk by \end{itemize} \begin{lstlisting} - imshow(I[::2, ::2]) + In []: imshow(I[::2, ::2]) \end{lstlisting} \end{frame} @@ -252,13 +251,28 @@ \begin{frame}[fragile] \frametitle{Least Square Fit} \begin{lstlisting} - l, t = loadtxt("pendulum.txt", unpack=True) - l - t - tsq = t * t - plot(l, tsq, 'bo') - plot(l, tsq, 'r') - \end{lstlisting} + In []: L, t = loadtxt("pendulum.txt", + unpack=True) + In []: L + In []: t + In []: tsq = t * t + In []: plot(L, tsq, 'bo') +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{$L$ vs. $T^2$ - Scatter} +Linear trend visible. +\begin{figure} +\includegraphics[width=4in]{data/L-Tsq-points} +\end{figure} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Least Square Fit} +\begin{lstlisting} + In []: plot(L, tsq, 'r') +\end{lstlisting} \begin{itemize} \item Both the plots, aren't what we expect -- linear plot \item Enter Least square fit! @@ -266,6 +280,23 @@ \end{frame} \begin{frame}[fragile] +\frametitle{$L$ vs. $T^2$ - Line} +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$ - Least Square Fit} +This is what our intention is. +\begin{figure} +\includegraphics[width=4in]{data/least-sq-fit} +\end{figure} +\end{frame} + +\begin{frame}[fragile] \frametitle{Matrix Formulation} \begin{itemize} \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ @@ -295,34 +326,36 @@ \begin{frame}[fragile] \frametitle{Least Square Fit Line} \begin{lstlisting} - A = array((l, ones_like(l))) - A.T - A + In []: A = array((L, ones_like(L))) + In []: A = A.T + In []: A \end{lstlisting} \begin{itemize} \item We now have \texttt{A} and \texttt{tsq} \end{itemize} \begin{lstlisting} - result = lstsq(A, tsq) + In []: result = lstsq(A, tsq) \end{lstlisting} \begin{itemize} \item Result has a lot of values along with m and c, that we need \end{itemize} \begin{lstlisting} - m, c = result[0] - print m, c + In []: m, c = result[0] + In []: print m, c \end{lstlisting} \end{frame} + + \begin{frame}[fragile] \frametitle{Least Square Fit Line} \begin{itemize} \item Now that we have m and c, we use them to generate line and plot \end{itemize} \begin{lstlisting} - tsq_fit = m * l + c - plot(l, tsq, 'bo') - plot(l, tsq_fit, 'r') + In []: tsq_fit = m * L + c + In []: plot(L, tsq, 'bo') + In []:plot(L, tsq_fit, 'r') \end{lstlisting} \end{frame} diff --git a/advanced_python/slides/modules.tex b/advanced_python/slides/modules.tex index 957789a..edae339 100644 --- a/advanced_python/slides/modules.tex +++ b/advanced_python/slides/modules.tex @@ -6,13 +6,13 @@ \item Script to print `hello world' -- \texttt{hello.py} \end{itemize} \begin{lstlisting} - print "Hello world!" + In []: print "Hello world!" \end{lstlisting} \begin{itemize} \item We have been running scripts from IPython \end{itemize} \begin{lstlisting} - %run -i hello.py + In []: %run -i hello.py \end{lstlisting} \begin{itemize} \item Now, we run from the shell using python @@ -28,9 +28,9 @@ \item Save the following in \texttt{sine\_plot.py} \end{itemize} \begin{lstlisting} - x = linspace(-2*pi, 2*pi, 100) - plot(x, sin(x)) - show() + In []: x = linspace(-2*pi, 2*pi, 100) + In []: plot(x, sin(x)) + In []: show() \end{lstlisting} \begin{itemize} \item Now, let us run the script @@ -103,19 +103,20 @@ \item Save it as \texttt{gcd\_script.py} \end{itemize} \begin{lstlisting} - def gcd(a, b): - while b: - a, b = b, a%b - return a + In []: def gcd(a, b): + .... while b: + .... a, b = b, a%b + .... return a + .... \end{lstlisting} \begin{itemize} \item Also add the tests to the file \end{itemize} \begin{lstlisting} - if gcd(40, 12) == 4 and gcd(12, 13) == 1: - print "Everything OK" - else: - print "The GCD function is wrong" + .... if gcd(40, 12) == 4 and gcd(12, 13) == 1: + .... print "Everything OK" + .... else: + .... print "The GCD function is wrong" \end{lstlisting} \begin{lstlisting} $ python gcd_script.py diff --git a/advanced_python/slides/plotting.tex b/advanced_python/slides/plotting.tex index 4f8d51f..6fa31e6 100644 --- a/advanced_python/slides/plotting.tex +++ b/advanced_python/slides/plotting.tex @@ -9,8 +9,8 @@ $ ipython -pylab \end{lstlisting} % $ \begin{lstlisting} - p = linspace(-pi,pi,100) - plot(p, cos(p)) + In []: p = linspace(-pi,pi,100) + In []: plot(p, cos(p)) \end{lstlisting} \end{frame} @@ -20,11 +20,11 @@ \begin{itemize} \item \texttt{p} has a hundred points in the range -pi to pi \begin{lstlisting} - print p[0], p[-1], len(p) + In []: print p[0], p[-1], len(p) \end{lstlisting} \item Look at the doc-string of \texttt{linspace} for more details \begin{lstlisting} - linspace? + In []: linspace? \end{lstlisting} \end{itemize} \begin{itemize} @@ -38,50 +38,50 @@ \begin{frame}[fragile] \frametitle{Plot color and thickness} \begin{lstlisting} - clf() - plot(p, sin(p), 'r') + In []: clf() + In []: plot(p, sin(p), 'r') \end{lstlisting} \begin{itemize} \item Gives a sine curve in Red. \end{itemize} \begin{lstlisting} - plot(p, cos(p), linewidth=2) + In []: plot(p, cos(p), linewidth=2) \end{lstlisting} \begin{itemize} \item Sets line thickness to 2 \end{itemize} \begin{lstlisting} - clf() - plot(p, sin(p), '.') + In []: clf() + In []:plot(p, sin(p), '.') \end{lstlisting} \begin{itemize} \item Produces a plot with only points \end{itemize} \begin{lstlisting} - plot? + In []: plot? \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{\texttt{title}} \begin{lstlisting} - x = linspace(-2, 4, 50) - plot(x, -x*x + 4*x - 5, 'r', linewidth=2) - title("Parabolic function -x^2+4x-5") + In []: x = linspace(-2, 4, 50) + In []: plot(x, -x*x + 4*x - 5, 'r', linewidth=2) + In []: title("Parabolic function -x^2+4x-5") \end{lstlisting} \begin{itemize} \item We can set title using \LaTeX~ \end{itemize} \begin{lstlisting} - title("Parabolic function $-x^2+4x-5$") + In []: title("Parabolic function $-x^2+4x-5$") \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Axes labels} \begin{lstlisting} - xlabel("x") - ylabel("f(x)") + In []: xlabel("x") + In []:ylabel("f(x)") \end{lstlisting} \begin{itemize} \item We could, if required use \LaTeX~ @@ -91,7 +91,7 @@ \begin{frame}[fragile] \frametitle{Annotate} \begin{lstlisting} - annotate("local maxima", xy=(2, -1)) + In []: annotate("local maxima", xy=(2, -1)) \end{lstlisting} \begin{itemize} \item First argument is the annotation text @@ -103,8 +103,8 @@ \begin{frame}[fragile] \frametitle{Limits of Plot area} \begin{lstlisting} - xlim() - ylim() + In []: xlim() + In []: ylim() \end{lstlisting} \begin{itemize} \item With no arguments, \texttt{xlim} \& \texttt{ylim} get the @@ -112,10 +112,10 @@ \item New limits are set, when arguments are passed to them \end{itemize} \begin{lstlisting} - xlim(-4, 5) + In []: xlim(-4, 5) \end{lstlisting} \begin{lstlisting} - ylim(-15, 2) + In []: ylim(-15, 2) \end{lstlisting} \end{frame} @@ -126,17 +126,17 @@ \begin{itemize} \item To see the history of commands, we typed \begin{lstlisting} - %hist + In []: %hist \end{lstlisting} \item All commands, valid or invalid, appear in the history \item \texttt{\%hist} is a magic command, available only in IPython \end{itemize} \begin{lstlisting} - %hist 5 + In []: %hist 5 # last 5 commands \end{lstlisting} \begin{lstlisting} - %hist 5-10 + In []: %hist 5-10 # commands between 5 and 10 \end{lstlisting} \end{frame} @@ -150,7 +150,7 @@ \item \texttt{\%save} magic command to save the commands to a file \end{itemize} \begin{lstlisting} - %save plot_script.py 1 3-6 8 + In []: %save plot_script.py 1 3-6 8 \end{lstlisting} \begin{itemize} \item File name must have a \texttt{.py} extension @@ -160,7 +160,7 @@ \begin{frame}[fragile] \frametitle{Running the script} \begin{lstlisting} - %run -i plot_script.py + In []: %run -i plot_script.py \end{lstlisting} \begin{itemize} \item There were no errors in the plot, but we don't see it! @@ -168,7 +168,7 @@ \item We need to explicitly ask for the image to be shown \end{itemize} \begin{lstlisting} - show() + In []: show() \end{lstlisting} \begin{itemize} \item \texttt{-i} asks the interpreter to check for names, @@ -183,9 +183,9 @@ \begin{frame}[fragile] \frametitle{\texttt{savefig}} \begin{lstlisting} - x = linspace(-3*pi,3*pi,100) - plot(x,sin(x)) - savefig('sine.png') + In []: x = linspace(-3*pi,3*pi,100) + In []: plot(x,sin(x)) + In []: savefig('sine.png') \end{lstlisting} \begin{itemize} \item \texttt{savefig} takes one argument @@ -199,16 +199,16 @@ \begin{frame}[fragile] \frametitle{Overlaid plots} \begin{lstlisting} - x = linspace(0, 50, 10) - plot(x, sin(x)) + In []: x = linspace(0, 50, 10) + In []: plot(x, sin(x)) \end{lstlisting} \begin{itemize} \item The curve isn't as smooth as we expected \item We chose too few points in the interval \end{itemize} \begin{lstlisting} - y = linspace(0, 50, 500) - plot(y, sin(y)) + In []: y = linspace(0, 50, 500) + In []: plot(y, sin(y)) \end{lstlisting} \begin{itemize} \item The plots are overlaid @@ -219,14 +219,14 @@ \begin{frame}[fragile] \frametitle{Legend} \begin{lstlisting} - legend(['sine-10 points', 'sine-500 points']) + In []: legend(['sine-10 points', 'sine-500 points']) \end{lstlisting} \begin{itemize} \item Placed in the location, \texttt{pylab} thinks is `best' \item \texttt{loc} parameter allows to change the location \end{itemize} \begin{lstlisting} - legend(['sine-10 points', 'sine-500 points'], + In []: legend(['sine-10 points', 'sine-500 points'], loc='center') \end{lstlisting} \end{frame} @@ -234,24 +234,24 @@ \begin{frame}[fragile] \frametitle{Plotting in separate figures} \begin{lstlisting} - clf() - x = linspace(0, 50, 500) - figure(1) - plot(x, sin(x), 'b') - figure(2) - plot(x, cos(x), 'g') + In []: clf() + In []: x = linspace(0, 50, 500) + In []: figure(1) + In []: plot(x, sin(x), 'b') + In []:figure(2) + In []: plot(x, cos(x), 'g') \end{lstlisting} \begin{itemize} \item \texttt{figure} command allows us to have plots separately \item It is also used to switch context between the plots \end{itemize} \begin{lstlisting} - savefig('cosine.png') - figure(1) - title('sin(y)') - savefig('sine.png') - close() - close() + In []: savefig('cosine.png') + In []: figure(1) + In []: title('sin(y)') + In []: savefig('sine.png') + In []: close() + In []: close() \end{lstlisting} \begin{itemize} \item \texttt{close('all')} closes all the figures @@ -261,7 +261,7 @@ \begin{frame}[fragile] \frametitle{Subplots} \begin{lstlisting} - subplot(2, 1, 1) + In []: subplot(2, 1, 1) \end{lstlisting} \begin{itemize} \item number of rows @@ -269,13 +269,13 @@ \item plot number, in serial order, to access or create \end{itemize} \begin{lstlisting} - subplot(2, 1, 2) - x = linspace(0, 50, 500) - plot(x, cos(x)) + In []: subplot(2, 1, 2) + In []: x = linspace(0, 50, 500) + In []: plot(x, cos(x)) - subplot(2, 1, 1) - y = linspace(0, 5, 100) - plot(y, y ** 2) + In []: subplot(2, 1, 1) + In []: y = linspace(0, 5, 100) + In []: plot(y, y ** 2) \end{lstlisting} \end{frame} @@ -289,8 +289,8 @@ \item We read the data using \texttt{loadtxt} \end{itemize} \begin{lstlisting} - primes = loadtxt('primes.txt') - print primes + In []: primes = loadtxt('primes.txt') + In []: print primes \end{lstlisting} \begin{itemize} \item \texttt{primes} is a sequence of floats @@ -306,8 +306,8 @@ \item \texttt{loadtxt} requires both columns to be of same length \end{itemize} \begin{lstlisting} - pend = loadtxt('pendulum.txt') - print pend + In []: pend = loadtxt('pendulum.txt') + In []: print pend \end{lstlisting} \begin{itemize} \item \texttt{pend} is not a simple sequence like \texttt{primes} @@ -317,9 +317,9 @@ \begin{frame}[fragile] \frametitle{Unpacking with \texttt{loadtxt}} \begin{lstlisting} - L, T = loadtxt('pendulum.txt', unpack=True) - print L - print T + In []: L, T = loadtxt('pendulum.txt', unpack=True) + In []: print L + In []: print T \end{lstlisting} \begin{itemize} \item We wish to plot L vs. $T^2$ @@ -327,9 +327,9 @@ \item (We could instead iterate over T and calculate) \end{itemize} \begin{lstlisting} - Tsq = square(T) + In []: Tsq = square(T) - plot(L, Tsq, '.') + In []: plot(L, Tsq, '.') \end{lstlisting} \end{frame} @@ -341,11 +341,11 @@ \item Read the values and make an error bar plot \end{itemize} \begin{lstlisting} - L, T, L_err, T_err = \ - loadtxt('pendulum_error.txt', unpack=True) - Tsq = square(T) + In []: L, T, L_err, T_err = \ + In []: loadtxt('pendulum_error.txt', unpack=True) + In []: Tsq = square(T) - errorbar(L, Tsq , xerr=L_err, + In []: errorbar(L, Tsq , xerr=L_err, yerr=T_err, fmt='b.') \end{lstlisting} \end{frame} @@ -361,10 +361,10 @@ \item Let's plot the data of profits of a company \end{itemize} \begin{lstlisting} - year, profit = loadtxt('company-a-data.txt', + In []: year, profit = loadtxt('company-a-data.txt', dtype=type(int())) - scatter(year, profit) + In []: scatter(year, profit) \end{lstlisting} \begin{itemize} \item \alert{\texttt{dtype=int}; default is float} @@ -374,11 +374,11 @@ \begin{frame}[fragile] \frametitle{Pie Chart \& Bar Chart} \begin{lstlisting} - pie(profit, labels=year) + In []: pie(profit, labels=year) \end{lstlisting} \begin{lstlisting} - bar(year, profit) + In []: bar(year, profit) \end{lstlisting} \end{frame} @@ -388,11 +388,11 @@ \item Plot a \texttt{log-log} chart of $y=5x^3$ for x from 1 to 20 \end{itemize} \begin{lstlisting} - x = linspace(1,20,100) - y = 5*x**3 + In []: x = linspace(1,20,100) + In []: y = 5*x**3 - loglog(x, y) - plot(x, y) + In []: loglog(x, y) + In []: plot(x, y) \end{lstlisting} \begin{itemize} \item Look at \url{http://matplotlib.sourceforge.net/contents.html} |