day2/session1.tex
changeset 4 e5047bcbb608
parent 3 18a010e4caa0
child 6 1f9492506ba2
equal deleted inserted replaced
3:18a010e4caa0 4:e5047bcbb608
   156 >>> x = linspace(0.0, 10.0, 1000)
   156 >>> x = linspace(0.0, 10.0, 1000)
   157 >>> x *= 2*pi/10 # inplace.
   157 >>> x *= 2*pi/10 # inplace.
   158 # apply functions to array.
   158 # apply functions to array.
   159 >>> y = sin(x)
   159 >>> y = sin(x)
   160 \end{lstlisting}
   160 \end{lstlisting}
       
   161 \inctime{5}
   161 \end{frame}
   162 \end{frame}
   162 
   163 
   163 \begin{frame}
   164 \begin{frame}
   164   \frametitle{Basic concepts}
   165   \frametitle{Basic concepts}
   165   \begin{itemize}
   166   \begin{itemize}
   194 \end{lstlisting}
   195 \end{lstlisting}
   195     
   196     
   196 \inctime{10}
   197 \inctime{10}
   197 \end{frame}
   198 \end{frame}
   198 
   199 
   199 \begin{frame}
       
   200     {More Numpy}
       
   201 
       
   202     \begin{itemize}
       
   203         \item Multi-dimensional arrays
       
   204         \item Random number generation
       
   205     \end{itemize}
       
   206 
       
   207 \end{frame}
       
   208 
       
   209 \begin{frame}[fragile]
   200 \begin{frame}[fragile]
   210   \frametitle{Multi-dimensional arrays}
   201   \frametitle{Multi-dimensional arrays}
   211 \begin{lstlisting}
   202 \begin{lstlisting}
   212 >>> a = array([[ 0, 1, 2, 3],
   203 >>> a = array([[ 0, 1, 2, 3],
   213 ...            [10,11,12,13]])
   204 ...            [10,11,12,13]])
   220 >>> a[1] # The second row
   211 >>> a[1] # The second row
   221 array([10,11,12,-1])
   212 array([10,11,12,-1])
   222 
   213 
   223 \end{lstlisting}
   214 \end{lstlisting}
   224 \end{frame}
   215 \end{frame}
   225 
       
   226 \begin{frame}[fragile]
       
   227   \frametitle{Slicing arrays}
       
   228 \begin{lstlisting}
       
   229 >>> a = array([[1,2,3], [4,5,6], 
       
   230                [7,8,9]])
       
   231 >>> a[0,1:3]
       
   232 array([2, 3])
       
   233 >>> a[1:,1:]
       
   234 array([[5, 6],
       
   235        [8, 9]])
       
   236 >>> a[:,2]
       
   237 array([3, 6, 9])
       
   238 \end{lstlisting}
       
   239 \end{frame}
       
   240 \begin{frame}[fragile]
       
   241   \frametitle{Striding arrays}
       
   242 \begin{lstlisting}
       
   243 >>> a[0::2,0::2]
       
   244 array([[1, 3],
       
   245        [7, 9]])
       
   246 # Slices are references to the 
       
   247 # same memory!
       
   248 \end{lstlisting}
       
   249 \end{frame}
       
   250 
       
   251 \subsection{Array creation }
       
   252 \begin{frame}[fragile]
       
   253   \frametitle{Array creation functions}
       
   254   \begin{itemize}
       
   255   \item \typ{array(object, dtype=None, \ldots)}
       
   256   \item \typ{arange(start, stop=None, step=1 \ldots)}
       
   257   \item \typ{linspace(start, stop, num=50, \ldots)}
       
   258   \item \typ{ones(shape, dtype=None, \ldots)}
       
   259   \item \typ{zeros(shape, dtype=float,\ldots)}
       
   260   \item \typ{identity(n)}
       
   261   \item \typ{empty(shape, dtype=float,\ldots)}
       
   262   \item \typ{ones\_like(x)}, 
       
   263   \item \typ{zeros\_like(x)}, \typ{empty\_like(x)}
       
   264   \end{itemize}
       
   265 \end{frame}
       
   266 
       
   267 \subsection{Array math}
       
   268 \begin{frame}[fragile]
   216 \begin{frame}[fragile]
   269   \frametitle{Array math}
   217   \frametitle{Array math}
   270   \begin{itemize}
   218   \begin{itemize}
   271   \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
   219   \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
   272       \typ{+, -, *, /, \%}
   220       \typ{+, -, *, /, \%}
   277   \item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
   225   \item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
   278       exp(x), sqrt(x)} etc.
   226       exp(x), sqrt(x)} etc.
   279   \item \typ{sum(x, axis=0), product(x, axis=0)} 
   227   \item \typ{sum(x, axis=0), product(x, axis=0)} 
   280   \item \typ{dot(a, bp)}
   228   \item \typ{dot(a, bp)}
   281   \end{itemize}
   229   \end{itemize}
   282 \end{frame}
   230   \inctime{10}
   283 
   231 \end{frame}
   284 
   232 
       
   233 \subsection{Array Creation \& Slicing, Striding Arrays}
       
   234 \begin{frame}[fragile]
       
   235   \frametitle{Array creation functions}
       
   236   \begin{itemize}
       
   237   \item \typ{array(object, dtype=None, \ldots)}
       
   238   \item \typ{arange(start, stop=None, step=1 \ldots)}
       
   239   \item \typ{linspace(start, stop, num=50, \ldots)}
       
   240   \item \typ{ones(shape, dtype=None, \ldots)}
       
   241   \item \typ{zeros(shape, dtype=float,\ldots)}
       
   242   \item \typ{identity(n)}
       
   243   \item \typ{empty(shape, dtype=float,\ldots)}
       
   244   \item \typ{ones\_like(x)}, 
       
   245   \item \typ{zeros\_like(x)}, \typ{empty\_like(x)}
       
   246   \end{itemize}
       
   247 \end{frame}
       
   248 
       
   249 \begin{frame}[fragile]
       
   250   \frametitle{Slicing arrays}
       
   251 \begin{lstlisting}
       
   252 >>> a = array([[1,2,3], [4,5,6], 
       
   253                [7,8,9]])
       
   254 >>> a[0,1:3]
       
   255 array([2, 3])
       
   256 >>> a[1:,1:]
       
   257 array([[5, 6],
       
   258        [8, 9]])
       
   259 >>> a[:,2]
       
   260 array([3, 6, 9])
       
   261 \end{lstlisting}
       
   262 \end{frame}
       
   263 
       
   264 \begin{frame}[fragile]
       
   265   \frametitle{Striding arrays}
       
   266 \begin{lstlisting}
       
   267 >>> a[0::2,0::2]
       
   268 array([[1, 3],
       
   269        [7, 9]])
       
   270 # Slices are references to the 
       
   271 # same memory!
       
   272 \end{lstlisting}
       
   273 \end{frame}
       
   274 
       
   275 \begin{frame}[fragile]
       
   276 \frametitle{Random Numbers}
       
   277 \begin{lstlisting}
       
   278 >>> np.random.rand(3,2)
       
   279 array([[ 0.96276665,  0.77174861],
       
   280        [ 0.35138557,  0.61462271],
       
   281        [ 0.16789255,  0.43848811]])
       
   282 >>> np.random.randint(1,100)
       
   283 42
       
   284 \end{lstlisting}
       
   285 \inctime{15}
       
   286 \end{frame}
       
   287 
       
   288 \begin{frame}[fragile]
       
   289   \frametitle{Problem set 1.0}
       
   290 \inctime{15}
       
   291 \end{frame}
   285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   293 
   286 \section{2D Plotting}
   294 \section{2D Plotting}
   287 \subsection{Getting Started}
   295 \subsection{Getting Started}
   288 
   296 
   289 \begin{frame}
   297 \begin{frame}
   290     {IPython's \typ{pylab} mode}
   298     {IPython's \typ{pylab} mode}
   312 \begin{itemize}
   320 \begin{itemize}
   313   \item Also: PNG, PDF, PS, EPS, SVG, PDF
   321   \item Also: PNG, PDF, PS, EPS, SVG, PDF
   314 \end{itemize}
   322 \end{itemize}
   315 \end{frame}
   323 \end{frame}
   316        
   324        
   317 \subsection{Playing with lines}
   325 \subsection{Plots - Lines, Labels and Legends}
   318 \begin{frame}[fragile]
   326 \begin{frame}[fragile]
   319   \frametitle{Basic plotting \ldots}
   327   \frametitle{Basic plotting \ldots}
   320 \begin{lstlisting}
   328 \begin{lstlisting}
   321 # Set properties of objects:
   329 # Set properties of objects:
   322 >>> l, = plot(x, sin(x))
   330 >>> l, = plot(x, sin(x))
   341 >>> figure(1)
   349 >>> figure(1)
   342 >>> title('Easy as 1,2,3')
   350 >>> title('Easy as 1,2,3')
   343 \end{lstlisting}
   351 \end{lstlisting}
   344     
   352     
   345 \end{frame}
   353 \end{frame}
   346 
       
   347 \subsection{Handling Text}
       
   348 
   354 
   349 \begin{frame}[fragile]
   355 \begin{frame}[fragile]
   350   \frametitle{Legends and Annotation}
   356   \frametitle{Legends and Annotation}
   351 \begin{lstlisting}
   357 \begin{lstlisting}
   352 >>> plot(x, cos(5*x), 'r--', 
   358 >>> plot(x, cos(5*x), 'r--', 
   614 r = arange(0,1,0.001)
   620 r = arange(0,1,0.001)
   615 theta = 2*2*pi*r
   621 theta = 2*2*pi*r
   616 polar(theta, r, color='#ee8d18', lw=3)
   622 polar(theta, r, color='#ee8d18', lw=3)
   617 # the radius of the grid labels
   623 # the radius of the grid labels
   618 setp(ax.thetagridlabels, y=1.075) 
   624 setp(ax.thetagridlabels, y=1.075) 
   619 title(r"$\theta=4\pi r", fontsize=20)
   625 title(r'$\theta=4\pi r$', fontsize=20)
   620 \end{lstlisting}
   626 \end{lstlisting}
       
   627 
   621   \end{block}
   628   \end{block}
   622 \end{columns}
   629 \end{columns}
   623 \end{frame}
   630 \end{frame}
   624 
   631 
   625 \begin{frame}[fragile] \frametitle{Contours}
   632 \begin{frame}[fragile] \frametitle{Contours}