22 \begin{center} |
22 \begin{center} |
23 \LARGE{Matrices and Least Square Fit}\\ |
23 \LARGE{Matrices and Least Square Fit}\\ |
24 \large{FOSSEE} |
24 \large{FOSSEE} |
25 \end{center} |
25 \end{center} |
26 \section{Matrices} |
26 \section{Matrices} |
27 Inputting a Matrix |
27 \subsection{Basics} |
28 \begin{lstlisting} |
28 Matrix Creation\\ |
29 In []: C = array([[1,1,2], |
29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
30 [2,4,1], |
30 It creates C matrix of shape 3x3\\ |
31 [-1,3,7]]) |
31 Shape is dimensions of given array. |
32 In []: B = ones_like(C) |
32 \begin{lstlisting} |
33 In []: A = ones((3,2)) |
33 In []: C.shape |
34 In []: I = identity(3) |
34 Out[]: (3, 3) |
35 \end{lstlisting} |
35 In []: shape([[1,2],[4,5],[3,0]]) |
36 Accessing Elements |
36 Out[]: (3, 2) |
37 \begin{lstlisting} |
37 \end{lstlisting} |
|
38 \typ{In []: B = ones_like(C)} \\ |
|
39 B would be array of ones with the same shape and type as C.\\ |
|
40 \typ{In []: A = ones((3,2))} \\ |
|
41 A would be new matrix of given shape(arguments), filled with ones.\\ |
|
42 \typ{In []: I = identity(3)}\\ |
|
43 I would be identity matrix of shape 3x3 |
|
44 |
|
45 \subsection{Accessing Elements} |
|
46 \begin{lstlisting} |
|
47 In []: C |
|
48 Out[]: |
|
49 array([[ 1, 1, 2], |
|
50 [ 2, 4, 1], |
|
51 [-1, 3, 7]]) |
38 In []: C[1,2] |
52 In []: C[1,2] |
39 Out[]: 1 |
53 Out[]: 1 |
40 |
54 \end{lstlisting} |
|
55 Two indexes seperated by \typ{','} specifies [row, column]. So \typ{C[1,2]} gets third element of second row(indices starts from 0). |
|
56 \newpage |
|
57 \begin{lstlisting} |
41 In []: C[1] |
58 In []: C[1] |
42 Out[]: array([2, 4, 1]) |
59 Out[]: array([2, 4, 1]) |
43 \end{lstlisting} |
60 \end{lstlisting} |
44 |
61 Single index implies complete row. |
45 Changing elements |
62 \subsection{Changing elements} |
46 \begin{lstlisting} |
63 \begin{lstlisting} |
47 In []: C[1,1] = -2 |
64 In []: C[1,1] = -2 |
48 In []: C |
65 In []: C |
49 Out[]: |
66 Out[]: |
50 array([[ 1, 1, 2], |
67 array([[ 1, 1, 2], |
57 array([[ 1, 1, 2], |
74 array([[ 1, 1, 2], |
58 [ 0, 0, 0], |
75 [ 0, 0, 0], |
59 [-1, 3, 7]]) |
76 [-1, 3, 7]]) |
60 \end{lstlisting} |
77 \end{lstlisting} |
61 |
78 |
62 Slicing |
79 \subsection{Slicing} |
|
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. |
63 \begin{lstlisting} |
81 \begin{lstlisting} |
64 In []: C[:,1] |
82 In []: C[:,1] |
65 Out[]: array([1, 0, 3]) |
83 Out[]: array([1, 0, 3]) |
66 |
84 \end{lstlisting} |
|
85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |
|
86 \begin{lstlisting} |
67 In []: C[1,:] |
87 In []: C[1,:] |
68 Out[]: array([0, 0, 0]) |
88 Out[]: array([0, 0, 0]) |
69 |
89 \end{lstlisting} |
|
90 Here we get second row(1), all columns(':') of C matrix. |
|
91 \newpage |
|
92 \begin{lstlisting} |
70 In []: C[0:2,:] |
93 In []: C[0:2,:] |
71 Out[]: |
94 Out[]: |
72 array([[1, 1, 2], |
95 array([[1, 1, 2], |
73 [0, 0, 0]]) |
96 [0, 0, 0]]) |
74 |
97 \end{lstlisting} |
|
98 Result is sub-matrix with first and second row(endpoint is excluded), and all columns from C. |
|
99 \begin{lstlisting} |
75 In []: C[1:3,:] |
100 In []: C[1:3,:] |
76 Out[]: |
101 Out[]: |
77 array([[ 0, 0, 0], |
102 array([[ 0, 0, 0], |
78 [-1, 3, 7]]) |
103 [-1, 3, 7]]) |
79 |
104 |
80 In []: C[:2,:] |
105 In []: C[:2,:] |
81 Out[]: |
106 Out[]: |
82 array([[1, 1, 2], |
107 array([[1, 1, 2], |
83 [0, 0, 0]]) |
108 [0, 0, 0]]) |
84 |
109 \end{lstlisting} |
|
110 \typ{':2'} => start from first row, till and excluding third row. |
|
111 \begin{lstlisting} |
85 In []: C[1:,:] |
112 In []: C[1:,:] |
86 Out[]: |
113 Out[]: |
87 array([[ 0, 0, 0], |
114 array([[ 0, 0, 0], |
88 [-1, 3, 7]]) |
115 [-1, 3, 7]]) |
89 |
116 |
90 In []: C[1:,:2] |
117 In []: C[1:,:2] |
91 Out[]: |
118 Out[]: |
92 array([[ 0, 0], |
119 array([[ 0, 0], |
93 [-1, 3]]) |
120 [-1, 3]]) |
94 \end{lstlisting} |
121 \end{lstlisting} |
95 |
122 \typ{'1:'} => Start from second row, till last row\\ |
96 Striding |
123 \typ{':2'} => Start from first column, till and excluding third column. |
|
124 \newpage |
|
125 \subsection{Striding} |
|
126 Often apart from submatrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\ |
|
127 Following method will return Matrix with alternate rows. |
97 \begin{lstlisting} |
128 \begin{lstlisting} |
98 In []: C[::2,:] |
129 In []: C[::2,:] |
99 Out[]: |
130 Out[]: |
100 array([[ 1, 1, 2], |
131 array([[ 1, 1, 2], |
101 [-1, 3, 7]]) |
132 [-1, 3, 7]]) |
102 |
133 \end{lstlisting} |
|
134 \typ{C[startR:stopR:stepR,startC:stopC:stepC]} => Syntax of mentioning starting index, ending index, and step to jump.\\ |
|
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. |
|
136 \begin{lstlisting} |
103 In []: C[:,::2] |
137 In []: C[:,::2] |
104 Out[]: |
138 Out[]: |
105 xarray([[ 1, 2], |
139 xarray([[ 1, 2], |
106 [ 0, 0], |
140 [ 0, 0], |
107 [-1, 7]]) |
141 [-1, 7]]) |
108 |
142 \end{lstlisting} |
|
143 Same as above, just that here we get matrix with each alternate column and all rows. |
|
144 \begin{lstlisting} |
109 In []: C[::2,::2] |
145 In []: C[::2,::2] |
110 Out[]: |
146 Out[]: |
111 array([[ 1, 2], |
147 array([[ 1, 2], |
112 [-1, 7]]) |
148 [-1, 7]]) |
113 \end{lstlisting} |
149 \end{lstlisting} |
114 |
150 \section{Matrix Operations} |
115 Matrix Operations |
151 For a Matrix A and B of equal shapes. |
116 \begin{lstlisting} |
152 \begin{lstlisting} |
117 In []: A.T # Transpose |
153 In []: A.T # Transpose |
118 In []: sum(A) # Sum of all elements |
154 In []: sum(A) # Sum of all elements |
119 In []: A+B # Addition |
155 In []: A+B # Addition |
120 In []: A*B # Product |
156 In []: A*B # Element wise product |
|
157 In []: dot(A,b) #Matrix multiplication |
121 In []: inv(A) # Inverse |
158 In []: inv(A) # Inverse |
122 In []: det(A) # Determinant |
159 In []: det(A) # Determinant |
123 \end{lstlisting} |
160 \end{lstlisting} |
124 |
|
125 Eigen Values and Eigen Vectors |
161 Eigen Values and Eigen Vectors |
126 \begin{lstlisting} |
162 \begin{lstlisting} |
127 In []: eig(A) #Eigen Values and Vectors |
163 In []: eig(A) #Eigen Values and Vectors |
128 In []: eigvals(A) #Eigen Values |
164 In []: eigvals(A) #Eigen Values |
129 \end{lstlisting} |
165 \end{lstlisting} |
133 %% \end{lstlisting} |
169 %% \end{lstlisting} |
134 %% Single Value Decomposition |
170 %% Single Value Decomposition |
135 %% \begin{lstlisting} |
171 %% \begin{lstlisting} |
136 %% In []: svd(A) |
172 %% In []: svd(A) |
137 %% \end{lstlisting} |
173 %% \end{lstlisting} |
138 Least Square Fit Line |
174 \section{Least Square Fit Line} |
139 \begin{lstlisting} |
175 \begin{lstlisting} |
140 In []: A = array([L, ones_like(L)]) |
176 L = [] |
141 In []: A = A.T |
177 T = [] |
|
178 for line in open('pendulum.txt'): |
|
179 point = line.split() |
|
180 L.append(float(point[0])) |
|
181 T.append(float(point[1])) |
|
182 Tsq = [] |
|
183 for time in T: |
|
184 Tsq.append(time*time) |
|
185 plot(L, Tsq, '.') |
|
186 \end{lstlisting} |
|
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:\\ |
|
188 $L=m*Tsq+c$ (something similar to $y=m*x+c$)\\ |
|
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. \\ |
|
190 For finding Least Square Fit line for this particular data we have to do following steps:\\ |
|
191 \typ{In []: A = array([L, ones\_like(L)])}\\ |
|
192 A is 2x(Length of array L) array. |
|
193 \begin{lstlisting} |
|
194 In []: A = A.T #now A.shape = (Length of array L)x2 |
142 In []: result = lstsq(A,TSq) |
195 In []: result = lstsq(A,TSq) |
143 In []: coef = result[0] |
196 In []: coef = result[0] |
144 In []: Tline = coef[0]*L + coef[1] |
197 In []: Tline = coef[0]*L + coef[1] |
145 In []: plot(L, Tline) |
198 \end{lstlisting} |
146 \end{lstlisting} |
199 \typ{coef[0]} is array with all $m$ values, and \typ{coef[1]} contains $c$.\\ |
|
200 To get the final plot.\\ |
|
201 \typ{In []: plot(L, Tline)} |
147 |
202 |
148 \end{document} |
203 \end{document} |