day2/session1.tex
changeset 66 523f788d2147
parent 46 63704b5650f1
child 76 b24c2560f626
equal deleted inserted replaced
58:ab9192cfe870 66:523f788d2147
   144 
   144 
   145 \begin{frame}[fragile]
   145 \begin{frame}[fragile]
   146   \frametitle{Examples of \num}
   146   \frametitle{Examples of \num}
   147 \begin{lstlisting}
   147 \begin{lstlisting}
   148 # Simple array math example
   148 # Simple array math example
   149 >>> from numpy import *
   149 >>> import numpy as np
   150 >>> a = array([1,2,3,4])
   150 >>> a = np.array([1,2,3,4])
   151 >>> b = array([2,3,4,5])
   151 >>> b = np.arange(2,6)
       
   152 >>> b
       
   153 array([2,3,4,5])
   152 >>> a*2 + b + 1 # Basic math!
   154 >>> a*2 + b + 1 # Basic math!
   153 array([5, 8, 11, 14])
   155 array([5, 8, 11, 14])
       
   156 \end{lstlisting}
       
   157 \end{frame}
       
   158 
       
   159 \begin{frame}[fragile]
       
   160   \frametitle{Examples of \num}
       
   161 \begin{lstlisting}
   154 # Pi and e are defined.
   162 # Pi and e are defined.
   155 >>> x = linspace(0.0, 10.0, 1000)
   163 >>> x = np.linspace(0.0, 10.0, 1000)
   156 >>> x *= 2*pi/10 # inplace.
   164 >>> x *= 2*np.pi/10 # inplace.
   157 # apply functions to array.
   165 # apply functions to array.
   158 >>> y = sin(x)
   166 >>> y = np.sin(x)
       
   167 >>> z = np.exp(y)
   159 \end{lstlisting}
   168 \end{lstlisting}
   160 \inctime{5}
   169 \inctime{5}
   161 \end{frame}
   170 \end{frame}
   162 
   171 
   163 \begin{frame}
   172 \begin{frame}
   176 
   185 
   177 \begin{frame}[fragile]
   186 \begin{frame}[fragile]
   178   \frametitle{More examples of \num}
   187   \frametitle{More examples of \num}
   179 \vspace*{-8pt}
   188 \vspace*{-8pt}
   180 \begin{lstlisting}
   189 \begin{lstlisting}
   181 >>> x = array([1., 2, 3, 4])
   190 >>> x = np.array([1., 2, 3, 4])
   182 >>> size(x)
   191 >>> np.size(x)
   183 4
   192 4
   184 >>> x.dtype # What is a.dtype?
   193 >>> x.dtype # What is a.dtype?
   185 dtype('float64')
   194 dtype('float64')
   186 >>> x.shape
   195 >>> x.shape
   187 (4,)
   196 (4,)
   188 >>> print rank(x), x.itemsize
   197 >>> print np.rank(x), x.itemsize
   189 1 8
   198 1 8
   190 >>> x[0] = 10
   199 >>> x[0] = 10
   191 >>> print x[0], x[-1]
   200 >>> print x[0], x[-1]
   192 10.0 4.0
   201 10.0 4.0
   193 \end{lstlisting}
   202 \end{lstlisting}
   194 \end{frame}
   203 \end{frame}
   195 
   204 
   196 \begin{frame}[fragile]
   205 \begin{frame}[fragile]
   197   \frametitle{Multi-dimensional arrays}
   206   \frametitle{Multi-dimensional arrays}
   198 \begin{lstlisting}
   207 \begin{lstlisting}
   199 >>> a = array([[ 0, 1, 2, 3],
   208 >>> a = np.array([[ 0, 1, 2, 3],
   200 ...            [10,11,12,13]])
   209 ...            [10,11,12,13]])
   201 >>> a.shape # (rows, columns)
   210 >>> a.shape # (rows, columns)
   202 (2, 4)
   211 (2, 4)
   203 # Accessing and setting values
   212 # Accessing and setting values
   204 >>> a[1,3] 
   213 >>> a[1,3] 
   205 13
   214 13
   206 >>> a[1,3] = -1
   215 >>> a[1,3] = -1
   207 >>> a[1] # The second row
   216 >>> a[1] # The second row
   208 array([10,11,12,-1])
   217 array([10,11,12,-1])
   209 
   218 \end{lstlisting}
   210 \end{lstlisting}
   219 \end{frame}
   211 \end{frame}
   220 
   212 \begin{frame}[fragile]
   221 \begin{frame}[fragile]
   213   \frametitle{Array math}
   222   \frametitle{Array math}
   214   \begin{itemize}
   223   \begin{itemize}
   215   \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
   224   \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
   216       \typ{+, -, *, /, \%}
   225       \typ{+, -, *, /, \%}
   217   \item Inplace operators: \typ{a += b}, or \typ{add(a, b, a)} etc. 
   226   \item Inplace operators: \typ{a += b}, or \typ{np.add(a, b, a)} etc. 
   218   \item \typ{sum(x, axis=0)}, 
   227   \item \typ{np.sum(x, axis=0)}, 
   219         \typ{product(x, axis=0)},
   228         \typ{np.product(x, axis=0)},
   220         \typ{dot(a, bp)}   
   229         \typ{np.dot(a, bp)}   
   221   \end{itemize}
   230   \end{itemize}
       
   231 \begin{lstlisting}
       
   232 >>> b=np.array([[0,2,4,2],[1,2,3,4]])
       
   233 >>> np.add(a,b,a)
       
   234 >>> np.sum(x,axis=1)
       
   235 \end{lstlisting}
   222 \end{frame}
   236 \end{frame}
   223 
   237 
   224 \begin{frame}[fragile]
   238 \begin{frame}[fragile]
   225   \frametitle{Array math cont.}
   239   \frametitle{Array math cont.}
   226   \begin{itemize}
   240   \begin{itemize}
   227   \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
   241   \item Logical operations: \typ{np.equal (==)}, \typ{np.not\_equal (!=)},
   228     \typ{less (<)}, \typ{greater (>)} etc.
   242     \typ{np.less (<)}, \typ{np.greater (>)} etc.
   229   \item Trig and other functions: \typ{sin(x),}
   243   \item Trig and other functions: \typ{np.sin(x),}
   230         \typ{arcsin(x), sinh(x),}
   244         \typ{np.arcsin(x), np.sinh(x),}
   231       \typ{exp(x), sqrt(x)} etc.
   245       \typ{np.exp(x), np.sqrt(x)} etc.
   232   \end{itemize}
   246   \end{itemize}
       
   247 \begin{lstlisting}
       
   248 >>> np.greater(a,4)
       
   249 >>> np.sqrt(a)
       
   250 \end{lstlisting}
   233 \inctime{10}
   251 \inctime{10}
   234 \end{frame}
   252 \end{frame}
   235 
   253 
   236 \subsection{Array Creation \& Slicing, Striding Arrays}
   254 \subsection{Array Creation \& Slicing, Striding Arrays}
   237 \begin{frame}[fragile]
   255 \begin{frame}[fragile]
   238   \frametitle{Array creation functions}
   256   \frametitle{Array creation functions}
   239   \begin {block}{\typ{array(object, dtype=None, ...)}}
       
   240   \begin{lstlisting}
       
   241   >>> array( [2,3,4] )  
       
   242   array([2, 3, 4])
       
   243   \end{lstlisting}
       
   244   \end {block}
       
   245   \begin{block}{\typ{linspace(start, stop, num=50, ...)}}
       
   246   \begin{lstlisting}
       
   247   >>> linspace( 0, 2, 4 )   
       
   248   array([0.,0.6666667,1.3333333,2.])
       
   249   \end{lstlisting}
       
   250   \end{block}
       
   251   \begin{itemize}
   257   \begin{itemize}
   252   \item also try \typ{arange} command
   258     \item {\typ{np.array(object,dtype=None,...)}
       
   259     \begin{lstlisting}
       
   260 >>> np.array([2,3,4])  
       
   261 array([2, 3, 4])
       
   262     \end{lstlisting}
       
   263     \item \typ{np.linspace(start,stop,...)}
       
   264     \begin{lstlisting}
       
   265 >>> np.linspace(0, 2, 4)   
       
   266 array([0.,0.6666667,1.3333333,2.])
       
   267     \end{lstlisting}
       
   268     \item Also try \typ{np.arange}
   253   \end{itemize}
   269   \end{itemize}
   254 \end{frame}
   270 \end{frame}
   255 
   271 
   256 \begin{frame}[fragile]
   272 \begin{frame}[fragile]
   257   \frametitle{Array creation functions cont.}
   273   \frametitle{Array creation functions cont.}
   258   \begin{itemize}  
   274   \begin{itemize}  
   259   \item \typ{ones(shape, dtype=None, ...)}  
   275   \item \typ{np.ones(shape, dtype=None, ...)}  
   260   \begin{lstlisting} 
   276   \begin{lstlisting} 
   261   >>>ones([2,2])
   277 >>>np.ones([2,2])
   262   array([[ 1.,  1.],
   278 array([[ 1.,  1.],
   263        [ 1.,  1.]])
   279      [ 1.,  1.]])
   264   \end{lstlisting}  
   280   \end{lstlisting}  
   265   \item \typ{identity(n)} 
   281   \item \typ{np.identity(n)} 
   266   \item \typ{ones\_like(x)}  
   282   \item \typ{np.ones\_like(x)}  
   267   \begin{lstlisting} 
   283   \begin{lstlisting} 
   268   >>>a = array([[1,2,3],[4,5,6]])
   284 >>>a = np.array([[1,2,3],[4,5,6]])
   269   >>>ones_like(a)
   285 >>>np.ones_like(a)
   270     array([[1, 1, 1],
   286 array([[1, 1, 1],
   271            [1, 1, 1]])
   287        [1, 1, 1]])
   272   \end{lstlisting}
   288   \end{lstlisting}
   273   \item check out \typ{zeros, zeros\_like, empty}
   289   \item Also try \typ{zeros, zeros\_like, empty}
   274   \end{itemize}
   290   \end{itemize}
   275 \end{frame}
   291 \end{frame}
   276 
   292 
   277 \begin{frame}[fragile]
   293 \begin{frame}[fragile]
   278   \frametitle{Slicing arrays}
   294   \frametitle{Slicing arrays}
   279 \begin{lstlisting}
   295 \begin{lstlisting}
   280 >>> a = array([[1,2,3], [4,5,6], 
   296 >>> a = np.array([[1,2,3], [4,5,6], 
   281                [7,8,9]])
   297                [7,8,9]])
   282 >>> a[0,1:3]
   298 >>> a[0,1:3]
   283 array([2, 3])
   299 array([2, 3])
   284 >>> a[1:,1:]
   300 >>> a[1:,1:]
   285 array([[5, 6],
   301 array([[5, 6],
   325     \begin{enumerate}
   341     \begin{enumerate}
   326     \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $
   342     \item Convert an RGB image to Grayscale. $ Y = 0.5R + 0.25G + 0.25B $
   327     \item Scale the image to 50\%
   343     \item Scale the image to 50\%
   328     \item Introduce some random noise
   344     \item Introduce some random noise
   329     \item Smooth the image using a mean filter
   345     \item Smooth the image using a mean filter
   330       \\\small{Take the mean of all the neighbouring elements}
   346       \\\small{Each element in the array is replaced by mean of all the neighbouring elements}
   331       \\\small{How fast can you do it?}
   347       \\\small{How fast does your code run?}
   332     \end{enumerate}
   348     \end{enumerate}
   333 \inctime{15}
   349 \inctime{15}
   334 \end{frame}
   350 \end{frame}
   335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   336 
   352 
   406 \end{frame}
   422 \end{frame}
   407 
   423 
   408 \begin{frame}[fragile]
   424 \begin{frame}[fragile]
   409   \frametitle{Legends}
   425   \frametitle{Legends}
   410 \begin{lstlisting}
   426 \begin{lstlisting}
   411 >>> x = linspace(0, 2*pi, 1000)
   427 >>> x = linspace(0, 2*np.pi, 1000)
   412 >>> plot(x, cos(5*x), 'r--', 
   428 >>> plot(x, cos(5*x), 'r--', 
   413          label='cosine')
   429          label='cosine')
   414 >>> plot(x, sin(5*x), 'g--', 
   430 >>> plot(x, sin(5*x), 'g--', 
   415          label='sine')
   431          label='sine')
   416 >>> legend() 
   432 >>> legend() 
   433     
   449     
   434 
   450 
   435 \end{frame}
   451 \end{frame}
   436 
   452 
   437 \begin{frame}[fragile]
   453 \begin{frame}[fragile]
   438     \frametitle{Note: \typ{pylab} in Python scripts}
   454     \frametitle{\typ{pylab} in Python scripts}
   439 \begin{lstlisting}
   455 \begin{lstlisting}
   440 import pylab
   456 import pylab
   441 x = pylab.linspace(0, 20, 1000)
   457 x = pylab.linspace(0, 20, 1000)
   442 pylab.plot(x, pylab.sin(x))
   458 pylab.plot(x, pylab.sin(x))
   443 
   459 
   444 # Can also use:
   460 # Can also use:
   445 from pylab import linspace, sin, plot
   461 from pylab import linspace, sin, plot
   446 \end{lstlisting}
   462 \end{lstlisting}
   447 \inctime{5}
   463 \inctime{10}
   448 \end{frame}
   464 \end{frame}
   449 
   465 
   450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   451 \subsection{Types of Plots}
   467 \subsection{Types of Plots}
   452 \begin{frame}[fragile]
   468 \begin{frame}[fragile]
   740   \includegraphics[height=2.3in, interpolate=true]{data/plotmap}  
   756   \includegraphics[height=2.3in, interpolate=true]{data/plotmap}  
   741   \begin{center}
   757   \begin{center}
   742     \tiny
   758     \tiny
   743     For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
   759     For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
   744   \end{center}
   760   \end{center}
   745 \inctime{5}
       
   746 \end{frame}
   761 \end{frame}
   747 
   762 
   748 
   763 
   749 \begin{frame}
   764 \begin{frame}
   750   \frametitle{More information}
   765   \frametitle{More information}
   751   \begin{itemize}
   766   \begin{itemize}
   752   \item More information here: \url{http://matplotlib.sf.net}
   767   \item More information here: \url{http://matplotlib.sf.net}
   753   \item \url{http://matplotlib.sf.net/tutorial.html}
   768   \item \url{http://matplotlib.sf.net/tutorial.html}
   754   \item \url{http://matplotlib.sf.net/screenshots.html}
   769   \item \url{http://matplotlib.sf.net/screenshots.html}
   755   \end{itemize}
   770   \end{itemize}
       
   771 \inctime{5}
   756 \end{frame}
   772 \end{frame}
   757 
   773 
   758 \begin{frame}
   774 \begin{frame}
   759   \frametitle{Problem Set}
   775   \frametitle{Problem Set}
   760   \begin{enumerate}
   776   \begin{enumerate}