18 c.shape |
18 c.shape |
19 |
19 |
20 A few other handy array initialization methods are also available to make life easier. |
20 A few other handy array initialization methods are also available to make life easier. |
21 say we want to create an array of size 3x4 with all the elements initialized to 1, we use |
21 say we want to create an array of size 3x4 with all the elements initialized to 1, we use |
22 b = ones((3, 4)) |
22 b = ones((3, 4)) |
23 and b will be |
23 and b is |
24 b |
24 b |
25 similarly, say we already have an array, and we want to create one more array with the same shape and initial values to be one, for that we will use |
25 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: |
26 d = ones_like(c) |
26 d = ones_like(c) |
27 and d will be 3x3 array with all values 1 |
27 and d is a 3x3 array with all values equal to 1 |
28 |
28 |
29 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(array) of given order |
29 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 |
30 i = identity(3) |
30 i = identity(3) |
31 i |
31 i |
32 i = identity(5) |
32 i = identity(5) |
33 i |
33 i |
34 |
34 |
|
35 Note that identity takes just one argument since identity matrix is always a square matrix. |
|
36 |
35 ---------------- |
37 ---------------- |
36 Now that we have covered creation of arrays, we will see how to access and change values of particular elements. |
38 Now that we have covered creation of arrays, we shall see how to access and change values of particular elements. |
37 We created one 3x3 matrix earlier, |
39 Remember we created a 3x3 matrix earlier, |
38 c |
40 c |
39 |
41 |
40 to access the element 23 we type |
42 to access the element 23 we type |
41 c[1][2] |
43 c[1][2] |
42 |
44 |
43 It is second row third column of c. Note that index values of arrays also start from 0. |
45 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. |
44 Alternative and popular way of doing this is |
46 Alternatively, the more popular way of doing the same is |
45 c[1, 2] |
47 c[1, 2] |
46 |
48 |
47 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
49 here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
48 |
50 |
49 to access particular row completely we simply skip the column value |
51 to access particular row completely we simply skip the column value |
50 c[1] |
52 c[1] |
51 will give us the entire second row. |
53 gives us the entire second row. |
52 |
54 |
53 The way by which we access one element of array, we use that itself to assign new value |
55 We can assign a new value to an element, the same way we accessed it. For eg., |
54 c[1, 1] = -22 |
56 c[1, 1] = -22 |
55 c |
57 c |
56 |
58 |
57 and same thing can be used for entire row |
59 In order to change an entire row we type: |
58 c[1] = 0 |
60 c[1] = 0 |
59 c |
61 c |
60 as we can see, second row all elements are now 0 |
62 as we can see, all elements of the second row are now 0 |
61 |
63 |
62 Accessing a row is straight forward we skip column part |
64 Accessing a row is straight forward we skip column part |
63 but to access whole column we have to use |
65 but the same cannot be done to access columns. In order to access the whole column we have to use ':' |
64 c[:,2] |
66 c[:,2] |
65 will return third column. |
67 returns the third column. |
66 here the ':' part mentioned for row value symbolises entire row. |
68 here the ':' part mentioned for row value symbolises entire row. |
67 the c[1] we were using earlier can also be written as c[1,:] |
69 the c[1] we were using earlier can also be written as c[1,:] |
68 |
70 |
69 ':' actually takes two value. for any row or column we can mention |
71 ':' actually takes two value. for any row or column we can mention |
70 start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding |
72 start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding |
71 c[0:2,:] |
73 c[0:2,:] |
72 will result in rows starting from first(0) till second and all columns. Note here that 'end' in our case, '2' would not be included in resulting array. |
74 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. |
73 |
75 |
74 c[1:3,:] |
76 c[1:3,:] |
75 gives second and third row. |
77 gives second and third row. |
76 |
78 |
77 similarly we can try this on columns also: |
79 similarly we can try this on columns also: |
81 There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column. |
83 There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column. |
82 It actually means if we don't specify start and end part of slice default is from zero to end. |
84 It actually means if we don't specify start and end part of slice default is from zero to end. |
83 |
85 |
84 so |
86 so |
85 c[:, :2] |
87 c[:, :2] |
86 will also give us first two columns |
88 also gives us first two columns |
87 and c[:, 1:] will return last columns. |
89 and c[:, 1:] returns all columns excluding the 0th column. |
88 |
90 |
89 c[1:, :2] |
91 c[1:, :2] |
90 returns first two columns of last two rows |
92 returns first two columns of all rows excepting the 0th row. |
91 |
93 |
92 Now we will look into one more powerful feature of arrays: 'striding'. |
94 Now we shall look into one more powerful feature of arrays: 'striding'. |
93 Striding allows us to jump or skip rows or columns by certain interval. We can specify the step size. |
95 Striding allows us to jump or skip, rows or columns by a certain interval. We can specify the step size. |
94 c[:,:] will give us entire array |
96 c[:,:] gives us entire array |
95 we add one more ':' to row or column part to specify a step size. |
97 we add one more ':' to row or column part to specify a step size. |
96 c[:, ::2] |
98 c[:, ::2] |
97 will give us first and third column. Since step size is two, it start with first column(blank before : means 0) and then we jump one column and then third(blank after : means end) |
99 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) |
98 similarly |
100 similarly |
99 c[::2,:] returns 2x3 array with first and third row |
101 c[::2,:] returns a 2x3 array with the first and the third row |
100 |
102 |
101 and c[::2, ::2] will give us 2x2 array with first and third row and column |
103 and c[::2, ::2] gives us a 2x2 array with the first and the third row and column |
102 |
104 |
103 Lets us try to use these concepts of slicing and striding for doing some basic image manipulation |
105 Lets us try to use these concepts of slicing and striding for doing some basic image manipulation |
104 |
106 |
105 pylab has a function imread to read images. We will use '(in)famous' lena image for our experimentation. Its there on desktop. |
107 pylab has a function named imread to read images. We shall use the '(in)famous' lena image for our experimentation. Its there on desktop. |
106 |
108 |
107 a = imread('lena.png') |
109 a = imread('lena.png') |
108 a is a numpy array with the 'RGB' values of each pixel |
110 a is a numpy array with the 'RGB' values of each pixel |
109 a.shape |
111 a.shape |
110 |
112 |
111 its a 512x512x3 array. |
113 its a 512x512x3 array. |
112 |
114 |
113 to view the image write |
115 to view the image write |
114 imshow(a) |
116 imshow(a) |
115 |
117 |
116 lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get top left quarter by |
118 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 |
117 imshow(a[:255,:255]) (half of 512 is 256) |
119 imshow(a[:255,:255]) (half of 512 is 256) |
118 |
120 |
119 But hat is not 'interesting' part of lena. Lets crop the image so that only her face is visible. for that we will need some rough estimates of pixels. |
121 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. |
120 imshow(a) |
122 imshow(a) |
121 now move your mouse cursor over the image, it will give us x, y coordinates where ever we take our cursor. We can get rough estimate of lena's face now cropping to those boundaries is simple |
123 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 |
122 imshow(a[200:400, 200:400]) |
124 imshow(a[200:400, 200:400]) |
123 |
125 |
124 Next we will try striding on this image. We will resize the image by skipping each alternate pixel. We have already seen how to skip alternate elements so, |
126 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, |
125 imshow(a[::2, ::2]) |
127 imshow(a[::2, ::2]) |
126 note now the size of image is just 256x256 and still quality of image is not much compromised. |
128 note now the size of image is just 256x256 and still quality of image is not much compromised. |
127 ------------------------- |
129 ------------------------- |
128 |
130 |
129 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 |
131 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 |
131 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
133 a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
132 a |
134 a |
133 |
135 |
134 To get transpose of this matrix write |
136 To get transpose of this matrix write |
135 a.T |
137 a.T |
136 sum() function returns sum of all the elements of a matrix. |
138 |
|
139 The sum() function returns sum of all the elements of a matrix. |
137 sum(a) |
140 sum(a) |
138 |
141 |
139 lets create one more array for checking more operations |
142 let's create one more array for checking more operations |
140 b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) |
143 b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) |
141 |
144 |
142 + will take care of matrix additions |
145 + takes care of matrix additions |
143 a + b |
146 a + b |
144 |
147 |
145 lets try multiplication now, |
148 lets try multiplication now, |
146 a * b will return element wise product of two matrices. |
149 a * b returns the element wise product of two matrices. |
147 |
150 |
148 To get matrix product of a and b we use |
151 To get matrix product of a and b we use |
149 dot(a, b) |
152 dot(a, b) |
150 |
153 |
151 and to get inverse of matrix |
154 To get the inverse of a matrix we use, |
152 |
155 |
153 inv(a) |
156 inv(a) |
154 |
157 |
155 det(a) returns determinant of matrix a |
158 det(a) returns determinant of matrix a |
156 |
159 |
157 we shall create one array e |
160 we shall create an array e |
158 e = array([[3,2,4],[2,0,2],[4,2,3]]) |
161 e = array([[3,2,4],[2,0,2],[4,2,3]]) |
159 and then to evaluate eigenvalues of array |
162 and then to evaluate eigenvalues of array |
160 eig(a) |
163 eig(e) |
161 it returns both eigen values and eigen vector of given matrix |
164 it returns both eigen values and eigen vector of given matrix |
162 to get only eigen values use |
165 to get only eigen values use |
163 eigvals(a) |
166 eigvals(e) |
164 |
167 |
165 This brings us to end of this session. We have covered Matrices |
168 This brings us to end of this session. We have covered Matrices |
166 Initialization |
169 Initialization |
167 Slicing |
170 Slicing |
168 Striding |
171 Striding |
349 |
352 |
350 |
353 |
351 Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. |
354 Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. |
352 |
355 |
353 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 . |
356 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 . |
354 Now we will try to create a multi-dimensional array type in your ipython terminal |
357 Now we shall try to create a multi-dimensional array type in your ipython terminal |
355 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 will do c.shape . We can see that c is a 3 by 3 matrix . |
358 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 . |
356 |
359 |
357 There are other special methods of creating arrays as well we will now look at them . |
360 There are other special methods of creating arrays as well we shall now look at them . |
358 The first one is the command arange which is similar to range except that it returns an array. |
361 The first one is the command arange which is similar to range except that it returns an array. |
359 We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements . |
362 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 . |
360 Ones can be use to get all entries as ones . We can pass it the shape of the array as required . |
363 Ones can be use to get all entries as ones . We can pass it the shape of the array as required . |
361 type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out . |
364 type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out . |
362 To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like |
365 To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like |
363 command. type b= ones_like in parenthesis a . |
366 command. type b= ones_like in parenthesis a . |