day1/session4.tex
changeset 379 682b6f66fe11
parent 378 2299700a8b97
child 382 41c34770d63a
equal deleted inserted replaced
378:2299700a8b97 379:682b6f66fe11
   162 In []: identity(2)
   162 In []: identity(2)
   163 Out[]: 
   163 Out[]: 
   164 array([[ 1.,  0.],
   164 array([[ 1.,  0.],
   165        [ 0.,  1.]])
   165        [ 0.,  1.]])
   166   \end{lstlisting}
   166   \end{lstlisting}
   167 Also available \alert{\typ{zeros, zeros_like, empty, empty_like}}
   167 Also available \alert{\typ{zeros, zeros_like}}
   168 \end{small}
   168 \end{small}
   169 \end{frame}
   169 \end{frame}
   170 
   170 
   171 
   171 
   172 \begin{frame}[fragile]
   172 \begin{frame}[fragile]
   180 
   180 
   181 In []: c[1][2]
   181 In []: c[1][2]
   182 Out[]: 23
   182 Out[]: 23
   183 In []: c[1,2]
   183 In []: c[1,2]
   184 Out[]: 23
   184 Out[]: 23
       
   185   \end{lstlisting}
       
   186 \end{frame}
       
   187 
       
   188 \begin{frame}[fragile]
       
   189   \frametitle{Changing elements}
       
   190   \begin{small}
       
   191   \begin{lstlisting}
   185 In []: c[1]
   192 In []: c[1]
   186 Out[]: array([21, 22, 23])
   193 Out[]: array([21, 22, 23])
   187   \end{lstlisting}
   194 
   188 \end{frame}
       
   189 
       
   190 \begin{frame}[fragile]
       
   191   \frametitle{Changing elements}
       
   192   \begin{small}
       
   193   \begin{lstlisting}
       
   194 In []: c[1,1] = -22
   195 In []: c[1,1] = -22
   195 In []: c
   196 In []: c
   196 Out[]: 
   197 Out[]: 
   197 array([[ 11,  12,  13],
   198 array([[ 11,  12,  13],
   198        [ 21, -22,  23],
   199        [ 21, -22,  23],
   204 array([[11, 12, 13],
   205 array([[11, 12, 13],
   205        [ 0,  0,  0],
   206        [ 0,  0,  0],
   206        [31, 32, 33]])
   207        [31, 32, 33]])
   207   \end{lstlisting}
   208   \end{lstlisting}
   208   \end{small}
   209   \end{small}
   209 How to change one \alert{column}?
   210 How to access one \alert{column}?
   210 \end{frame}
   211 \end{frame}
   211 
   212 
   212 \begin{frame}[fragile]
   213 \begin{frame}[fragile]
   213   \frametitle{Slicing}
   214   \frametitle{Slicing}
   214 \begin{small}
   215 \begin{small}
   292   \end{lstlisting}
   293   \end{lstlisting}
   293 \emphbar{Shape specifies shape or dimensions of a matrix}
   294 \emphbar{Shape specifies shape or dimensions of a matrix}
   294 \end{frame}
   295 \end{frame}
   295 
   296 
   296 \begin{frame}[fragile]
   297 \begin{frame}[fragile]
   297   \frametitle{Slicing \& Striding Exercises}
   298   \frametitle{Elementary image processing}
   298 \begin{small}
   299 \begin{small}
   299   \begin{lstlisting}
   300   \begin{lstlisting}
   300 In []: a = imread('lena.png')
   301 In []: a = imread('lena.png')
   301 
   302 
   302 In []: imshow(a)
   303 In []: imshow(a)
   303 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc>
   304 Out[]: <matplotlib.image.AxesImage object at 0xa0384cc>
   304 
   305   \end{lstlisting}
   305   \end{lstlisting}
   306   \end{small}
   306 \end{small}
   307 \typ{imread} returns an array of shape (512, 512, 4) which represents an image of 512x512 pixels and 4 shades.\\
       
   308 \typ{imshow} renders the array as an image.
       
   309 \end{frame}
       
   310 
       
   311 \begin{frame}[fragile]
       
   312 \frametitle{Slicing \& Striding Exercises}
   307   \begin{itemize}
   313   \begin{itemize}
   308   \item Crop the image to get the top-left quarter
   314   \item Crop the image to get the top-left quarter
   309   \item Crop the image to get only the face
   315   \item Crop the image to get only the face
   310   \item Resize image to half by dropping alternate pixels
   316   \item Resize image to half by dropping alternate pixels
   311   \end{itemize}
   317   \end{itemize}
   312 \end{frame}
   318 
   313 
   319 \end{frame}
   314 \begin{frame}[fragile]
   320 \begin{frame}[fragile]
   315   \frametitle{Solutions}
   321   \frametitle{Solutions}
   316 \begin{small}
   322 \begin{small}
   317   \begin{lstlisting}
   323   \begin{lstlisting}
   318 In []: imshow(a[:256,:256])
   324 In []: imshow(a[:256,:256])
   343        [-1, -9,  3,  7]])
   349        [-1, -9,  3,  7]])
   344 \end{lstlisting}
   350 \end{lstlisting}
   345 \end{frame}
   351 \end{frame}
   346 
   352 
   347 \begin{frame}[fragile]
   353 \begin{frame}[fragile]
   348   \frametitle{Sum of all elements}
       
   349   \begin{lstlisting}
       
   350 In []: sum(a)
       
   351 Out[]: 12
       
   352   \end{lstlisting}
       
   353 \end{frame}
       
   354 
       
   355 \begin{frame}[fragile]
       
   356   \frametitle{Matrix Addition}
   354   \frametitle{Matrix Addition}
   357   \begin{lstlisting}
   355   \begin{lstlisting}
   358 In []: b = array([[3,2,-1,5],
   356 In []: b = array([[3,2,-1,5],
   359                   [2,-2,4,9],
   357                   [2,-2,4,9],
   360                   [-1,0.5,-1,-7],
   358                   [-1,0.5,-1,-7],
   408 \end{lstlisting}
   406 \end{lstlisting}
   409 \end{small}
   407 \end{small}
   410 \end{frame}
   408 \end{frame}
   411 
   409 
   412 \begin{frame}[fragile]
   410 \begin{frame}[fragile]
   413 \frametitle{Determinant}
   411 \frametitle{Determinant and sum of all elements}
   414 \begin{lstlisting}
   412 \begin{lstlisting}
   415 In []: det(a)
   413 In []: det(a)
   416 Out[]: 80.0
   414 Out[]: 80.0
   417 \end{lstlisting}
   415 \end{lstlisting}
       
   416   \begin{lstlisting}
       
   417 In []: sum(a)
       
   418 Out[]: 12
       
   419   \end{lstlisting}
       
   420 
   418 \end{frame}
   421 \end{frame}
   419 
   422 
   420 %%use S=array(X,Y)
   423 %%use S=array(X,Y)
   421 \begin{frame}[fragile]
   424 \begin{frame}[fragile]
   422 \frametitle{Eigenvalues and Eigen Vectors}
   425 \frametitle{Eigenvalues and Eigen Vectors}
   465 %% \end{frame}
   468 %% \end{frame}
   466 
   469 
   467 \section{Least Squares Fit}
   470 \section{Least Squares Fit}
   468 \begin{frame}[fragile]
   471 \begin{frame}[fragile]
   469 \frametitle{$L$ vs. $T^2$ - Scatter}
   472 \frametitle{$L$ vs. $T^2$ - Scatter}
   470 \vspace{-0.15in}
   473 Linear trend visible.
       
   474 \vspace{-0.1in}
   471 \begin{figure}
   475 \begin{figure}
   472 \includegraphics[width=4in]{data/L-Tsq-points}
   476 \includegraphics[width=4in]{data/L-Tsq-points}
   473 \end{figure}
   477 \end{figure}
   474 \end{frame}
   478 \end{frame}
   475 
   479 
   476 \begin{frame}[fragile]
   480 \begin{frame}[fragile]
   477 \frametitle{$L$ vs. $T^2$ - Line}
   481 \frametitle{$L$ vs. $T^2$ - Line}
   478 \vspace{-0.15in}
   482 This line does not make any mathematical sense.
       
   483 \vspace{-0.1in}
   479 \begin{figure}
   484 \begin{figure}
   480 \includegraphics[width=4in]{data/L-Tsq-Line}
   485 \includegraphics[width=4in]{data/L-Tsq-Line}
   481 \end{figure}
   486 \end{figure}
   482 \end{frame}
   487 \end{frame}
   483 
   488 
   484 \begin{frame}[fragile]
   489 \begin{frame}[fragile]
   485 \frametitle{$L$ vs. $T^2$ }
       
   486 \frametitle{$L$ vs. $T^2$ - Least Square Fit}
   490 \frametitle{$L$ vs. $T^2$ - Least Square Fit}
   487 \vspace{-0.15in}
   491 This is what our intention is.
       
   492 \vspace{-0.1in}
   488 \begin{figure}
   493 \begin{figure}
   489 \includegraphics[width=4in]{data/least-sq-fit}
   494 \includegraphics[width=4in]{data/least-sq-fit}
   490 \end{figure}
   495 \end{figure}
   491 \end{frame}
   496 \end{frame}
   492 
   497 
   493 \begin{frame}
   498 \begin{frame}[fragile]
   494 \frametitle{Least Square Fit Curve}
   499 \frametitle{Matrix Formulation}
   495 \begin{center}
       
   496 \begin{itemize}
       
   497 \item $L \alpha T^2$
       
   498 \item Best Fit Curve $\rightarrow$ Linear
       
   499   \begin{itemize}
       
   500   \item Least Square Fit
       
   501   \end{itemize}
       
   502 \item \typ{lstsq()} 
       
   503 \end{itemize}
       
   504 \end{center}
       
   505 \end{frame}
       
   506 
       
   507 \begin{frame}[fragile]
       
   508 \frametitle{\typ{lstsq}}
       
   509 \begin{itemize}
   500 \begin{itemize}
   510 \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$
   501 \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$
   511 \item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, where $T_{sq}$ is
   502 \item In matrix form, the equation can be represented as $T_{sq} = A \cdot p$, where $T_{sq}$ is
   512   $\begin{bmatrix}
   503   $\begin{bmatrix}
   513   T^2_1 \\
   504   T^2_1 \\
   533 
   524 
   534 \begin{frame}[fragile]
   525 \begin{frame}[fragile]
   535 \frametitle{Getting $L$ and $T^2$}
   526 \frametitle{Getting $L$ and $T^2$}
   536 %If you \alert{closed} IPython after session 2
   527 %If you \alert{closed} IPython after session 2
   537 \begin{lstlisting}
   528 \begin{lstlisting}
   538 In []: l = []
   529 In []: L = []
   539 In []: t = []
   530 In []: t = []
   540 In []: for line in open('pendulum.txt'):
   531 In []: for line in open('pendulum.txt'):
   541   ....     point = line.split()
   532   ....     point = line.split()
   542   ....     l.append(float(point[0]))
   533   ....     L.append(float(point[0]))
   543   ....     t.append(float(point[1]))
   534   ....     t.append(float(point[1]))
   544   ....
   535   ....
   545   ....
   536   ....
   546 \end{lstlisting}
   537 \end{lstlisting}
   547 \end{frame}
   538 \end{frame}
   548 
   539 
   549 \begin{frame}[fragile]
   540 \begin{frame}[fragile]
   550 \frametitle{Getting $L$ and $T^2$ \dots}
   541 \frametitle{Getting $L$ and $T^2$ \dots}
   551 \begin{lstlisting}
   542 \begin{lstlisting}
   552 In []: l = array(l)
   543 In []: L = array(L)
   553 In []: t = array(t)
   544 In []: t = array(t)
   554 \end{lstlisting}
   545 \end{lstlisting}
   555 \alert{\typ{In []: tsq = t*t}}
   546 \alert{\typ{In []: tsq = t*t}}
   556 \end{frame}
   547 \end{frame}
   557  
   548  
   558 \begin{frame}[fragile]
   549 \begin{frame}[fragile]
   559 \frametitle{Generating $A$}
   550 \frametitle{Generating $A$}
   560 \begin{lstlisting}
   551 \begin{lstlisting}
   561 In []: A = array([l, ones_like(l)])
   552 In []: A = array([L, ones_like(L)])
   562 In []: A = A.T
   553 In []: A = A.T
   563 \end{lstlisting}
   554 \end{lstlisting}
   564 %% \begin{itemize}
   555 %% \begin{itemize}
   565 %% \item A is also called a Van der Monde matrix
   556 %% \item A is also called a Van der Monde matrix
   566 %% \item It can also be generated using \typ{vander}
   557 %% \item It can also be generated using \typ{vander}
   584 
   575 
   585 \begin{frame}[fragile]
   576 \begin{frame}[fragile]
   586 \frametitle{Least Square Fit Line \ldots}
   577 \frametitle{Least Square Fit Line \ldots}
   587 We get the points of the line from \typ{coef}
   578 We get the points of the line from \typ{coef}
   588 \begin{lstlisting}
   579 \begin{lstlisting}
   589 In []: Tline = coef[0]*l + coef[1]
   580 In []: Tline = coef[0]*L + coef[1]
       
   581 
       
   582 In []: Tline.shape
   590 \end{lstlisting}
   583 \end{lstlisting}
   591 \begin{itemize}
   584 \begin{itemize}
   592 \item Now plot \typ{Tline} vs. \typ{l}, to get the Least squares fit line. 
   585 \item Now plot \typ{Tline} vs. \typ{L}, to get the Least squares fit line. 
   593 \end{itemize}
   586 \end{itemize}
   594 \begin{lstlisting}
   587 \begin{lstlisting}
   595 In []: plot(l, Tline)
   588 In []: plot(L, Tline)
   596 \end{lstlisting}
   589 \end{lstlisting}
   597 \end{frame}
   590 \end{frame}
   598 
   591 
   599 \begin{frame}[fragile]
   592 \begin{frame}[fragile]
   600 \frametitle{Least Squares Fit}
   593 \frametitle{Least Squares Fit}