diff options
author | Prabhu Ramachandran | 2016-12-09 21:24:51 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2016-12-09 21:24:51 +0530 |
commit | 042b171b655a5b96dcedc3416c1b56035bf91cff (patch) | |
tree | 9189e9bb9e966e0cfb35b4679b9989e1d82f53ee | |
parent | de57336bcde096b21c2dd1265e3d789cd2c10d53 (diff) | |
download | python-workshops-042b171b655a5b96dcedc3416c1b56035bf91cff.tar.gz python-workshops-042b171b655a5b96dcedc3416c1b56035bf91cff.tar.bz2 python-workshops-042b171b655a5b96dcedc3416c1b56035bf91cff.zip |
Finish basic session3.
This removes the csv2rec. We should add pandas material later.
-rw-r--r-- | scipy/basic/README.txt | 13 | ||||
-rw-r--r-- | scipy/basic/session3.tex | 178 |
2 files changed, 65 insertions, 126 deletions
diff --git a/scipy/basic/README.txt b/scipy/basic/README.txt index 4cf4303..35d9c22 100644 --- a/scipy/basic/README.txt +++ b/scipy/basic/README.txt @@ -43,8 +43,7 @@ Basic Tutorial: - NumPy array basics: 1D arrays. - Reading data files. - More on NumPy: multi-dimensional arrays, slicing, elementary image - processing. - - Some powertools: pandas dataframe basics. + processing, random numbers. - Using SciPy for Linear Algebra, FFT's, root finding and integrating ODEs. @@ -54,11 +53,7 @@ Basic Tutorial: A laptop is definitely recommended since this will be a hands-on session. You will need to have Python (2.7/3.x would work fine), IPython (0.10.x), Numpy (1.x), SciPy (>0.5.x), matplotlib (any recent - version), Mayavi-3.x and above. + version). - On Windows and Mac OS X it is easiest to install these by installing - the Enthought Python Distribution. On GNU/Linux these are typically - available along with the distribution. The packages are all - available on Ubuntu. FOSSEE provides a live Ubuntu-based DVD with - all the necessary packages. It can be downloaded here: - http://fossee.in/download#DVDs + On Linux, Windows and Mac OS X it is easiest to install these by installing + the Enthought Canopy. diff --git a/scipy/basic/session3.tex b/scipy/basic/session3.tex index 6d603a5..2c923fd 100644 --- a/scipy/basic/session3.tex +++ b/scipy/basic/session3.tex @@ -1,14 +1,14 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Tutorial slides on Python. % -% Author: FOSSEE +% Author: FOSSEE % Copyright (c) 2009, FOSSEE, IIT Bombay %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \documentclass[14pt,compress]{beamer} %\documentclass[draft]{beamer} %\documentclass[compress,handout]{beamer} -%\usepackage{pgfpages} +%\usepackage{pgfpages} %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] % Modified from: generic-ornate-15min-45min.de.tex @@ -44,7 +44,7 @@ % Macros \setbeamercolor{emphbar}{bg=blue!20, fg=black} \newcommand{\emphbar}[1] -{\begin{beamercolorbox}[rounded=true]{emphbar} +{\begin{beamercolorbox}[rounded=true]{emphbar} {#1} \end{beamercolorbox} } @@ -78,11 +78,11 @@ Python} \subtitle{More on numpy arrays} -\author[Prabhu] {FOSSEE} +\author[FOSSEE] {FOSSEE} \institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {SciPy India, 2014\\ -December, 2014 +\date[] {SciPy India 2016\\ +Mumbai } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -109,7 +109,7 @@ December, 2014 } % If you wish to uncover everything in a step-wise fashion, uncomment -% the following command: +% the following command: %\beamerdefaultoverlayspecification{<+->} %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} @@ -143,7 +143,7 @@ In []: c = array([[11,12,13], [31,32,33]]) In []: c -Out[]: +Out[]: array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) @@ -155,16 +155,16 @@ array([[11, 12, 13], \begin{small} \begin{lstlisting} In []: ones((3,5)) -Out[]: +Out[]: array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]]) -In []: ones_like([1, 2, 3, 4]) -Out[]: array([1, 1, 1, 1]) +In []: ones_like([1, 2, 3, 4]) +Out[]: array([1, 1, 1, 1]) In []: identity(2) -Out[]: +Out[]: array([[ 1., 0.], [ 0., 1.]]) \end{lstlisting} @@ -178,7 +178,7 @@ Also available \alert{\typ{zeros, zeros_like, empty, empty_like}} \begin{small} \begin{lstlisting} In []: c -Out[]: +Out[]: array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) @@ -200,14 +200,14 @@ Out[]: array([21, 22, 23]) \begin{lstlisting} In []: c[1,1] = -22 In []: c -Out[]: +Out[]: array([[ 11, 12, 13], [ 21, -22, 23], [ 31, 32, 33]]) In []: c[1] = 0 In []: c -Out[]: +Out[]: array([[11, 12, 13], [ 0, 0, 0], [31, 32, 33]]) @@ -227,12 +227,12 @@ In []: c[1,:] Out[]: array([0, 0, 0]) In []: c[0:2,:] -Out[]: +Out[]: array([[11, 12, 13], [ 0, 0, 0]]) In []: c[1:3,:] -Out[]: +Out[]: array([[ 0, 0, 0], [31, 32, 33]]) \end{lstlisting} @@ -244,17 +244,17 @@ array([[ 0, 0, 0], \begin{small} \begin{lstlisting} In []: c[:2,:] -Out[]: +Out[]: array([[11, 12, 13], [ 0, 0, 0]]) In []: c[1:,:] -Out[]: +Out[]: array([[ 0, 0, 0], [31, 32, 33]]) In []: c[1:,:2] -Out[]: +Out[]: array([[ 0, 0], [31, 32]]) \end{lstlisting} @@ -267,18 +267,18 @@ array([[ 0, 0], \begin{small} \begin{lstlisting} In []: c[::2,:] -Out[]: +Out[]: array([[11, 12, 13], [31, 32, 33]]) In []: c[:,::2] -Out[]: +Out[]: array([[11, 13], [ 0, 0], [31, 33]]) In []: c[::2,::2] -Out[]: +Out[]: array([[11, 13], [31, 33]]) \end{lstlisting} @@ -289,7 +289,7 @@ array([[11, 13], \frametitle{Shape of a matrix} \begin{lstlisting} In []: c -Out[]: +Out[]: array([[11, 12, 13], [ 0, 0, 0], [31, 32, 33]]) @@ -365,7 +365,7 @@ In []: b = array([[3,2,-1,5], [-1,0.5,-1,-7], [9,-5,7,3]]) In []: a + b -Out[]: +Out[]: array([[ 4. , 3. , 1. , 4. ], [ 4. , 3. , 3. , 0. ], [ 1. , 1.5, -2. , -4. ], @@ -377,7 +377,7 @@ array([[ 4. , 3. , 1. , 4. ], \frametitle{Elementwise Multiplication} \begin{lstlisting} In []: a*b -Out[]: +Out[]: array([[ 3. , 2. , -2. , -5. ], [ 4. , -10. , -4. , -81. ], [ -2. , 0.5, 1. , -21. ], @@ -390,7 +390,7 @@ array([[ 3. , 2. , -2. , -5. ], \frametitle{Matrix Multiplication} \begin{lstlisting} In []: dot(a, b) -Out[]: +Out[]: array([[ -6. , 6. , -6. , -3. ], [-64. , 38.5, -44. , 35. ], [ 36. , -13.5, 24. , 35. ], @@ -406,7 +406,7 @@ array([[ -6. , 6. , -6. , -3. ], \begin{small} \begin{lstlisting} In []: inv(a) -Out[]: +Out[]: array([[-0.5 , 0.55, -0.15, 0.7 ], [ 0.75, -0.5 , 0.5 , -0.75], [ 0.5 , -0.15, -0.05, -0.1 ], @@ -437,7 +437,7 @@ Out[]: 12 In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) In []: eig(e) -Out[]: +Out[]: (array([-1., 8., -1.]), array([[-0.74535599, 0.66666667, -0.1931126 ], [ 0.2981424 , 0.33333333, -0.78664085], @@ -462,7 +462,7 @@ Out[]: 8.1240384046359608 \begin{small} \begin{lstlisting} In []: svd(e) -Out[]: +Out[]: (array( [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], @@ -488,7 +488,7 @@ Linear trend visible. \begin{frame}[fragile] \frametitle{$L$ vs. $T^2$ - Line} -This line does not make any mathematical sense. +This plot is not a straight line, can we do better? \vspace{-0.1in} \begin{figure} \includegraphics[width=4in]{data/L-Tsq-Line} @@ -508,34 +508,43 @@ This is what our intention is. \frametitle{Matrix Formulation} \begin{itemize} \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ -\item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, where $T_{sq}$ is - $\begin{bmatrix} +\item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, +\item We need to find $p$ to plot the line +\end{itemize} +\end{frame} + +\begin{frame} + \begin{equation} + \begin{bmatrix} T^2_1 \\ T^2_2 \\ \vdots\\ T^2_N \\ - \end{bmatrix}$ -, A is - $\begin{bmatrix} +\end{bmatrix} += \begin{bmatrix} L_1 & 1 \\ L_2 & 1 \\ \vdots & \vdots\\ L_N & 1 \\ - \end{bmatrix}$ - and p is - $\begin{bmatrix} +\end{bmatrix} \cdot +\begin{bmatrix} m\\ c\\ - \end{bmatrix}$ -\item We need to find $p$ to plot the line -\end{itemize} + \end{bmatrix} +\end{equation} + +Or + +\[T_sq = A \cdot p \] + \end{frame} + \begin{frame}[fragile] \frametitle{Getting $L$ and $T^2$} %If you \alert{closed} IPython after session 2 \begin{lstlisting} -In []: L, t = loadtxt('pendulum.txt', +In []: L, t = loadtxt('pendulum.txt', ...: unpack=True) In []: tsq = t*t \end{lstlisting} @@ -577,13 +586,14 @@ In []: Tline = coef[0]*L + coef[1] In []: Tline.shape \end{lstlisting} \begin{itemize} -\item Now plot \typ{Tline} vs. \typ{L}, to get the least squares fit line. +\item Now plot \typ{Tline} vs. \typ{L}, to get the least squares fit line. \end{itemize} \begin{lstlisting} In []: plot(L, Tline, 'r') In []: plot(L, tsq, 'o') \end{lstlisting} +\inctime{10} \end{frame} \begin{frame}[fragile] @@ -592,7 +602,6 @@ In []: plot(L, tsq, 'o') \begin{figure} \includegraphics[width=4in]{data/least-sq-fit} \end{figure} -\inctime{10} \end{frame} \section{Random numbers} @@ -611,7 +620,7 @@ In []: import numpy In []: numpy.random \end{lstlisting} \begin{itemize} - \item \typ{random.random}: produces uniform deviates in $[0, 1)$ + \item \typ{random.random}: produces uniform deviates in $(0, 1)$ \item \typ{random.normal}: draws random samples from a Gaussian distribution \item Useful to create a random matrix of any shape @@ -621,11 +630,11 @@ In []: numpy.random \begin{frame}[fragile] \frametitle{Using the \typ{random} module} \begin{lstlisting} -In []: x = random.random(size=1000) -In []: y = random.random(size=1000) +In []: x = random.random(size=100) +In []: y = random.random(size=100) In []: scatter(x, y) # Scatter plot it. -In []: x,y = random.normal(size=(2,1000)) +In []: x,y = random.normal(size=(2,100)) In []: clf() In []: scatter(x, y) \end{lstlisting} @@ -637,76 +646,14 @@ Note that \typ{size} can be a tuple. \frametitle{Using the \typ{random} module} \begin{lstlisting} In []: img = random.random( - . size=(200, 200)) + ...: size=(200, 200) + ...: ) In []: clf() In []: imshow(img) \end{lstlisting} -\inctime{5} \end{frame} -\section{Record arrays} - -\begin{frame}[fragile] - \frametitle{Record arrays} - \begin{itemize} - \item Numpy arrays - \item Create custom data types - \item Consider: \\ - \typ{[ (id0, x0), (id1, x1), ...]} - \item \typ{id}: int - \item \typ{x}: float - - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{Record arrays} - \begin{lstlisting} -In []: typ = [('id', int), ('x', float)] -In []: rec = numpy.zeros(10, dtype=typ) -In []: rec['id'] = range(10) -In []: rec['x'] = random.random(size=10) - \end{lstlisting} - \begin{itemize} - \item Data type can be any valid numpy dtype - \item Very convenient - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\typ{csv2rec}} - \begin{itemize} - \item CSV file $\rightarrow$ Record array - \item Very convenient and powerful - \item Automatic naming - \item Handles missing data - \item Handles different delimiters - \end{itemize} - \begin{lstlisting} -In []: csv2rec? - \end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\typ{csv2rec} example data} - \begin{itemize} - \item \typ{data.csv} - \item First column ``x'' - \item Second column ``y'' - \item Third column ``temp'' - \end{itemize} -\end{frame} - -\begin{frame}[fragile] - \frametitle{\typ{csv2rec} example} - \begin{lstlisting} -In []: data = csv2rec('data.csv') -In []: scatter(data['x'], data['y'], - ...: c=data['temp']) -In []: colorbar() - \end{lstlisting} -\end{frame} \section{Summary} \begin{frame} @@ -715,10 +662,8 @@ In []: colorbar() \item Matrices \item Least Squares \item Random numbers - \item Record arrays - \item \typ{csv2rec} \end{itemize} - \inctime{5} +\inctime{10} \end{frame} \end{document} @@ -827,4 +772,3 @@ eigenvalues? %% $N\times3$ array called \lstinline+data+, how would you obtain the third %% column of this data? %% \end{frame} - |