diff options
-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, 161 insertions, 195 deletions
diff --git a/advanced_python/slides/arrays.tex b/advanced_python/slides/arrays.tex index b565e72..7b829f9 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} - In []: a1 = array([1,2,3,4]) + a1 = array([1,2,3,4]) a1 # 1-D - In []: a2 = array([[1,2,3,4],[5,6,7,8]]) + a2 = array([[1,2,3,4],[5,6,7,8]]) a2 # 2-D \end{lstlisting} \end{frame} @@ -17,38 +17,39 @@ \begin{frame}[fragile] \frametitle{\texttt{arange} and \texttt{shape}} \begin{lstlisting} - In []: ar1 = arange(1, 5) - In []: ar2 = arange(1, 9) - In []: print ar2 - In []: ar2.shape = 2, 4 - In []: print ar2 + ar1 = arange(1, 5) + + ar2 = arange(1, 9) + print ar2 + ar2.shape = 2, 4 + print ar2 \end{lstlisting} \begin{itemize} \item \texttt{linspace} and \texttt{loadtxt} also returned arrays \end{itemize} \begin{lstlisting} - In []: ar1.shape - In []: ar2.shape + ar1.shape + ar2.shape \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Special methods} \begin{lstlisting} - In []: identity(3) + identity(3) \end{lstlisting} \begin{itemize} \item array of shape (3, 3) with diagonals as 1s, rest 0s \end{itemize} \begin{lstlisting} - In []: zeros((4,5)) + zeros((4,5)) \end{lstlisting} \begin{itemize} \item array of shape (4, 5) with all 0s \end{itemize} \begin{lstlisting} - In []: a = zeros_like([1.5, 1, 2, 3]) - In []: print a, a.dtype + a = zeros_like([1.5, 1, 2, 3]) + print a, a.dtype \end{lstlisting} \begin{itemize} \item An array with all 0s, with similar shape and dtype as argument @@ -60,17 +61,17 @@ \begin{frame}[fragile] \frametitle{Operations on arrays} \begin{lstlisting} - In []: a1 - In []: a1 * 2 - In []: a1 + a1 + a1 * 2 + a1 \end{lstlisting} \begin{itemize} \item The array is not changed; New array is returned \end{itemize} \begin{lstlisting} - In []: a1 + 3 - In []: a1 - 7 - In []: a1 / 2.0 + a1 + 3 + a1 - 7 + a1 / 2.0 \end{lstlisting} \end{frame} @@ -80,25 +81,25 @@ \item Like lists, we can assign the new array, the old name \end{itemize} \begin{lstlisting} - In []: a1 = a1 + 2 - In []: a1 + a1 = a1 + 2 + a1 \end{lstlisting} \begin{itemize} \item \alert{Beware of Augmented assignment!} \end{itemize} \begin{lstlisting} - 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 + 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 \end{lstlisting} \begin{itemize} \item Operations on two arrays; element-wise \end{itemize} \begin{lstlisting} - In []: a1 + a1 - In []: a1 * a2 + a1 + a1 + a1 * a2 \end{lstlisting} \end{frame} @@ -107,24 +108,24 @@ \begin{frame}[fragile] \frametitle{Accessing \& changing elements} \begin{lstlisting} - In []: A = array([12, 23, 34, 45, 56]) + A = array([12, 23, 34, 45, 56]) - In []: C = array([[11, 12, 13, 14, 15], + 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]]) - In []: A[2] - In []: C[2, 3] + A[2] + C[2, 3] \end{lstlisting} \begin{itemize} \item Indexing starts from 0 \item Assign new values, to change elements \end{itemize} \begin{lstlisting} - In []: A[2] = -34 - In []: C[2, 3] = -34 + A[2] = -34 + C[2, 3] = -34 \end{lstlisting} \end{frame} @@ -134,49 +135,49 @@ \item Indexing works just like with lists \end{itemize} \begin{lstlisting} - In []: C[2] - In []: C[4] - In []: C[-1] + C[2] + C[4] + C[-1] \end{lstlisting} \begin{itemize} \item Change the last row into all zeros \end{itemize} \begin{lstlisting} - In []: C[-1] = [0, 0, 0, 0, 0] + C[-1] = [0, 0, 0, 0, 0] \end{lstlisting} OR \begin{lstlisting} - In []: C[-1] = 0 + C[-1] = 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Accessing columns} \begin{lstlisting} - In []: C[:, 2] - In []: C[:, 4] - In []: C[:, -1] + C[:, 2] + C[:, 4] + 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} - In []: C[:, -1] = 0 + C[:, -1] = 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Slicing} \begin{lstlisting} - In []: I = imread('squares.png') - In []: imshow(I) + I = imread('squares.png') + imshow(I) \end{lstlisting} \begin{itemize} \item The image is just an array \end{itemize} \begin{lstlisting} - In []: print I, I.shape + print I, I.shape \end{lstlisting} \begin{enumerate} \item Get the top left quadrant of the image @@ -190,14 +191,14 @@ \item Slicing works just like with lists \end{itemize} \begin{lstlisting} - In []:C[0:3, 2] - In []: C[2, 0:3] - In []:C[2, :3] + C[0:3, 2] + C[2, 0:3] + C[2, :3] \end{lstlisting} \begin{lstlisting} - In []: imshow(I[:150, :150]) + imshow(I[:150, :150]) - In []: imshow(I[75:225, 75:225]) + imshow(I[75:225, 75:225]) \end{lstlisting} \end{frame} @@ -210,15 +211,15 @@ \item The idea is similar to striding in lists \end{itemize} \begin{lstlisting} - In []: C[0:5:2, 0:5:2] - In []: C[::2, ::2] - In []: C[1::2, ::2] + C[0:5:2, 0:5:2] + C[::2, ::2] + C[1::2, ::2] \end{lstlisting} \begin{itemize} \item Now, the image can be shrunk by \end{itemize} \begin{lstlisting} - In []: imshow(I[::2, ::2]) + imshow(I[::2, ::2]) \end{lstlisting} \end{frame} @@ -251,28 +252,13 @@ \begin{frame}[fragile] \frametitle{Least Square Fit} \begin{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} + l, t = loadtxt("pendulum.txt", unpack=True) + l + t + tsq = t * t + plot(l, tsq, 'bo') + plot(l, tsq, 'r') + \end{lstlisting} \begin{itemize} \item Both the plots, aren't what we expect -- linear plot \item Enter Least square fit! @@ -280,23 +266,6 @@ Linear trend visible. \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$ @@ -326,36 +295,34 @@ This is what our intention is. \begin{frame}[fragile] \frametitle{Least Square Fit Line} \begin{lstlisting} - In []: A = array((L, ones_like(L))) - In []: A = A.T - In []: A + A = array((l, ones_like(l))) + A.T + A \end{lstlisting} \begin{itemize} \item We now have \texttt{A} and \texttt{tsq} \end{itemize} \begin{lstlisting} - In []: result = lstsq(A, tsq) + 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} - In []: m, c = result[0] - In []: print m, c + m, c = result[0] + 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} - In []: tsq_fit = m * L + c - In []: plot(L, tsq, 'bo') - In []:plot(L, tsq_fit, 'r') + tsq_fit = m * l + c + plot(l, tsq, 'bo') + plot(l, tsq_fit, 'r') \end{lstlisting} \end{frame} diff --git a/advanced_python/slides/modules.tex b/advanced_python/slides/modules.tex index edae339..957789a 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} - In []: print "Hello world!" + print "Hello world!" \end{lstlisting} \begin{itemize} \item We have been running scripts from IPython \end{itemize} \begin{lstlisting} - In []: %run -i hello.py + %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} - In []: x = linspace(-2*pi, 2*pi, 100) - In []: plot(x, sin(x)) - In []: show() + x = linspace(-2*pi, 2*pi, 100) + plot(x, sin(x)) + show() \end{lstlisting} \begin{itemize} \item Now, let us run the script @@ -103,20 +103,19 @@ \item Save it as \texttt{gcd\_script.py} \end{itemize} \begin{lstlisting} - In []: def gcd(a, b): - .... while b: - .... a, b = b, a%b - .... return a - .... + 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 6fa31e6..b8b4bce 100644 --- a/advanced_python/slides/plotting.tex +++ b/advanced_python/slides/plotting.tex @@ -9,8 +9,8 @@ $ ipython -pylab \end{lstlisting} % $ \begin{lstlisting} - In []: p = linspace(-pi,pi,100) - In []: plot(p, cos(p)) + p = linspace(-pi,pi,100) + 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} - In []: print p[0], p[-1], len(p) + print p[0], p[-1], len(p) \end{lstlisting} \item Look at the doc-string of \texttt{linspace} for more details \begin{lstlisting} - In []: linspace? + linspace? \end{lstlisting} \end{itemize} \begin{itemize} @@ -38,50 +38,50 @@ \begin{frame}[fragile] \frametitle{Plot color and thickness} \begin{lstlisting} - In []: clf() - In []: plot(p, sin(p), 'r') + clf() + plot(p, sin(p), 'r') \end{lstlisting} \begin{itemize} \item Gives a sine curve in Red. \end{itemize} \begin{lstlisting} - In []: plot(p, cos(p), linewidth=2) + plot(p, cos(p), linewidth=2) \end{lstlisting} \begin{itemize} \item Sets line thickness to 2 \end{itemize} \begin{lstlisting} - In []: clf() - In []:plot(p, sin(p), '.') + clf() + plot(p, sin(p), '.') \end{lstlisting} \begin{itemize} \item Produces a plot with only points \end{itemize} \begin{lstlisting} - In []: plot? + plot? \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{\texttt{title}} \begin{lstlisting} - 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") + x = linspace(-2, 4, 50) + plot(x, -x*x + 4*x - 5, 'r', linewidth=2) + title("Parabolic function -x^2+4x-5") \end{lstlisting} \begin{itemize} \item We can set title using \LaTeX~ \end{itemize} \begin{lstlisting} - In []: title("Parabolic function $-x^2+4x-5$") + title("Parabolic function $-x^2+4x-5$") \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Axes labels} \begin{lstlisting} - In []: xlabel("x") - In []:ylabel("f(x)") + xlabel("x") + 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} - In []: annotate("local maxima", xy=(2, -1)) + 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} - In []: xlim() - In []: ylim() + xlim() + 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} - In []: xlim(-4, 5) + xlim(-4, 5) \end{lstlisting} \begin{lstlisting} - In []: ylim(-15, 2) + ylim(-15, 2) \end{lstlisting} \end{frame} @@ -126,17 +126,17 @@ \begin{itemize} \item To see the history of commands, we typed \begin{lstlisting} - In []: %hist + %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} - In []: %hist 5 + %hist 5 # last 5 commands \end{lstlisting} \begin{lstlisting} - In []: %hist 5-10 + %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} - In []: %save plot_script.py 1 3-6 8 + %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} - In []: %run -i plot_script.py + %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} - In []: show() + 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} - In []: x = linspace(-3*pi,3*pi,100) - In []: plot(x,sin(x)) - In []: savefig('sine.png') + x = linspace(-3*pi,3*pi,100) + plot(x,sin(x)) + 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} - In []: x = linspace(0, 50, 10) - In []: plot(x, sin(x)) + x = linspace(0, 50, 10) + 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} - In []: y = linspace(0, 50, 500) - In []: plot(y, sin(y)) + y = linspace(0, 50, 500) + plot(y, sin(y)) \end{lstlisting} \begin{itemize} \item The plots are overlaid @@ -219,14 +219,14 @@ \begin{frame}[fragile] \frametitle{Legend} \begin{lstlisting} - In []: legend(['sine-10 points', 'sine-500 points']) + 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} - In []: legend(['sine-10 points', 'sine-500 points'], + 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} - 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') + clf() + x = linspace(0, 50, 500) + figure(1) + plot(x, sin(x), 'b') + figure(2) + 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} - In []: savefig('cosine.png') - In []: figure(1) - In []: title('sin(y)') - In []: savefig('sine.png') - In []: close() - In []: close() + savefig('cosine.png') + figure(1) + title('sin(y)') + savefig('sine.png') + close() + close() \end{lstlisting} \begin{itemize} \item \texttt{close('all')} closes all the figures @@ -261,7 +261,7 @@ \begin{frame}[fragile] \frametitle{Subplots} \begin{lstlisting} - In []: subplot(2, 1, 1) + 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} - In []: subplot(2, 1, 2) - In []: x = linspace(0, 50, 500) - In []: plot(x, cos(x)) + subplot(2, 1, 2) + x = linspace(0, 50, 500) + plot(x, cos(x)) - In []: subplot(2, 1, 1) - In []: y = linspace(0, 5, 100) - In []: plot(y, y ** 2) + subplot(2, 1, 1) + y = linspace(0, 5, 100) + plot(y, y ** 2) \end{lstlisting} \end{frame} @@ -289,8 +289,8 @@ \item We read the data using \texttt{loadtxt} \end{itemize} \begin{lstlisting} - In []: primes = loadtxt('primes.txt') - In []: print primes + primes = loadtxt('primes.txt') + 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} - In []: pend = loadtxt('pendulum.txt') - In []: print pend + pend = loadtxt('pendulum.txt') + 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} - In []: L, T = loadtxt('pendulum.txt', unpack=True) - In []: print L - In []: print T + L, T = loadtxt('pendulum.txt', unpack=True) + print L + 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} - In []: Tsq = square(T) + Tsq = square(T) - In []: plot(L, Tsq, '.') + 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} - In []: L, T, L_err, T_err = \ - In []: loadtxt('pendulum_error.txt', unpack=True) - In []: Tsq = square(T) + L, T, L_err, T_err = \ + loadtxt('pendulum_error.txt', unpack=True) + Tsq = square(T) - In []: errorbar(L, Tsq , xerr=L_err, + 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} - In []: year, profit = loadtxt('company-a-data.txt', + year, profit = loadtxt('company-a-data.txt', dtype=type(int())) - In []: scatter(year, profit) + 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} - In []: pie(profit, labels=year) + pie(profit, labels=year) \end{lstlisting} \begin{lstlisting} - In []: bar(year, profit) + 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} - In []: x = linspace(1,20,100) - In []: y = 5*x**3 + x = linspace(1,20,100) + y = 5*x**3 - In []: loglog(x, y) - In []: plot(x, y) + loglog(x, y) + plot(x, y) \end{lstlisting} \begin{itemize} \item Look at \url{http://matplotlib.sourceforge.net/contents.html} |