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], |