diff options
Diffstat (limited to 'day2/session1.tex')
-rw-r--r-- | day2/session1.tex | 118 |
1 files changed, 67 insertions, 51 deletions
diff --git a/day2/session1.tex b/day2/session1.tex index 53ee07a..1d80bd4 100644 --- a/day2/session1.tex +++ b/day2/session1.tex @@ -146,16 +146,25 @@ \frametitle{Examples of \num} \begin{lstlisting} # Simple array math example ->>> from numpy import * ->>> a = array([1,2,3,4]) ->>> b = array([2,3,4,5]) +>>> import numpy as np +>>> a = np.array([1,2,3,4]) +>>> b = np.arange(2,6) +>>> b +array([2,3,4,5]) >>> a*2 + b + 1 # Basic math! array([5, 8, 11, 14]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Examples of \num} +\begin{lstlisting} # Pi and e are defined. ->>> x = linspace(0.0, 10.0, 1000) ->>> x *= 2*pi/10 # inplace. +>>> x = np.linspace(0.0, 10.0, 1000) +>>> x *= 2*np.pi/10 # inplace. # apply functions to array. ->>> y = sin(x) +>>> y = np.sin(x) +>>> z = np.exp(y) \end{lstlisting} \inctime{5} \end{frame} @@ -178,14 +187,14 @@ array([5, 8, 11, 14]) \frametitle{More examples of \num} \vspace*{-8pt} \begin{lstlisting} ->>> x = array([1., 2, 3, 4]) ->>> size(x) +>>> x = np.array([1., 2, 3, 4]) +>>> np.size(x) 4 >>> x.dtype # What is a.dtype? dtype('float64') >>> x.shape (4,) ->>> print rank(x), x.itemsize +>>> print np.rank(x), x.itemsize 1 8 >>> x[0] = 10 >>> print x[0], x[-1] @@ -196,7 +205,7 @@ dtype('float64') \begin{frame}[fragile] \frametitle{Multi-dimensional arrays} \begin{lstlisting} ->>> a = array([[ 0, 1, 2, 3], +>>> a = np.array([[ 0, 1, 2, 3], ... [10,11,12,13]]) >>> a.shape # (rows, columns) (2, 4) @@ -206,78 +215,85 @@ dtype('float64') >>> a[1,3] = -1 >>> a[1] # The second row array([10,11,12,-1]) - \end{lstlisting} \end{frame} + \begin{frame}[fragile] \frametitle{Array math} \begin{itemize} \item Basic \alert{elementwise} math (given two arrays \typ{a, b}): \typ{+, -, *, /, \%} - \item Inplace operators: \typ{a += b}, or \typ{add(a, b, a)} etc. - \item \typ{sum(x, axis=0)}, - \typ{product(x, axis=0)}, - \typ{dot(a, bp)} + \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc. + \item \typ{np.sum(x, axis=0)}, + \typ{np.product(x, axis=0)}, + \typ{np.dot(a, bp)} \end{itemize} +\begin{lstlisting} +>>> b=np.array([[0,2,4,2],[1,2,3,4]]) +>>> np.add(a,b,a) +>>> np.sum(x,axis=1) +\end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Array math cont.} \begin{itemize} - \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)}, - \typ{less (<)}, \typ{greater (>)} etc. - \item Trig and other functions: \typ{sin(x),} - \typ{arcsin(x), sinh(x),} - \typ{exp(x), sqrt(x)} etc. + \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)}, + \typ{np.less (<)}, \typ{np.greater (>)} etc. + \item Trig and other functions: \typ{np.sin(x),} + \typ{np.arcsin(x), np.sinh(x),} + \typ{np.exp(x), np.sqrt(x)} etc. \end{itemize} +\begin{lstlisting} +>>> np.greater(a,4) +>>> np.sqrt(a) +\end{lstlisting} \inctime{10} \end{frame} \subsection{Array Creation \& Slicing, Striding Arrays} \begin{frame}[fragile] \frametitle{Array creation functions} - \begin {block}{\typ{array(object, dtype=None, ...)}} - \begin{lstlisting} - >>> array( [2,3,4] ) - array([2, 3, 4]) - \end{lstlisting} - \end {block} - \begin{block}{\typ{linspace(start, stop, num=50, ...)}} - \begin{lstlisting} - >>> linspace( 0, 2, 4 ) - array([0.,0.6666667,1.3333333,2.]) - \end{lstlisting} - \end{block} \begin{itemize} - \item also try \typ{arange} command + \item {\typ{np.array(object,dtype=None,...)} + \begin{lstlisting} +>>> np.array([2,3,4]) +array([2, 3, 4]) + \end{lstlisting} + \item \typ{np.linspace(start,stop,...)} + \begin{lstlisting} +>>> np.linspace(0, 2, 4) +array([0.,0.6666667,1.3333333,2.]) + \end{lstlisting} + \item Also try \typ{np.arange} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Array creation functions cont.} \begin{itemize} - \item \typ{ones(shape, dtype=None, ...)} + \item \typ{np.ones(shape, dtype=None, ...)} \begin{lstlisting} - >>>ones([2,2]) - array([[ 1., 1.], - [ 1., 1.]]) +>>>np.ones([2,2]) +array([[ 1., 1.], + [ 1., 1.]]) \end{lstlisting} - \item \typ{identity(n)} - \item \typ{ones\_like(x)} + \item \typ{np.identity(n)} + \item \typ{np.ones\_like(x)} \begin{lstlisting} - >>>a = array([[1,2,3],[4,5,6]]) - >>>ones_like(a) - array([[1, 1, 1], - [1, 1, 1]]) +>>>a = np.array([[1,2,3],[4,5,6]]) +>>>np.ones_like(a) +array([[1, 1, 1], + [1, 1, 1]]) \end{lstlisting} - \item check out \typ{zeros, zeros\_like, empty} + \item Also try \typ{zeros, zeros\_like, empty} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Slicing arrays} \begin{lstlisting} ->>> a = array([[1,2,3], [4,5,6], +>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> a[0,1:3] array([2, 3]) @@ -327,8 +343,8 @@ array([[ 0.96276665, 0.77174861], \item Scale the image to 50\% \item Introduce some random noise \item Smooth the image using a mean filter - \\\small{Take the mean of all the neighbouring elements} - \\\small{How fast can you do it?} + \\\small{Each element in the array is replaced by mean of all the neighbouring elements} + \\\small{How fast does your code run?} \end{enumerate} \inctime{15} \end{frame} @@ -408,7 +424,7 @@ array([[ 0.96276665, 0.77174861], \begin{frame}[fragile] \frametitle{Legends} \begin{lstlisting} ->>> x = linspace(0, 2*pi, 1000) +>>> x = linspace(0, 2*np.pi, 1000) >>> plot(x, cos(5*x), 'r--', label='cosine') >>> plot(x, sin(5*x), 'g--', @@ -435,7 +451,7 @@ array([[ 0.96276665, 0.77174861], \end{frame} \begin{frame}[fragile] - \frametitle{Note: \typ{pylab} in Python scripts} + \frametitle{\typ{pylab} in Python scripts} \begin{lstlisting} import pylab x = pylab.linspace(0, 20, 1000) @@ -444,7 +460,7 @@ pylab.plot(x, pylab.sin(x)) # Can also use: from pylab import linspace, sin, plot \end{lstlisting} -\inctime{5} +\inctime{10} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -742,7 +758,6 @@ title('triangular head; scale '\ \tiny For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py} \end{center} -\inctime{5} \end{frame} @@ -753,6 +768,7 @@ title('triangular head; scale '\ \item \url{http://matplotlib.sf.net/tutorial.html} \item \url{http://matplotlib.sf.net/screenshots.html} \end{itemize} +\inctime{5} \end{frame} \begin{frame} |