author | Santosh G. Vattam <vattam.santosh@gmail.com> |
Mon, 08 Mar 2010 20:45:33 +0530 | |
changeset 379 | 682b6f66fe11 |
parent 343 | adb85e126341 |
permissions | -rwxr-xr-x |
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
1 |
\documentclass[12pt]{article} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
2 |
\title{Matrices and Least Square Fit} |
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
3 |
\author{FOSSEE} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
4 |
\usepackage{listings} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
5 |
\lstset{language=Python, |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
6 |
basicstyle=\ttfamily, |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
7 |
commentstyle=\itshape\bfseries, |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
8 |
showstringspaces=false, |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
9 |
} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
10 |
\newcommand{\typ}[1]{\lstinline{#1}} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
11 |
\usepackage[english]{babel} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
12 |
\usepackage[latin1]{inputenc} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
13 |
\usepackage{times} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
14 |
\usepackage[T1]{fontenc} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
15 |
\usepackage{ae,aecompl} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
16 |
\usepackage{mathpazo,courier,euler} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
17 |
\usepackage[scaled=.95]{helvet} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
18 |
|
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
19 |
\begin{document} |
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
20 |
\date{} |
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
21 |
\vspace{-1in} |
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
22 |
\begin{center} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
23 |
\LARGE{Matrices and Least Square Fit}\\ |
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
24 |
\large{FOSSEE} |
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
25 |
\end{center} |
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
26 |
\section{Matrices} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
27 |
\subsection{Basics} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
28 |
Matrix Creation\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
29 |
\typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
30 |
It creates C matrix of shape 3x3\\ |
330
46533051b9d3
commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents:
328
diff
changeset
|
31 |
Shape is dimensions of given array. |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
32 |
\begin{lstlisting} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
33 |
In []: C.shape |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
34 |
Out[]: (3, 3) |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
35 |
In []: shape([[1,2],[4,5],[3,0]]) |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
36 |
Out[]: (3, 2) |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
37 |
\end{lstlisting} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
38 |
\typ{In []: B = ones_like(C)} \\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
39 |
B would be array of ones with the same shape and type as C.\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
40 |
\typ{In []: A = ones((3,2))} \\ |
330
46533051b9d3
commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents:
328
diff
changeset
|
41 |
A would be new matrix of given shape(arguments), filled with ones.\\ |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
42 |
\typ{In []: I = identity(3)}\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
43 |
I would be identity matrix of shape 3x3 |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
44 |
|
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
45 |
\subsection{Accessing Elements} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
46 |
\begin{lstlisting} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
47 |
In []: C |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
48 |
Out[]: |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
49 |
array([[ 1, 1, 2], |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
50 |
[ 2, 4, 1], |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
51 |
[-1, 3, 7]]) |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
52 |
In []: C[1,2] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
53 |
Out[]: 1 |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
54 |
\end{lstlisting} |
340
347ff2714deb
Minor edits to correct spellings.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
328
diff
changeset
|
55 |
Two indexes separated by \typ{','} specifies [row, column]. So \typ{C[1,2]} gets third element of second row(indices starts from 0). |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
56 |
\newpage |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
57 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
58 |
In []: C[1] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
59 |
Out[]: array([2, 4, 1]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
60 |
\end{lstlisting} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
61 |
Single index implies complete row. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
62 |
\subsection{Changing elements} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
63 |
\begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
64 |
In []: C[1,1] = -2 |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
65 |
In []: C |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
66 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
67 |
array([[ 1, 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
68 |
[ 2, -2, 1], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
69 |
[-1, 3, 7]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
70 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
71 |
In []: C[1] = [0,0,0] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
72 |
In []: C |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
73 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
74 |
array([[ 1, 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
75 |
[ 0, 0, 0], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
76 |
[-1, 3, 7]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
77 |
\end{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
78 |
|
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
79 |
\subsection{Slicing} |
330
46533051b9d3
commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents:
328
diff
changeset
|
80 |
Accessing rows with Matrices is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
81 |
\begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
82 |
In []: C[:,1] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
83 |
Out[]: array([1, 0, 3]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
84 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
85 |
First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
86 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
87 |
In []: C[1,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
88 |
Out[]: array([0, 0, 0]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
89 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
90 |
Here we get second row(1), all columns(':') of C matrix. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
91 |
\newpage |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
92 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
93 |
In []: C[0:2,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
94 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
95 |
array([[1, 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
96 |
[0, 0, 0]]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
97 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
98 |
Result is sub-matrix with first and second row(endpoint is excluded), and all columns from C. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
99 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
100 |
In []: C[1:3,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
101 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
102 |
array([[ 0, 0, 0], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
103 |
[-1, 3, 7]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
104 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
105 |
In []: C[:2,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
106 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
107 |
array([[1, 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
108 |
[0, 0, 0]]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
109 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
110 |
\typ{':2'} => start from first row, till and excluding third row. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
111 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
112 |
In []: C[1:,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
113 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
114 |
array([[ 0, 0, 0], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
115 |
[-1, 3, 7]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
116 |
|
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
117 |
In []: C[1:,:2] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
118 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
119 |
array([[ 0, 0], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
120 |
[-1, 3]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
121 |
\end{lstlisting} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
122 |
\typ{'1:'} => Start from second row, till last row\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
123 |
\typ{':2'} => Start from first column, till and excluding third column. |
316
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
124 |
\newpage |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
125 |
\subsection{Striding} |
340
347ff2714deb
Minor edits to correct spellings.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
328
diff
changeset
|
126 |
Often apart from sub-matrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\ |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
127 |
Following method will return Matrix with alternate rows. |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
128 |
\begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
129 |
In []: C[::2,:] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
130 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
131 |
array([[ 1, 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
132 |
[-1, 3, 7]]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
133 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
134 |
\typ{C[startR:stopR:stepR,startC:stopC:stepC]} => Syntax of mentioning starting index, ending index, and step to jump.\\ |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
135 |
In above mentioned case, \typ{'::2'} means, start from first row, till last row(both are blank), with step of 2, that is, skipping alternate row. After first row, C[startR], next row would be C[startR+stepR] and so on. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
136 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
137 |
In []: C[:,::2] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
138 |
Out[]: |
343
adb85e126341
Moved all day1 quiz questions to end of day1-sessions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
341
diff
changeset
|
139 |
array([[ 1, 2], |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
140 |
[ 0, 0], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
141 |
[-1, 7]]) |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
142 |
\end{lstlisting} |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
143 |
Same as above, just that here we get matrix with each alternate column and all rows. |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
144 |
\begin{lstlisting} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
145 |
In []: C[::2,::2] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
146 |
Out[]: |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
147 |
array([[ 1, 2], |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
148 |
[-1, 7]]) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
149 |
\end{lstlisting} |
316
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
150 |
\section{Matrix Operations} |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
151 |
For a Matrix A and B of equal shapes. |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
152 |
\begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
153 |
In []: A.T # Transpose |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
154 |
In []: sum(A) # Sum of all elements |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
155 |
In []: A+B # Addition |
315
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
156 |
In []: A*B # Element wise product |
141f3903d4e8
Modified Matrix operation part of cheat sheet 4.
Shantanu <shantanu@fossee.in>
parents:
295
diff
changeset
|
157 |
In []: dot(A,b) #Matrix multiplication |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
158 |
In []: inv(A) # Inverse |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
159 |
In []: det(A) # Determinant |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
160 |
\end{lstlisting} |
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
161 |
Eigen Values and Eigen Vectors |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
162 |
\begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
163 |
In []: eig(A) #Eigen Values and Vectors |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
164 |
In []: eigvals(A) #Eigen Values |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
165 |
\end{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
166 |
%% Norm |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
167 |
%% \begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
168 |
%% In []: norm(A) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
169 |
%% \end{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
170 |
%% Single Value Decomposition |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
171 |
%% \begin{lstlisting} |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
172 |
%% In []: svd(A) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
173 |
%% \end{lstlisting} |
316
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
174 |
\section{Least Square Fit Line} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
175 |
\begin{lstlisting} |
316
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
176 |
L = [] |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
177 |
T = [] |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
178 |
for line in open('pendulum.txt'): |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
179 |
point = line.split() |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
180 |
L.append(float(point[0])) |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
181 |
T.append(float(point[1])) |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
182 |
Tsq = [] |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
183 |
for time in T: |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
184 |
Tsq.append(time*time) |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
185 |
plot(L, Tsq, '.') |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
186 |
\end{lstlisting} |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
187 |
This is exact curve we get from L Vs Tsq from data.This relation among L and Tsq is not of straight line. For getting Least Square Fit line, we have to solve the relations:\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
188 |
$L=m*Tsq+c$ (something similar to $y=m*x+c$)\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
189 |
For present scenario, we have L and corresponding Tsq values. For finding m and c at given points we use \typ{lstlq} function provided by pylab. It returns the least-squares solution to an equation. \\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
190 |
For finding Least Square Fit line for this particular data we have to do following steps:\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
191 |
\typ{In []: A = array([L, ones\_like(L)])}\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
192 |
A is 2x(Length of array L) array. |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
193 |
\begin{lstlisting} |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
194 |
In []: A = A.T #now A.shape = (Length of array L)x2 |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
195 |
In []: result = lstsq(A,TSq) |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
196 |
In []: coef = result[0] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
197 |
In []: Tline = coef[0]*L + coef[1] |
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
198 |
\end{lstlisting} |
316
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
199 |
\typ{coef[0]} is array with all $m$ values, and \typ{coef[1]} contains $c$.\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
200 |
To get the final plot.\\ |
6108f2007151
Modified cheat sheet of session 4 day 1.
Shantanu <shantanu@fossee.in>
parents:
315
diff
changeset
|
201 |
\typ{In []: plot(L, Tline)} |
295
39d7c4e14585
Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
284
diff
changeset
|
202 |
|
284
3c191accbb32
Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
203 |
\end{document} |