author | asokan <asokan@fossee.in> |
Tue, 18 May 2010 15:40:17 +0530 | |
changeset 126 | 2eac725a5766 |
parent 125 | 27ddf1255daa |
permissions | -rw-r--r-- |
67 | 1 |
Hello friends and welcome to this tutorial on Matrices. |
125 | 2 |
|
67 | 3 |
In python all matrix operations are done using arrays. |
125 | 4 |
|
5 |
We saw in the previous session that arrays are better suited for mathematical operations. We saw this in the context of simple statistical functions such as mean. |
|
6 |
||
7 |
In this session we shall see how to perform efficient matrix operations using arrays. We will create arrays, initialize them, manipulate them and perform simple image processing using them. For this tutorial we shall need the lena.png image. Hope you have the image with you. |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
8 |
|
72 | 9 |
Let's now start off. As you can see our lena image is on the desktop, so first let's navigate to the desktop by cd Desktop. |
10 |
||
125 | 11 |
Let's now start Ipython, using the command python -pylab |
72 | 12 |
|
125 | 13 |
First things first, let's start by creating a normal array. |
14 |
||
15 |
Type: |
|
16 |
p equal to array([5, 8, 10, 13]) |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
17 |
|
67 | 18 |
let's check the value of a by typing |
125 | 19 |
p |
20 |
Note how python displays an array, compared to a list. |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
21 |
|
125 | 22 |
Here p is single dimension array, that is it has only one row. Let us now create a multi-dimensional array. |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
23 |
|
125 | 24 |
Type: |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
25 |
|
125 | 26 |
q = array([[11,12,13], [21,22,23], [31,32,33]]) |
27 |
||
28 |
both p and q are arrays but with different dimensions or shape |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
29 |
we can check shape of arrays by using shape attribute of arrays. |
125 | 30 |
p.shape |
31 |
q.shape |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
32 |
|
125 | 33 |
A few array initialization methods are also available to make life easier; |
67 | 34 |
say we want to create an array of size 3x4 with all the elements initialized to 1, we use |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
35 |
b = ones((3, 4)) |
69 | 36 |
and b is |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
37 |
b |
125 | 38 |
similarly, suppose we already have an array, and we want to create another array with the same shape but with initial values equal to one, for eg, to get an array similar in shape to the array 'q' but with all elements as 1 we type: |
39 |
d = ones_like(q) |
|
69 | 40 |
and d is a 3x3 array with all values equal to 1 |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
41 |
|
125 | 42 |
Similarly there are functions like zeros and zeros_like which initialize array with all values being 0. One more useful function available is 'identity', it create unit matrix of given order |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
43 |
i = identity(3) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
44 |
i |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
45 |
|
69 | 46 |
Note that identity takes just one argument since identity matrix is always a square matrix. |
47 |
||
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
48 |
---------------- |
69 | 49 |
Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. |
50 |
Remember we created a 3x3 matrix earlier, |
|
125 | 51 |
q |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
52 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
53 |
to access the element 23 we type |
125 | 54 |
q[1][2] |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
55 |
|
125 | 56 |
It is at the second row of the third column of the matrix/array q. Note that index values of arrays also start from 0. |
69 | 57 |
Alternatively, the more popular way of doing the same is |
125 | 58 |
q[1, 2] |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
59 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
60 |
here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
61 |
|
125 | 62 |
to access particular row completely we specify the row value alone: |
63 |
q[1] |
|
64 |
This gives us the entire second row. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
65 |
|
69 | 66 |
We can assign a new value to an element, the same way we accessed it. For eg., |
125 | 67 |
q[1, 1] = -22 |
68 |
q |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
69 |
|
125 | 70 |
One of the most powerful aspects of a high-level language like python is the way it supports matrix operations. We can use them like we do it maths rather than think of them as elements like a programmer. |
71 |
||
72 |
For example to change a whole row, we type: |
|
73 |
q[1] = 0 |
|
74 |
q |
|
69 | 75 |
as we can see, all elements of the second row are now 0 |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
76 |
|
125 | 77 |
In order to access a column, we need to syntactically indicate that the number given is the column index rather than the row index. In other words we need a placeholder for the row position. We cannot use space. That is we cannot say q[, 1] as that would be a syntax error ( q[m, n] being the method to access an _element_). |
78 |
||
79 |
We have to say all rows and a column(s) by extending the slice notation seen earlier. |
|
80 |
q[:,2] |
|
69 | 81 |
returns the third column. |
125 | 82 |
Here the ':' part specifies the row numbers of the slice; as we have seen before by leaving the from and to parts of the slice empty we effectively say ALL. Thus q[:, n] stands for a matrix which is the submatrix obtained by taking all rows and column n+1. |
83 |
||
84 |
The row reference q[1] can also be written as q[1,:] |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
85 |
|
125 | 86 |
As we have seen ':' takes two values namely start and end. As before rows or columns starting from 'start' till 'end' --excluding end-- will be returned. Lets try some examples: |
87 |
q [0:2,:] |
|
88 |
results in a matrix which is rows 0 and 1 and all columns. Note here that 'end', in this case, '2' will not be included in resulting matrix. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
89 |
|
125 | 90 |
Similarly q[1:3,:] |
91 |
gives second and third rows. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
92 |
|
125 | 93 |
q[:, 0:2] gives us first two columns |
94 |
||
95 |
This manner of accessing chunks of arrays is also known as 'slicing'. Since the idea is the same as slicing for lists the name is also the same. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
96 |
|
125 | 97 |
As noted the default values for slices carry over. |
98 |
Thus |
|
99 |
q[:, :2] |
|
100 |
gives us the first two columns |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
101 |
|
125 | 102 |
q[:, 1:] |
103 |
returns all columns excluding the 0th column. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
104 |
|
125 | 105 |
q[1:, :2] |
69 | 106 |
returns first two columns of all rows excepting the 0th row. |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
107 |
|
125 | 108 |
When slicing lists we saw the idea of striding. Recall that if L is a list, |
109 |
L[start : stop : step], |
|
110 |
produces a new list whose first element is L[start] and has all elements whose index is start + n * step; stop signals the last index _before_ which we should stop. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
111 |
|
125 | 112 |
Matrices also support striding--that is skip, rows or columns by a certain interval. |
113 |
We add one more ':' to row or column part to specify a step size. |
|
114 |
Let us type |
|
115 |
q[:, ::2] |
|
116 |
and see what is shown. |
|
117 |
The first colon specifies that we pick all rows, the comma signals that we start specifying columns. The empty places indicate defaults. That is start from the 0th and go to the end. The presence of step--in this case 2--tells us that we will select alternate columns. Thus we q[:, ::2] extracts all rows and alternate columns starting with 0th column. |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
118 |
|
125 | 119 |
q[::2,:] |
120 |
returns a 2x3 matrix with the first and the third row. |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
121 |
|
125 | 122 |
q[::2, ::2] |
123 |
gives us a 2x2 array with the first and the third rows and first and third columns. |
|
124 |
||
125 |
Lets us use slicing and striding for doing some basic image manipulation |
|
126 |
||
127 |
pylab has a function named imread to read images. We shall use lena.png image for our experimentation. Its there on desktop. |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
128 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
129 |
a = imread('lena.png') |
125 | 130 |
Now a is an array with the RGB and Alpha channel values of each pixel |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
131 |
a.shape |
125 | 132 |
tells us that |
133 |
it is an 512x512x4 array. |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
134 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
135 |
to view the image write |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
136 |
imshow(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
137 |
|
125 | 138 |
lets try to crop the image to top left quarter. Since a is an array we can use slicing to get the top left quarter by |
139 |
imshow(a[:256,:256]) (half of 512 is 256) |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
140 |
|
125 | 141 |
Let's crop the image so that only her face is visible. And to do that we'll need some rough estimates of the coordinates of the face. |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
142 |
imshow(a) |
125 | 143 |
now move your mouse pointer over the image, it gives us x, y coordinates of the mouse pointer's current location. With this we can get rough estimate of lena's face. We observe that Lena's face begins from somewhere around 200, 200 and ends at 400, 400. Now cropping to these boundaries is simple |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
144 |
imshow(a[200:400, 200:400]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
145 |
|
69 | 146 |
Next we shall try striding on this image. We shall resize the image by skipping alternate pixels. We have already seen how to skip alternate elements so, |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
147 |
imshow(a[::2, ::2]) |
125 | 148 |
note that the size of image is just 256x256. |
149 |
------------------ |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
150 |
|
125 | 151 |
Till now we have covered initializing and accessing elements of arrays. Now we shall look at other manipulations for arrays. We start by creating a 4x4 array |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
152 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
153 |
a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
154 |
a |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
155 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
156 |
To get transpose of this matrix write |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
157 |
a.T |
69 | 158 |
|
159 |
The sum() function returns sum of all the elements of a matrix. |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
160 |
sum(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
161 |
|
69 | 162 |
let's create one more array for checking more operations |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
163 |
b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
164 |
|
69 | 165 |
+ takes care of matrix additions |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
166 |
a + b |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
167 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
168 |
lets try multiplication now, |
125 | 169 |
a * b returns a new matrix whose elements are products of corresponding elements. THIS IS NOT matrix multiplication. |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
170 |
|
125 | 171 |
To get the usual product of matrices a and b we use |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
172 |
dot(a, b) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
173 |
|
69 | 174 |
To get the inverse of a matrix we use, |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
175 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
176 |
inv(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
177 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
178 |
det(a) returns determinant of matrix a |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
179 |
|
125 | 180 |
we shall create an matrix e |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
181 |
e = array([[3,2,4],[2,0,2],[4,2,3]]) |
125 | 182 |
and then to evaluate eigenvalues of the same. |
69 | 183 |
eig(e) |
125 | 184 |
returns both eigen values and eigen vector of given matrix |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
185 |
to get only eigen values use |
69 | 186 |
eigvals(e) |
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
187 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
188 |
This brings us to end of this session. We have covered Matrices |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
189 |
Initialization |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
190 |
Slicing |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
191 |
Striding |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
192 |
A bit of image processing |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
193 |
Functions available for arrays |
61 | 194 |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
195 |
Thank you |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
196 |