day1/session4.tex
changeset 318 e75d3c993ed5
parent 304 c53251e506cc
child 319 cef948318842
equal deleted inserted replaced
306:57291186d598 318:e75d3c993ed5
   132 \end{frame}
   132 \end{frame}
   133 
   133 
   134 \begin{frame}[fragile]
   134 \begin{frame}[fragile]
   135 \frametitle{Matrices: Initializing}
   135 \frametitle{Matrices: Initializing}
   136 \begin{lstlisting}
   136 \begin{lstlisting}
   137 In []: A = array([[ 1,  1,  2, -1],
   137 In []: c = array([[1,1,2],
   138                   [ 2,  5, -1, -9],
   138                   [2,4,1],
   139                   [ 2,  1, -1,  3],
   139                   [-1,3,7]])
   140                   [ 1, -3,  2,  7]])
   140 
   141 In []: A
   141 In []: c
   142 Out[]: 
   142 Out[]: 
   143 array([[ 1,  1,  2, -1],
   143 array([[1,1,2],
   144        [ 2,  5, -1, -9],
   144        [2,4,1],
   145        [ 2,  1, -1,  3],
   145        [-1,3,7]])
   146        [ 1, -3,  2,  7]])
       
   147 \end{lstlisting}
   146 \end{lstlisting}
   148 \end{frame}
   147 \end{frame}
   149 
   148 
   150 \begin{frame}[fragile]
   149 \begin{frame}[fragile]
   151 \frametitle{Initializing some special matrices}
   150 \frametitle{Initializing some special matrices}
   171 
   170 
   172 
   171 
   173 \begin{frame}[fragile]
   172 \begin{frame}[fragile]
   174   \frametitle{Accessing elements}
   173   \frametitle{Accessing elements}
   175   \begin{lstlisting}
   174   \begin{lstlisting}
   176 In []: C = array([[1,1,2],
   175 In []: c
   177                   [2,4,1],
   176 Out[]: 
   178                   [-1,3,7]])
   177 array([[1,1,2],
   179 
   178        [2,4,1],
   180 In []: C[1][2]
   179        [-1,3,7]])
       
   180 
       
   181 In []: c[1][2]
   181 Out[]: 1
   182 Out[]: 1
   182 
   183 In []: c[1,2]
   183 In []: C[1,2]
       
   184 Out[]: 1
   184 Out[]: 1
   185 
   185 In []: c[1]
   186 In []: C[1]
       
   187 Out[]: array([2, 4, 1])
   186 Out[]: array([2, 4, 1])
   188   \end{lstlisting}
   187   \end{lstlisting}
   189 \end{frame}
   188 \end{frame}
   190 
   189 
   191 \begin{frame}[fragile]
   190 \begin{frame}[fragile]
   192   \frametitle{Changing elements}
   191   \frametitle{Changing elements}
   193   \begin{small}
   192   \begin{small}
   194   \begin{lstlisting}
   193   \begin{lstlisting}
   195 In []: C[1,1] = -2
   194 In []: c[1,1] = -2
   196 In []: C
   195 In []: c
   197 Out[]: 
   196 Out[]: 
   198 array([[ 1,  1,  2],
   197 array([[ 1,  1,  2],
   199        [ 2, -2,  1],
   198        [ 2, -2,  1],
   200        [-1,  3,  7]])
   199        [-1,  3,  7]])
   201 
   200 
   202 In []: C[1] = [0,0,0]
   201 In []: c[1] = [0,0,0]
   203 In []: C
   202 In []: c
   204 Out[]: 
   203 Out[]: 
   205 array([[ 1,  1,  2],
   204 array([[ 1,  1,  2],
   206        [ 0,  0,  0],
   205        [ 0,  0,  0],
   207        [-1,  3,  7]])
   206        [-1,  3,  7]])
   208   \end{lstlisting}
   207   \end{lstlisting}
   212 
   211 
   213 \begin{frame}[fragile]
   212 \begin{frame}[fragile]
   214   \frametitle{Slicing}
   213   \frametitle{Slicing}
   215 \begin{small}
   214 \begin{small}
   216   \begin{lstlisting}
   215   \begin{lstlisting}
   217 In []: C[:,1]
   216 In []: c[:,1]
   218 Out[]: array([1, 0, 3])
   217 Out[]: array([1, 0, 3])
   219 
   218 
   220 In []: C[1,:]
   219 In []: c[1,:]
   221 Out[]: array([0, 0, 0])
   220 Out[]: array([0, 0, 0])
   222 
   221 
   223 In []: C[0:2,:]
   222 In []: c[0:2,:]
   224 Out[]: 
   223 Out[]: 
   225 array([[1, 1, 2],
   224 array([[1, 1, 2],
   226        [0, 0, 0]])
   225        [0, 0, 0]])
   227 
   226 
   228 In []: C[1:3,:]
   227 In []: c[1:3,:]
   229 Out[]: 
   228 Out[]: 
   230 array([[ 0,  0,  0],
   229 array([[ 0,  0,  0],
   231        [-1,  3,  7]])
   230        [-1,  3,  7]])
   232   \end{lstlisting}
   231   \end{lstlisting}
   233 \end{small}
   232 \end{small}
   235 
   234 
   236 \begin{frame}[fragile]
   235 \begin{frame}[fragile]
   237   \frametitle{Slicing \ldots}
   236   \frametitle{Slicing \ldots}
   238 \begin{small}
   237 \begin{small}
   239   \begin{lstlisting}
   238   \begin{lstlisting}
   240 In []: C[:2,:]
   239 In []: c[:2,:]
   241 Out[]: 
   240 Out[]: 
   242 array([[1, 1, 2],
   241 array([[1, 1, 2],
   243        [0, 0, 0]])
   242        [0, 0, 0]])
   244 
   243 
   245 In []: C[1:,:]
   244 In []: c[1:,:]
   246 Out[]: 
   245 Out[]: 
   247 array([[ 0,  0,  0],
   246 array([[ 0,  0,  0],
   248        [-1,  3,  7]])
   247        [-1,  3,  7]])
   249 
   248 
   250 In []: C[1:,:2]
   249 In []: c[1:,:2]
   251 Out[]: 
   250 Out[]: 
   252 array([[ 0,  0],
   251 array([[ 0,  0],
   253        [-1,  3]])
   252        [-1,  3]])
   254   \end{lstlisting}
   253   \end{lstlisting}
   255 
   254 
   258 
   257 
   259 \begin{frame}[fragile]
   258 \begin{frame}[fragile]
   260   \frametitle{Striding}
   259   \frametitle{Striding}
   261   \begin{small}
   260   \begin{small}
   262   \begin{lstlisting}
   261   \begin{lstlisting}
   263 In []: C[::2,:]
   262 In []: c[::2,:]
   264 Out[]: 
   263 Out[]: 
   265 array([[ 1,  1,  2],
   264 array([[ 1,  1,  2],
   266        [-1,  3,  7]])
   265        [-1,  3,  7]])
   267 
   266 
   268 In []: C[:,::2]
   267 In []: c[:,::2]
   269 Out[]: 
   268 Out[]: 
   270 xarray([[ 1,  2],
   269 xarray([[ 1,  2],
   271        [ 0,  0],
   270        [ 0,  0],
   272        [-1,  7]])
   271        [-1,  7]])
   273 
   272 
   274 In []: C[::2,::2]
   273 In []: c[::2,::2]
   275 Out[]: 
   274 Out[]: 
   276 array([[ 1,  2],
   275 array([[ 1,  2],
   277        [-1,  7]])
   276        [-1,  7]])
   278   \end{lstlisting}
   277   \end{lstlisting}
   279   \end{small}
   278   \end{small}
   281 
   280 
   282 \begin{frame}[fragile]
   281 \begin{frame}[fragile]
   283   \frametitle{Slicing \& Striding Exercises}
   282   \frametitle{Slicing \& Striding Exercises}
   284 \begin{small}
   283 \begin{small}
   285   \begin{lstlisting}
   284   \begin{lstlisting}
   286 In []: A = imread('lena.png')
   285 In []: a = imread('lena.png')
   287 
   286 
   288 In []: imshow(A)
   287 In []: imshow(a)
   289 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc>
   288 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc>
   290 
   289 
   291 In []: A.shape 
   290 In []: a.shape 
   292 Out[]: (512, 512, 4)
   291 Out[]: (512, 512, 4)
   293   \end{lstlisting}
   292   \end{lstlisting}
   294 \end{small}
   293 \end{small}
   295   \begin{itemize}
   294   \begin{itemize}
   296   \item Crop the image to get the top-left quarter
   295   \item Crop the image to get the top-left quarter
   301 
   300 
   302 \begin{frame}[fragile]
   301 \begin{frame}[fragile]
   303   \frametitle{Solutions}
   302   \frametitle{Solutions}
   304 \begin{small}
   303 \begin{small}
   305   \begin{lstlisting}
   304   \begin{lstlisting}
   306 In []: imshow(A[:256,:256])
   305 In []: imshow(a[:256,:256])
   307 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c>
   306 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c>
   308 
   307 
   309 In []: imshow(A[200:400,200:400])
   308 In []: imshow(a[200:400,200:400])
   310 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c>
   309 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c>
   311 
   310 
   312 In []: imshow(A[::2,::2])
   311 In []: imshow(a[::2,::2])
   313 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c>
   312 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c>
   314   \end{lstlisting}
   313   \end{lstlisting}
   315 \end{small}
   314 \end{small}
   316 \end{frame}
   315 \end{frame}
   317 
   316 
   318 \begin{frame}[fragile]
   317 \begin{frame}[fragile]
   319 \frametitle{Transpose of a Matrix}
   318 \frametitle{Transpose of a Matrix}
   320 \begin{lstlisting}
   319 \begin{lstlisting}
   321 In []: A.T
   320 In []: a.T
   322 Out[]:
   321 Out[]:
   323 array([[ 1,  2,  2,  1],
   322 array([[ 1,  2,  2,  1],
   324        [ 1,  5,  1, -3],
   323        [ 1,  5,  1, -3],
   325        [ 2, -1, -1,  2],
   324        [ 2, -1, -1,  2],
   326        [-1, -9,  3,  7]])
   325        [-1, -9,  3,  7]])
   328 \end{frame}
   327 \end{frame}
   329 
   328 
   330 \begin{frame}[fragile]
   329 \begin{frame}[fragile]
   331   \frametitle{Sum of all elements}
   330   \frametitle{Sum of all elements}
   332   \begin{lstlisting}
   331   \begin{lstlisting}
   333 In []: sum(A)
   332 In []: sum(a)
   334 Out[]: 12
   333 Out[]: 12
   335   \end{lstlisting}
   334   \end{lstlisting}
   336 \end{frame}
   335 \end{frame}
   337 
   336 
   338 \begin{frame}[fragile]
   337 \begin{frame}[fragile]
   339   \frametitle{Matrix Addition}
   338   \frametitle{Matrix Addition}
   340   \begin{lstlisting}
   339   \begin{lstlisting}
   341 In []: B = array([[3,2,-1,5],
   340 In []: b = array([[3,2,-1,5],
   342                   [2,-2,4,9],
   341                   [2,-2,4,9],
   343                   [-1,0.5,-1,-7],
   342                   [-1,0.5,-1,-7],
   344                   [9,-5,7,3]])
   343                   [9,-5,7,3]])
   345 In []: A + B
   344 In []: a + b
   346 Out[]: 
   345 Out[]: 
   347 array([[  4. ,   3. ,   1. ,   4. ],
   346 array([[  4. ,   3. ,   1. ,   4. ],
   348        [  4. ,   3. ,   3. ,   0. ],
   347        [  4. ,   3. ,   3. ,   0. ],
   349        [  1. ,   1.5,  -2. ,  -4. ],
   348        [  1. ,   1.5,  -2. ,  -4. ],
   350        [ 10. ,  -8. ,   9. ,  10. ]])
   349        [ 10. ,  -8. ,   9. ,  10. ]])
   352 \end{frame}
   351 \end{frame}
   353 
   352 
   354 \begin{frame}[fragile]
   353 \begin{frame}[fragile]
   355 \frametitle{Elementwise Multiplication}
   354 \frametitle{Elementwise Multiplication}
   356 \begin{lstlisting}
   355 \begin{lstlisting}
   357 In []: A*B
   356 In []: a*b
   358 Out[]: 
   357 Out[]: 
   359 array([[  3. ,   2. ,  -2. ,  -5. ],
   358 array([[  3. ,   2. ,  -2. ,  -5. ],
   360        [  4. , -10. ,  -4. , -81. ],
   359        [  4. , -10. ,  -4. , -81. ],
   361        [ -2. ,   0.5,   1. , -21. ],
   360        [ -2. ,   0.5,   1. , -21. ],
   362        [  9. ,  15. ,  14. ,  21. ]])
   361        [  9. ,  15. ,  14. ,  21. ]])
   365 \end{frame}
   364 \end{frame}
   366 
   365 
   367 \begin{frame}[fragile]
   366 \begin{frame}[fragile]
   368 \frametitle{Matrix Multiplication}
   367 \frametitle{Matrix Multiplication}
   369 \begin{lstlisting}
   368 \begin{lstlisting}
   370 In []: dot(A,B)
   369 In []: dot(a, b)
   371 Out[]: 
   370 Out[]: 
   372 array([[ -6. ,   6. ,  -6. ,  -3. ],
   371 array([[ -6. ,   6. ,  -6. ,  -3. ],
   373        [-64. ,  38.5, -44. ,  35. ],
   372        [-64. ,  38.5, -44. ,  35. ],
   374        [ 36. , -13.5,  24. ,  35. ],
   373        [ 36. , -13.5,  24. ,  35. ],
   375        [ 58. , -26. ,  34. , -15. ]])
   374        [ 58. , -26. ,  34. , -15. ]])
   377 \end{frame}
   376 \end{frame}
   378 
   377 
   379 \begin{frame}[fragile]
   378 \begin{frame}[fragile]
   380 \frametitle{Inverse of a Matrix}
   379 \frametitle{Inverse of a Matrix}
   381 \begin{lstlisting}
   380 \begin{lstlisting}
   382 In []: inv(A)
   381 In []: inv(a)
   383 \end{lstlisting}
   382 \end{lstlisting}
   384 \begin{small}
   383 \begin{small}
   385 \begin{lstlisting}
   384 \begin{lstlisting}
   386 Out[]: 
   385 Out[]: 
   387 array([[-0.5 ,  0.55, -0.15,  0.7 ],
   386 array([[-0.5 ,  0.55, -0.15,  0.7 ],
   393 \end{frame}
   392 \end{frame}
   394 
   393 
   395 \begin{frame}[fragile]
   394 \begin{frame}[fragile]
   396 \frametitle{Determinant}
   395 \frametitle{Determinant}
   397 \begin{lstlisting}
   396 \begin{lstlisting}
   398 In []: det(A)
   397 In []: det(a)
   399 Out[]: 80.0
   398 Out[]: 80.0
   400 \end{lstlisting}
   399 \end{lstlisting}
   401 \end{frame}
   400 \end{frame}
   402 
   401 
   403 %%use S=array(X,Y)
   402 %%use S=array(X,Y)
   404 \begin{frame}[fragile]
   403 \begin{frame}[fragile]
   405 \frametitle{Eigenvalues and Eigen Vectors}
   404 \frametitle{Eigenvalues and Eigen Vectors}
   406 \begin{small}
   405 \begin{small}
   407 \begin{lstlisting}
   406 \begin{lstlisting}
   408 In []: E = array([[3,2,4],[2,0,2],[4,2,3]])
   407 In []: e = array([[3,2,4],[2,0,2],[4,2,3]])
   409 
   408 
   410 In []: eig(E)
   409 In []: eig(e)
   411 Out[]: 
   410 Out[]: 
   412 (array([-1.,  8., -1.]),
   411 (array([-1.,  8., -1.]),
   413  array([[-0.74535599,  0.66666667, -0.1931126 ],
   412  array([[-0.74535599,  0.66666667, -0.1931126 ],
   414         [ 0.2981424 ,  0.33333333, -0.78664085],
   413         [ 0.2981424 ,  0.33333333, -0.78664085],
   415         [ 0.59628479,  0.66666667,  0.58643303]]))
   414         [ 0.59628479,  0.66666667,  0.58643303]]))
   416 
   415 
   417 In []: eigvals(E)
   416 In []: eigvals(e)
   418 Out[]: array([-1.,  8., -1.])
   417 Out[]: array([-1.,  8., -1.])
   419 \end{lstlisting}
   418 \end{lstlisting}
   420 \end{small}
   419 \end{small}
   421 \end{frame}
   420 \end{frame}
   422 
   421 
   423 %% \begin{frame}[fragile]
   422 %% \begin{frame}[fragile]
   424 %% \frametitle{Computing Norms}
   423 %% \frametitle{Computing Norms}
   425 %% \begin{lstlisting}
   424 %% \begin{lstlisting}
   426 %% In []: norm(E)
   425 %% In []: norm(e)
   427 %% Out[]: 8.1240384046359608
   426 %% Out[]: 8.1240384046359608
   428 %% \end{lstlisting}
   427 %% \end{lstlisting}
   429 %% \end{frame}
   428 %% \end{frame}
   430 
   429 
   431 %% \begin{frame}[fragile]
   430 %% \begin{frame}[fragile]
   432 %%   \frametitle{Singular Value Decomposition}
   431 %%   \frametitle{Singular Value Decomposition}
   433 %%   \begin{small}
   432 %%   \begin{small}
   434 %%   \begin{lstlisting}
   433 %%   \begin{lstlisting}
   435 %% In []: svd(E)
   434 %% In []: svd(e)
   436 %% Out[]: 
   435 %% Out[]: 
   437 %% (array(
   436 %% (array(
   438 %% [[ -6.66666667e-01,  -1.23702565e-16,   7.45355992e-01],
   437 %% [[ -6.66666667e-01,  -1.23702565e-16,   7.45355992e-01],
   439 %%  [ -3.33333333e-01,  -8.94427191e-01,  -2.98142397e-01],
   438 %%  [ -3.33333333e-01,  -8.94427191e-01,  -2.98142397e-01],
   440 %%  [ -6.66666667e-01,   4.47213595e-01,  -5.96284794e-01]]),
   439 %%  [ -6.66666667e-01,   4.47213595e-01,  -5.96284794e-01]]),