1 Hello friends and welcome to this tutorial on Matrices. |
1 Hello friends and welcome to this tutorial on Matrices. |
|
2 |
2 In python all matrix operations are done using arrays. |
3 In python all matrix operations are done using arrays. |
3 We have already seen in previous session that how arrays are better suited for certain mathematical operations. In this session we shall see how to perform efficient matrix operations using arrays. We shall see how to create them, how to initialize them, how to manipulate and use them to perform some basic image processing. For this tutorial we shall need the lena.png image. Hope you have the image with you. |
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. |
4 |
8 |
5 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. |
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. |
6 |
10 |
7 Let's now fire up Ipython, ipython -pylab |
11 Let's now start Ipython, using the command python -pylab |
8 |
12 |
9 First things first, let's start by creating a normal array, type: |
13 First things first, let's start by creating a normal array. |
10 a equal to array([5, 8, 10, 13]) |
14 |
|
15 Type: |
|
16 p equal to array([5, 8, 10, 13]) |
11 |
17 |
12 let's check the value of a by typing |
18 let's check the value of a by typing |
13 a |
19 p |
|
20 Note how python displays an array, compared to a list. |
14 |
21 |
15 Here a is single dimension array, that is it has only one row. Let's now look at creating multi-dimensional arrays by |
22 Here p is single dimension array, that is it has only one row. Let us now create a multi-dimensional array. |
16 |
23 |
17 c = array([[11,12,13], [21,22,23], [31,32,33]]) |
24 Type: |
18 |
25 |
19 both c and a are arrays but with different dimensions or shape |
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 |
20 we can check shape of arrays by using shape attribute of arrays. |
29 we can check shape of arrays by using shape attribute of arrays. |
21 a.shape |
30 p.shape |
22 c.shape |
31 q.shape |
23 |
32 |
24 A few other handy array initialization methods are also available to make life easier. |
33 A few array initialization methods are also available to make life easier; |
25 say we want to create an array of size 3x4 with all the elements initialized to 1, we use |
34 say we want to create an array of size 3x4 with all the elements initialized to 1, we use |
26 b = ones((3, 4)) |
35 b = ones((3, 4)) |
27 and b is |
36 and b is |
28 b |
37 b |
29 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 'c' but with all elements as 1 we type: |
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: |
30 d = ones_like(c) |
39 d = ones_like(q) |
31 and d is a 3x3 array with all values equal to 1 |
40 and d is a 3x3 array with all values equal to 1 |
32 |
41 |
33 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 identity matrix of given order |
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 |
34 i = identity(3) |
43 i = identity(3) |
35 i |
44 i |
36 |
45 |
37 Note that identity takes just one argument since identity matrix is always a square matrix. |
46 Note that identity takes just one argument since identity matrix is always a square matrix. |
38 |
47 |
39 ---------------- |
48 ---------------- |
40 Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. |
49 Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. |
41 Remember we created a 3x3 matrix earlier, |
50 Remember we created a 3x3 matrix earlier, |
42 c |
51 q |
43 |
52 |
44 to access the element 23 we type |
53 to access the element 23 we type |
45 c[1][2] |
54 q[1][2] |
46 |
55 |
47 It is at the second row of the third column of the matrix/array c. Note that index values of arrays also start from 0. |
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. |
48 Alternatively, the more popular way of doing the same is |
57 Alternatively, the more popular way of doing the same is |
49 c[1, 2] |
58 q[1, 2] |
50 |
59 |
51 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
60 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
52 |
61 |
53 to access particular row completely we simply skip the column value |
62 to access particular row completely we specify the row value alone: |
54 c[1] |
63 q[1] |
55 gives us the entire second row. |
64 This gives us the entire second row. |
56 |
65 |
57 We can assign a new value to an element, the same way we accessed it. For eg., |
66 We can assign a new value to an element, the same way we accessed it. For eg., |
58 c[1, 1] = -22 |
67 q[1, 1] = -22 |
59 c |
68 q |
60 |
69 |
61 In order to change an entire row we type: |
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. |
62 c[1] = 0 |
71 |
63 c |
72 For example to change a whole row, we type: |
|
73 q[1] = 0 |
|
74 q |
64 as we can see, all elements of the second row are now 0 |
75 as we can see, all elements of the second row are now 0 |
65 |
76 |
66 Accessing a row is straight forward we skip column part |
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_). |
67 but the same cannot be done to access columns. In order to access the whole column we have to use ':' |
78 |
68 c[:,2] |
79 We have to say all rows and a column(s) by extending the slice notation seen earlier. |
|
80 q[:,2] |
69 returns the third column. |
81 returns the third column. |
70 here the ':' part mentioned for row value symbolises entire row. |
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. |
71 the c[1] we were using earlier can also be written as c[1,:] |
|
72 |
83 |
73 ':' actually takes two value. for any row or column we can mention |
84 The row reference q[1] can also be written as q[1,:] |
74 start:end values, and rows or columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding |
|
75 c[0:2,:] |
|
76 results in rows starting from row zero(0) upto the second row and all columns. Note here that 'end', in this case, '2' will not be included in resulting array. |
|
77 |
85 |
78 c[1:3,:] |
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: |
79 gives second and third row. |
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. |
80 |
89 |
81 similarly we can try this on columns also: |
90 Similarly q[1:3,:] |
82 c[:, 0:2] gives us first two column |
91 gives second and third rows. |
83 This whole concept of accessing chunks of arrays is known as 'slicing' |
|
84 |
92 |
85 There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column. |
93 q[:, 0:2] gives us first two columns |
86 It actually means if we don't specify start and end part of slice default is from zero to end. |
|
87 |
94 |
88 so |
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. |
89 c[:, :2] |
|
90 also gives us first two columns |
|
91 and c[:, 1:] returns all columns excluding the 0th column. |
|
92 |
96 |
93 c[1:, :2] |
97 As noted the default values for slices carry over. |
|
98 Thus |
|
99 q[:, :2] |
|
100 gives us the first two columns |
|
101 |
|
102 q[:, 1:] |
|
103 returns all columns excluding the 0th column. |
|
104 |
|
105 q[1:, :2] |
94 returns first two columns of all rows excepting the 0th row. |
106 returns first two columns of all rows excepting the 0th row. |
95 |
107 |
96 Now we shall look into one more powerful feature of arrays: 'striding'. |
108 When slicing lists we saw the idea of striding. Recall that if L is a list, |
97 Striding allows us to jump or skip, rows or columns by a certain interval. We can specify the step size. |
109 L[start : stop : step], |
98 c[:,:] gives us entire array |
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. |
99 we add one more ':' to row or column part to specify a step size. |
|
100 c[:, ::2] |
|
101 gives us first and third column. Since step size is two, it starts with the first column(blank before : means 0) and then we jump one column and then third(blank after : means end) |
|
102 similarly |
|
103 c[::2,:] returns a 2x3 array with the first and the third row |
|
104 |
111 |
105 and c[::2, ::2] gives us a 2x2 array with the first and the third row and column |
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. |
106 |
118 |
107 Lets us try to use these concepts of slicing and striding for doing some basic image manipulation |
119 q[::2,:] |
|
120 returns a 2x3 matrix with the first and the third row. |
108 |
121 |
109 pylab has a function named imread to read images. We shall use the '(in)famous' lena image for our experimentation. Its there on desktop. |
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. |
110 |
128 |
111 a = imread('lena.png') |
129 a = imread('lena.png') |
112 a is a numpy array with the 'RGB' values of each pixel |
130 Now a is an array with the RGB and Alpha channel values of each pixel |
113 a.shape |
131 a.shape |
114 |
132 tells us that |
115 its a 512x512x3 array. |
133 it is an 512x512x4 array. |
116 |
134 |
117 to view the image write |
135 to view the image write |
118 imshow(a) |
136 imshow(a) |
119 |
137 |
120 lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get the top left quarter by |
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 |
121 imshow(a[:255,:255]) (half of 512 is 256) |
139 imshow(a[:256,:256]) (half of 512 is 256) |
122 |
140 |
123 Lena's hat is not of much interest for us. Let's crop the image so that only her face is visible. And to do that we'll need some rough estimates of pixels. |
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. |
124 imshow(a) |
142 imshow(a) |
125 now move your mouse pointer over the image, it gives us x, y coordinates of the mouse pointer's current location. We can get rough estimate of lena's face. We can observe that Lena's face begins from somewhere around 200, 200 and ends at 400, 400. Now cropping to these boundaries is simple |
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 |
126 imshow(a[200:400, 200:400]) |
144 imshow(a[200:400, 200:400]) |
127 |
145 |
128 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, |
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, |
129 imshow(a[::2, ::2]) |
147 imshow(a[::2, ::2]) |
130 note now the size of image is just 256x256 and still quality of image is not much compromised. |
148 note that the size of image is just 256x256. |
131 ------------------------- |
149 ------------------ |
132 |
150 |
133 Till now we have covered initializing and accessing elements of arrays. Now we shall concentrate on functions available for arrays. We start this by creating 4x4 array by |
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 |
134 |
152 |
135 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
153 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
136 a |
154 a |
137 |
155 |
138 To get transpose of this matrix write |
156 To get transpose of this matrix write |
174 A bit of image processing |
192 A bit of image processing |
175 Functions available for arrays |
193 Functions available for arrays |
176 |
194 |
177 Thank you |
195 Thank you |
178 |
196 |
179 ---------------- |
|
180 We have seen |
|
181 Welcome to the Tutorial on arrays. |
|
182 |
|
183 As mentioned in [the previous tutorial] arrays are much faster and |
|
184 more efficient. In this tutorial we shall look at creating arrays, |
|
185 accessing elements and changing them. |
|
186 |
|
187 --- |
|
188 |
|
189 Let's start with creating simple arrays. We've already seen how to |
|
190 convert lists to arrays. Inputting a new array is similar to that. |
|
191 |
|
192 In []: |
|
193 |
|
194 Type /a/, to see what it is. |
|
195 |
|
196 In []: a |
|
197 |
|
198 We enter a multi-dimensional array this way - |
|
199 |
|
200 In []: c = array([[11,12,13], |
|
201 [21,22,23], |
|
202 [31,32,33]]) |
|
203 |
|
204 To see what c is, we just type c in the prompt. |
|
205 |
|
206 In []: c |
|
207 |
|
208 To see the dimensions of the array c, we use c.shape |
|
209 In []: c.shape |
|
210 |
|
211 Now let us look at some special methods of creating an |
|
212 array. There are various functions that allow us to create special |
|
213 arrays. |
|
214 |
|
215 The first one we shall look at is, /arange/. /arange/ is similar to |
|
216 the range command, except that it returns an array and accepts |
|
217 float arguments. |
|
218 |
|
219 In []: a = arange(10) |
|
220 |
|
221 In []: a |
|
222 This is the array we just created. |
|
223 |
|
224 In []: a.shape |
|
225 Note that /a/ is one dimensional and has 10 elements, as expected. |
|
226 |
|
227 We could also use a.shape to change the shape of the array a. |
|
228 In []: a.shape = 2,5 |
|
229 Note that the total size of new array must be unchanged. |
|
230 |
|
231 We type a, to see what it looks like |
|
232 In []: a |
|
233 |
|
234 ones command can be used to get an array with all the entries as |
|
235 ones. We pass it the shape of the array that we require. |
|
236 |
|
237 In []: |
|
238 |
|
239 Look at b, by printing it out. |
|
240 In []: b |
|
241 |
|
242 To create an array with all entries as ones, with it's shape |
|
243 similar to an already existing array, we use the ones_like |
|
244 command. |
|
245 In []: b = ones_like(a) |
|
246 |
|
247 zeros and zeros_like are similar commands that can give you arrays |
|
248 with all zeros. empty and empty_like give you empty arrays (arrays |
|
249 with no initialization done.) |
|
250 |
|
251 In []: b = zeros((3, 4)) |
|
252 In []: b = zeros_like(a) |
|
253 |
|
254 The identity command can be used to obtain a square array with |
|
255 ones on the main diagonal. |
|
256 |
|
257 In []: identity(3) |
|
258 |
|
259 To obtain a 2-D array, that is not necessarily square, eye command |
|
260 can be used. Look at the documentation of eye (using eye?) for |
|
261 more info. |
|
262 |
|
263 --- |
|
264 |
|
265 Now that we have learnt how to create arrays, let's move on to |
|
266 accessing elements and changing them. |
|
267 |
|
268 Let's work with the c, array which we had already created. |
|
269 |
|
270 In []: c |
|
271 |
|
272 Let's say we want to access the element 23 in c, we say |
|
273 |
|
274 In []: c[1][2] |
|
275 Note that this is similar to accessing an element inside a list of |
|
276 lists. Also, note that counting again starts from 0. |
|
277 |
|
278 But arrays provide a more convenient way to access the elements. |
|
279 In []: c[1, 2] |
|
280 |
|
281 Now, we can also change the element using a simple assignment. |
|
282 In []: c[1, 2] = -23 |
|
283 |
|
284 Let's look at accessing more than one elements at a time. We begin |
|
285 with accessing rows. |
|
286 In []: c[1] gives us the second row. (counting starts from 0) |
|
287 |
|
288 To get a column, we use a syntax that is similar to the one used |
|
289 to access a single element. |
|
290 In []: c[:,1], gives us the first column. |
|
291 |
|
292 The colon specifies that we wish to obtain all elements in that |
|
293 dimension from the array. |
|
294 |
|
295 So, we could use a more explicit way to access the second row of |
|
296 the array. |
|
297 In []: c[1,:] |
|
298 |
|
299 The colon can be used to access specific portions of the array, |
|
300 similar to the way we do with lists. |
|
301 In []: c[1,1:3] |
|
302 Observe that we get the second and third columns from the second |
|
303 row. As with lists, the number after the colon is excluded when |
|
304 slicing a portion of the array. |
|
305 |
|
306 In []: c[1:3,1] |
|
307 Now, we get the second and third rows from the first column. |
|
308 |
|
309 In []: c[1:3,1:3] |
|
310 We get the second and third rows and the second and third |
|
311 columns. |
|
312 |
|
313 The numbers before and after the colons are optional. If the |
|
314 number before the colon is omitted, it is assumed to be zero by |
|
315 default. If the element after the colon is omitted, it is assumed |
|
316 to be until the end. |
|
317 |
|
318 In []: c[1:, 1:] |
|
319 This is essentially similar to the previous example. We are using |
|
320 the default value i.e, the end, instead of specifying 3, |
|
321 explicitly. |
|
322 |
|
323 In []: c[:2, :2] |
|
324 We have omitted specifying the zero before the colon, explicitly. |
|
325 |
|
326 --- |
|
327 |
|
328 You may have observed the similarity of the semi-colon notation to |
|
329 the notation used in lists. As expected, the semi-colon notation |
|
330 also provides a way to specify a jump. This {concept/idea} is |
|
331 termed as Striding. |
|
332 |
|
333 To get every alternate row of c, starting from the first one, we say |
|
334 In []: c[::2,:] |
|
335 |
|
336 To get every alternate row of c, starting from the second one, we |
|
337 say |
|
338 In []: c[1::2,:] |
|
339 |
|
340 |
|
341 In []: c[:,::2] |
|
342 In []: c[::2,::2] |
|
343 |
|
344 --- |
|
345 |
|
346 We come to the end of this tutorial on arrays. In this tutorial, |
|
347 you've learnt how to create arrays and access, change elements. |
|
348 |
|
349 Thank you. |
|
350 |
|
351 Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
|
352 |
|
353 In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. |
|
354 |
|
355 |
|
356 Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. |
|
357 |
|
358 On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal . |
|
359 Now we shall try to create a multi-dimensional array type in your ipython terminal |
|
360 c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we do c.shape . We can see that c is a 3 by 3 matrix . |
|
361 |
|
362 There are other special methods of creating arrays as well we shall now look at them . |
|
363 The first one is the command arange which is similar to range except that it returns an array. |
|
364 We type on our Ipython interpreter a = arange(10). We see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements . |
|
365 Ones can be use to get all entries as ones . We can pass it the shape of the array as required . |
|
366 type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out . |
|
367 To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like |
|
368 command. type b= ones_like in parenthesis a . |
|