# HG changeset patch # User Madhusudan.C.S # Date 1257853927 -19800 # Node ID f87f2a310abedf155ba88942712f042e7653f37e # Parent d47d36d057a7f7cd143c4e6dd4f081c7aa7b264b# Parent a835affb14471200a122ef063928daae5aa11bb5 Merged all files. It is a trick. If you want to know RTFM. diff -r d47d36d057a7 -r f87f2a310abe day1/cheatsheet1.tex --- a/day1/cheatsheet1.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/cheatsheet1.tex Tue Nov 10 17:22:07 2009 +0530 @@ -1,6 +1,22 @@ \documentclass[12pt]{article} + + \title{Interactive Plotting} \author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, +commentstyle=\itshape\bfseries +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + \begin{document} \date{} \vspace{-1in} @@ -10,35 +26,56 @@ \end{center} \section{Starting up...} -\begin{verbatim} +\begin{lstlisting} $ ipython -pylab -\end{verbatim} +\end{lstlisting} Exiting -\begin{verbatim} +\begin{lstlisting} In [2]: (Ctrl-D)^D Do you really want to exit ([y]/n)? y -\end{verbatim} -Breaking out of loops -\begin{verbatim} -In [1]: while True: - ...: print "Hello, World!" - ...: -Hello, World! -Hello, World!(Ctrl-C)^C -\end{verbatim} +\end{lstlisting} \section{Plotting} -\begin{verbatim} + + \begin{lstlisting} In [1]: x = linspace(0, 2*pi, 50) -In [2]: plot(x,sin(x)) +In [2]: plot(x, sin(x)) In [3]: xlabel('x') In [4]: ylabel('sin(x)') -In [5]: legend(['x', '-x', 'sin(x)', 'xsin(x)']) -In [6]: annotate('origin', xy=(0, 0), xytext=(0, -7), - arrowprops=dict(shrink=0.05)) -In [7]: xlim(5*pi, 5*pi) -In [8]: ylim(5*pi, 5*pi) -In [9]: clf() #Clears the Plot area -\end{verbatim} +In [5]: title('Sinusoids') +In [6]: legend(['sin(y)']) +In [7]: legend(['sin(2y)'], loc = 'center') +# loc = 'upper right', 'upper left', 'lower left, 'lower right', 'center left', +# 'center right', 'lower center', 'upper center', 'best', 'right', 'center' + +In [8]: legend(['sin(2y)'], loc = (.8, .1)) + +In [9]: savefig('sin.png') # Save figure +In [10]: close() # Closes the figure + +In [11]: clf() # Clears the Plot area + +In [12]: plot(y, sin(y), 'g') +# Colors can be: 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w' + +In [13]: plot(y, cos(y), 'r', linewidth=2) + +In [14]: legend(['x', '-x']) +In [15]: annotate('origin', xy=(0, 0)) + +In [16]: xmin, xman = xlim() # Without arguments gets +In [17]: ymin, ymax = ylim() # values + +In [18]: xlim(0, 2 * pi) # With values, sets the +In [19]: ylim(ymin - 0.2, ymax + 0.2) # specified values + \end{lstlisting} + +\section{Saving and running scripts} +\begin{itemize} + \item \typ{\%hist} + \item \typ{\%save four\_plot.py 16 18-27} + \item \typ{\%run -i four\_plot.py} +\end{itemize} + \end{document} diff -r d47d36d057a7 -r f87f2a310abe day1/cheatsheet2.tex --- a/day1/cheatsheet2.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/cheatsheet2.tex Tue Nov 10 17:22:07 2009 +0530 @@ -1,11 +1,28 @@ \documentclass[12pt]{article} -%\title{Plotting Data} -%\author{FOSSEE} + + +\title{Plotting Points} +\author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, + commentstyle=\itshape\bfseries, + showstringspaces=false, +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + \begin{document} \date{} \vspace{-1in} \begin{center} -\LARGE{Plotting Data}\\ +\LARGE{Plotting Points}\\ \large{FOSSEE} \end{center} \section{Plotting from Data files} @@ -21,5 +38,75 @@ tsq.append(t*t) plot(l, tsq, '.') # Plotting points \end{verbatim} +\section{Plotting Points with Lists} + +\begin{lstlisting} +In [1]: x = [0, 1, 2, 3] +In [2]: y = [7, 11, 15, 19] +In [3]: plot(x, y) +In [4]: clf() +In [5]: plot(x, y, 'o') # Plotting Circles +#Dots - '.', #Dashed lines - '--' #Lines - '-' +\end{lstlisting} + +\section{Lists} + +Initializing + \begin{lstlisting} +In [10]: mtlist = [] # Empty List +In [11]: lst = [ 1, 2, 3, 4, 5] + \end{lstlisting} +Slicing +\begin{lstlisting} +In [12]: lst[1:3] # A slice. +Out[12]: [2, 3] + +In [13]: lst[1:-1] +Out[13]: [2, 3, 4] +\end{lstlisting} +Appending to lists +\begin{lstlisting} +In [14]: a = [ 6, 7, 8, 9] +In [15]: b = lst + a +In [16]: b +Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9] + +In [17]: lst.append(6) +In [18]: lst +Out[18]: [ 1, 2, 3, 4, 5, 6] +\end{lstlisting} + +Iterating over a List +\begin{lstlisting} +In [19]: for each in b: # Iterating over the list, element-wise + ....: print b # Print each element + ....: +\end{lstlisting} + +Splitting Strings +\begin{lstlisting} +In [20]: line = '1.2000e-01 7.4252e-01' +In [21]: point = line.split() # Splits the string at the space +Out[21]: ['1.2000e-01', '7.4252e-01'] +\end{lstlisting} + +Plotting from Files +\begin{lstlisting} +In [22]: L = [] +In [23]: T = [] + +#Open a file & operate on each line +In [24]: for line in open('pendulum.txt'): + .... point = line.split() + .... L.append(float(point[0])) + .... T.append(float(point[1])) +In [25]: TSq = [] +In [26]: for t in T: + ....: TSq.append(t*t) + ....: + ....: +In [27]: plot(L, TSq, '.') +\end{lstlisting} + \end{document} diff -r d47d36d057a7 -r f87f2a310abe day1/cheatsheet3.tex --- a/day1/cheatsheet3.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/cheatsheet3.tex Tue Nov 10 17:22:07 2009 +0530 @@ -1,6 +1,22 @@ \documentclass[12pt]{article} \title{Interactive Plotting} \author{FOSSEE} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, + commentstyle=\itshape\bfseries, + showstringspaces=false, +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + \begin{document} \date{} \vspace{-1in} @@ -10,54 +26,59 @@ \end{center} \section{Statistics} Dictionary -\begin{verbatim} -In [1]: d = {"Hitchhiker's guide" : 42, - ....: "Terminator" : "I'll be back"} #Creation -In [2]: d["Hitchhiker's guide"] # Accessing a value with key -In [3]: "Hitchhiker's guide" in d #Checking for a key -In [4]: d.keys() # Obtaining List of Keys -In [5]: d.values() # Obtaining List of Values -\end{verbatim} +\begin{lstlisting} +In []: d = {"Hitchhiker's guide" : 42, + ....: "Terminator" : "I'll be back"} #Creation +In []: d["Hitchhiker's guide"] # Accessing a value with key +In []: "Hitchhiker's guide" in d #Checking for a key +In []: d.keys() # Obtaining List of Keys +In []: d.values() # Obtaining List of Values +\end{lstlisting} Iterating through List indices -\begin{verbatim} -In [1]: names = ["Guido","Alex", "Tim"] -In [2]: for i, name in enumerate(names): - ...: print i, name -\end{verbatim} - -\begin{verbatim} -In [1]: score = int(score_str) if score_str != 'AA' else 0 -\end{verbatim} -Drawing Pie Charts -\begin{verbatim} -In [1]: pie(science.values(), labels=science.keys()) -\end{verbatim} +\begin{lstlisting} +In []: names = ["Guido","Alex", "Tim"] +In []: for i, name in enumerate(names): + ...: print i, name +\end{lstlisting} +Computing Mean value of `\texttt{g}' +\begin{lstlisting} +In []: G = [] +In []: for line in open('pendulum.txt'): + .... points = line.split() + .... l = float(points[0]) + .... t = float(points[1]) + .... g = 4 * pi * pi * l / t * t + .... G.append(g) +\end{lstlisting} sum() and len() functions -\begin{verbatim} -In [1]: mean = sum(math_scores) / len(math_scores) -\end{verbatim} +\begin{lstlisting} + total = 0 + for g in G: + total += g + mean_g = total / len(g) + + mean_g = sum(G) / len(G) + mean_g = mean(G) +\end{lstlisting} +\newpage +Ternary Operator +\begin{lstlisting} +In []: score = int(score_str) if score_str != 'AA' else 0 +\end{lstlisting} +Drawing Pie Charts +\begin{lstlisting} +In []: pie(science.values(), labels=science.keys()) +\end{lstlisting} Numpy Arrays -\begin{verbatim} -In [1]: a = array([1, 2, 3]) #Creating -In [2]: b = array([4, 5, 6]) -In [3]: a + b #Sum; Element-wise -\end{verbatim} +\begin{lstlisting} +In []: a = array([1, 2, 3]) #Creating +In []: b = array([4, 5, 6]) +In []: a + b #Sum; Element-wise +\end{lstlisting} Numpy statistical operations -\begin{verbatim} -In [1]: mean(math_scores) -In [2]: median(math_scores) -In [3]: stats.mode(math_scores) -In [4]: std(math_scores) -\end{verbatim} -Generating Van der Monde matrix -\begin{verbatim} -In [1]: A = vander(L, 2) -\end{verbatim} -Getting a Least Squares Fit curve -\begin{verbatim} -In [1]: coef, res, r, s = lstsq(A,TSq) -In [2]: Tline = coef[0]*L + coef[1] -In [3]: plot(L, Tline) -\end{verbatim} +\begin{lstlisting} +In []: mean(math_scores) +In []: median(math_scores) +In []: std(math_scores) +\end{lstlisting} \end{document} - diff -r d47d36d057a7 -r f87f2a310abe day1/cheatsheet4.tex --- a/day1/cheatsheet4.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/cheatsheet4.tex Tue Nov 10 17:22:07 2009 +0530 @@ -1,47 +1,148 @@ \documentclass[12pt]{article} -\title{Matrices and Solution of Equations} +\title{Matrices and Least Square Fit} \author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, + commentstyle=\itshape\bfseries, + showstringspaces=false, +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + \begin{document} \date{} \vspace{-1in} \begin{center} -\LARGE{Matrices and Solution of Equations}\\ +\LARGE{Matrices and Least Square Fit}\\ \large{FOSSEE} \end{center} \section{Matrices} Inputting a Matrix -\begin{verbatim} -In [1]: A = matrix([[1, 2, 3],[4, 5, 6]]) -\end{verbatim} +\begin{lstlisting} +In []: C = array([[1,1,2], + [2,4,1], + [-1,3,7]]) +In []: B = ones_like(C) +In []: A = ones((3,2)) +In []: I = identity(3) +\end{lstlisting} +Accessing Elements +\begin{lstlisting} +In []: C[1,2] +Out[]: 1 + +In []: C[1] +Out[]: array([2, 4, 1]) +\end{lstlisting} + +Changing elements +\begin{lstlisting} +In []: C[1,1] = -2 +In []: C +Out[]: +array([[ 1, 1, 2], + [ 2, -2, 1], + [-1, 3, 7]]) + +In []: C[1] = [0,0,0] +In []: C +Out[]: +array([[ 1, 1, 2], + [ 0, 0, 0], + [-1, 3, 7]]) +\end{lstlisting} + +Slicing +\begin{lstlisting} +In []: C[:,1] +Out[]: array([1, 0, 3]) + +In []: C[1,:] +Out[]: array([0, 0, 0]) + +In []: C[0:2,:] +Out[]: +array([[1, 1, 2], + [0, 0, 0]]) + +In []: C[1:3,:] +Out[]: +array([[ 0, 0, 0], + [-1, 3, 7]]) + +In []: C[:2,:] +Out[]: +array([[1, 1, 2], + [0, 0, 0]]) + +In []: C[1:,:] +Out[]: +array([[ 0, 0, 0], + [-1, 3, 7]]) + +In []: C[1:,:2] +Out[]: +array([[ 0, 0], + [-1, 3]]) +\end{lstlisting} + +Striding +\begin{lstlisting} +In []: C[::2,:] +Out[]: +array([[ 1, 1, 2], + [-1, 3, 7]]) + +In []: C[:,::2] +Out[]: +xarray([[ 1, 2], + [ 0, 0], + [-1, 7]]) + +In []: C[::2,::2] +Out[]: +array([[ 1, 2], + [-1, 7]]) +\end{lstlisting} + Matrix Operations -\begin{verbatim} -In [1]: A.T # Transpose -In [2]: sum(A) # Sum of all elements -In [3]: A+B # Addition -In [1]: A*B # Product -In [1]: inv(A) # Inverse -In [1]: det(A) # Determinant -\end{verbatim} +\begin{lstlisting} +In []: A.T # Transpose +In []: sum(A) # Sum of all elements +In []: A+B # Addition +In []: A*B # Product +In []: inv(A) # Inverse +In []: det(A) # Determinant +\end{lstlisting} Eigen Values and Eigen Vectors -\begin{verbatim} -In [1]: eig(A) #Eigen Values and Vectors -In [2]: eigvals(A) #Eigen Values -\end{verbatim} -Norm -\begin{verbatim} -In [1]: norm(A) -\end{verbatim} -Single Value Decomposition -\begin{verbatim} -In [1]: svd(A) -\end{verbatim} -Solving a set of equations -\begin{verbatim} -In [1]: A = matrix([...]) # Input Equation Coefficient Matrix -In [2]: b = matrix([...]) # Equation Target Values -In [3]: x = solve(A, b) -In [4]: Ax = A*x -\end{verbatim} +\begin{lstlisting} +In []: eig(A) #Eigen Values and Vectors +In []: eigvals(A) #Eigen Values +\end{lstlisting} +%% Norm +%% \begin{lstlisting} +%% In []: norm(A) +%% \end{lstlisting} +%% Single Value Decomposition +%% \begin{lstlisting} +%% In []: svd(A) +%% \end{lstlisting} +Least Square Fit Line +\begin{lstlisting} +In []: A = array([L, ones_like(L)]) +In []: A = A.T +In []: result = lstsq(A,TSq) +In []: coef = result[0] +In []: Tline = coef[0]*L + coef[1] +In []: plot(L, Tline) +\end{lstlisting} + \end{document} - diff -r d47d36d057a7 -r f87f2a310abe day1/cheatsheet6.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day1/cheatsheet6.tex Tue Nov 10 17:22:07 2009 +0530 @@ -0,0 +1,44 @@ +\documentclass[12pt]{article} +\title{Solving Equations \& ODEs} +\author{FOSSEE} +\begin{document} +\date{} +\vspace{-1in} +\begin{center} +\LARGE{Solving Equations \& ODEs}\\ +\large{FOSSEE} +\end{center} +\section{Solving linear equations} +\begin{verbatim} + In []: A = array([[3,2,-1], + [2,-2,4], + [-1, 0.5, -1]]) + In []: b = array([[1], [-2], [0]]) + In []: x = solve(A, b) + In []: Ax = dot(A,x) + In []: allclose(Ax, b) + Out[]: True +\end{verbatim} +\section{Finding roots} +\begin{verbatim} + In []: coeffs = [1, 6, 13] + In []: roots(coeffs) +\end{verbatim} +Finding the roots of a function +\begin{verbatim} +In []: fsolve(sin(x)+cos(x)**2, 0) +\end{verbatim} +\section{ODE} +\begin{verbatim} + In []: def epid(y, t): + .... k, L = 0.00003, 25000 + .... return k*y*(L-y) + .... + + In []: t = arange(0, 12, 0.2) + + In []: y = odeint(epid, 250, t) + + In []: plot(t, y) +\end{verbatim} +\end{document} diff -r d47d36d057a7 -r f87f2a310abe day1/session2.tex --- a/day1/session2.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/session2.tex Tue Nov 10 17:22:07 2009 +0530 @@ -338,7 +338,7 @@ \end{lstlisting} \ldots \begin{block}{Windows users:} - >type pendulum.txt + > type pendulum.txt \end{block} \end{frame} diff -r d47d36d057a7 -r f87f2a310abe day1/session3.tex --- a/day1/session3.tex Tue Nov 10 16:27:03 2009 +0530 +++ b/day1/session3.tex Tue Nov 10 17:22:07 2009 +0530 @@ -144,9 +144,9 @@ \begin{lstlisting} In []: G = [] In []: for line in open('pendulum.txt'): - .... points = line.split() - .... l = float(points[0]) - .... t = float(points[1]) + .... point = line.split() + .... l = float(point[0]) + .... t = float(point[1]) .... g = 4 * pi * pi * l / t * t .... G.append(g) \end{lstlisting} @@ -166,24 +166,24 @@ for g in G: total += g -mean_g = total / len(g) -print "Mean: ", mean_g +g_mean = total / len(g) +print "Mean: ", g_mean \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Mean ``g''} \begin{lstlisting} -mean_g = sum(G) / len(G) -print "Mean: ", mean_g +g_mean = sum(G) / len(G) +print "Mean: ", g_mean \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Mean ``g''} \begin{lstlisting} -mean_g = mean(G) -print "Mean: ", mean_g +g_mean = mean(G) +print "Mean: ", g_mean \end{lstlisting} \inctime{10} \end{frame} @@ -248,7 +248,6 @@ \item File reading \item Parsing \item Dictionaries - \item List enumeration \item Arrays \item Statistical operations \end{itemize} @@ -321,6 +320,10 @@ \item Keys will be region codes \item Values will be the number students who scored more than 90\% in that region \end{itemize} + \begin{block}{Sample \emph{science} dictionary} + \{'A': 729, 'C': 764, 'B': 1120,'E': 414, 'D': 603, 'F': 500\} + \end{block} + \end{frame} \begin{frame}[fragile] diff -r d47d36d057a7 -r f87f2a310abe day2/session3.tex