9 .. + homogeneous |
9 .. + homogeneous |
10 .. + builtins - identitiy, zeros, |
10 .. + builtins - identitiy, zeros, |
11 .. * array operations |
11 .. * array operations |
12 .. + =+ - * /= |
12 .. + =+ - * /= |
13 |
13 |
|
14 .. Objectives |
|
15 .. ---------- |
|
16 |
|
17 .. Clearly state the objectives of the LO (along with RBT level) |
|
18 |
|
19 .. Prerequisites |
|
20 .. ------------- |
|
21 |
|
22 .. 1. Name of LO-1 |
|
23 .. 2. Name of LO-2 |
|
24 .. 3. Name of LO-3 |
|
25 |
|
26 .. Author: Anoop Jacob Thomas <anoop@fossee.in> |
|
27 Internal Reviewer : Puneeth |
|
28 External Reviewer : |
|
29 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
30 |
14 =========================== |
31 =========================== |
15 Getting started with Arrays |
32 Getting started with Arrays |
16 =========================== |
33 =========================== |
17 |
34 |
|
35 .. #[Puneeth: Prerequisites and Objectives are missing. Fill them in] |
|
36 |
18 {{{ show the welcome slide }}} |
37 {{{ show the welcome slide }}} |
19 |
38 |
20 Welcome to the spoken tutorial on getting started with arrays. |
39 Welcome to the spoken tutorial on getting started with arrays. |
21 |
40 |
22 {{{ switch to next slide, outline slide }}} |
41 {{{ switch to next slide, outline slide }}} |
23 |
42 |
24 In this tutorial, we will learn about arrays, how to convert a list |
43 In this tutorial, we will learn about arrays, how to convert a list into an |
25 into an array and also why an array is preferred over lists. And array |
44 array and also why an array is preferred over lists. And array operations. |
26 operations. |
45 |
|
46 .. #[Puneeth: Fix the grammar above.] |
27 |
47 |
28 {{{ switch to next slide on overview of array }}} |
48 {{{ switch to next slide on overview of array }}} |
29 |
49 |
30 Arrays are homogeneous data structures, unlike lists, arrays cannot |
50 Arrays are homogeneous data structures, unlike lists, arrays cannot have |
31 have heterogeneous data elements, that is, it can have only one type |
51 heterogeneous data elements, that is, it can have only one type of data |
32 of data type, either all integers, or strings, or float, and not a |
52 type, either all integers, or strings, or float, and not a mix. |
33 mix. |
53 |
34 |
54 .. #[Puneeth: Use multiple short sentences, rather than one long sentence |
35 Arrays are really fast in mathematical operations when compared to |
55 I would've written something like this. |
36 lists, it is at least 80 to 100 times faster than lists. |
56 |
|
57 Unlike lists, arrays are homogeneous data structures. They can have only |
|
58 type of data, ....] |
|
59 |
|
60 Arrays are really fast in mathematical operations when compared to lists, |
|
61 it is at least 80 to 100 times faster than lists. |
|
62 |
|
63 .. #[Puneeth: For what size of an array is that the comparison? |
37 |
64 |
38 {{{ switch to the next slide, creating arrays }}} |
65 {{{ switch to the next slide, creating arrays }}} |
39 |
66 |
40 Now let us see how to create arrays. |
67 Now let us see how to create arrays. |
41 |
68 |
42 I am assuming that you have your IPython interpreter running with the |
69 I am assuming that you have your IPython interpreter running with the |
43 ``-pylab`` option, so that you have the required modules loaded. |
70 ``-pylab`` option, so that you have the required modules loaded. |
44 |
71 |
|
72 .. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it |
|
73 .. is not open?] |
|
74 |
45 To create an array we will use the function ``array()`` as, |
75 To create an array we will use the function ``array()`` as, |
|
76 |
46 :: |
77 :: |
47 |
78 |
48 a1 = array([1,2,3,4]) |
79 a1 = array([1,2,3,4]) |
49 |
80 |
50 Notice that here we created a one dimensional array. Also notice the |
81 Notice that here we created a one dimensional array. Also notice the object |
51 object we passed to create an array. Now let us see how to create a |
82 we passed to create an array. Now let us see how to create a two |
52 two dimensional array. Pause here and try to do it yourself before |
83 dimensional array. Pause here and try to do it yourself before looking at |
53 looking at the solution. |
84 the solution. |
|
85 |
|
86 .. #[Puneeth: I don't think this question can be solved by an average |
|
87 .. viewer. Questions during the tutorial, should generally be to re-iterate |
|
88 .. concepts learnt? ] |
|
89 |
|
90 .. #[Puneeth: Also, you didn't even point out that we are converting a |
|
91 .. list, using the ``array`` function. Bring the later section about |
|
92 .. converting a list, here. A separate section is not necessary, IMHO.] |
54 |
93 |
55 This is how we create two dimensional arrays. |
94 This is how we create two dimensional arrays. |
|
95 |
56 :: |
96 :: |
57 |
97 |
58 a2 = array([[1,2,3,4],[5,6,7,8]]) |
98 a2 = array([[1,2,3,4],[5,6,7,8]]) |
59 |
99 |
|
100 .. #[Puneeth: Again, you could explain a bit about the fact that we are |
|
101 .. converting a list of lists.] |
|
102 |
60 Let us see an easy method of creating an array with elements 1 to 8. |
103 Let us see an easy method of creating an array with elements 1 to 8. |
|
104 |
61 :: |
105 :: |
62 |
106 |
63 ar = arange(1,9) |
107 ar = arange(1,9) |
64 |
108 |
|
109 .. #[Puneeth: say, creating the same array as before. for some time I got |
|
110 .. confused .] |
|
111 |
65 And it created a single dimensional array of elements from 1 to 8. |
112 And it created a single dimensional array of elements from 1 to 8. |
|
113 |
66 :: |
114 :: |
67 |
115 |
68 print ar |
116 print ar |
69 |
117 |
70 And how can we make it a two dimensional array of order 2 by 4. Pause |
118 .. #[Puneeth: be consistent with voice. say, we obtained... or something.] |
71 here and try to do it yourself, try ``ar.tab`` and find a suitable |
119 |
72 method for that. |
120 And how can we make it a two dimensional array of order 2 by 4. Pause here |
|
121 and try to do it yourself, try ``ar.tab`` and find a suitable method for |
|
122 that. |
73 |
123 |
74 {{{ switch to next slide, reshape() method }}} |
124 {{{ switch to next slide, reshape() method }}} |
75 |
125 |
76 We can use the function ``reshape()`` for that purpose and it can be |
126 We can use the function ``reshape()`` for that purpose and it can be done |
77 done as, |
127 as, |
|
128 |
78 :: |
129 :: |
79 |
130 |
80 ar.reshape(2,4) |
131 ar.reshape(2,4) |
81 ar.reshape(4,2) |
132 ar.reshape(4,2) |
82 ar = ar.reshape(2,4) |
133 ar = ar.reshape(2,4) |
83 |
134 |
84 {{{ switch to next slide, creating array from list}}} |
135 {{{ switch to next slide, creating array from list}}} |
85 |
136 |
86 Now, let us see how to convert a list object to an array. As you have |
137 Now, let us see how to convert a list object to an array. As you have |
87 already seen, in both of the previous statements we have passed a |
138 already seen, in both of the previous statements we have passed a list, so |
88 list, so creating an array can be done so, first let us create a list |
139 creating an array can be done so, first let us create a list ``l1`` |
89 ``l1`` |
140 |
90 :: |
141 :: |
91 |
142 |
92 l1 = [1,2,3,4] |
143 l1 = [1,2,3,4] |
93 |
144 |
94 Now we can convert the list to an array as, |
145 Now we can convert the list to an array as, |
|
146 |
95 :: |
147 :: |
96 |
148 |
97 a3 = array(l1) |
149 a3 = array(l1) |
98 |
150 |
99 |
151 |
100 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}} |
152 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}} |
101 |
153 |
102 Create a three dimensional array of the order (2,2,4). |
154 Create a three dimensional array of the order (2,2,4). |
|
155 |
|
156 .. #[Puneeth: s/order/shape or size ?] |
103 |
157 |
104 {{{ switch to the next slide, shape of an array }}} |
158 {{{ switch to the next slide, shape of an array }}} |
105 |
159 |
106 To find the shape of an array we can use the object ``.shape``, let us |
160 To find the shape of an array we can use the object ``.shape``, let us |
107 check the shape of the arrays we have created so far, |
161 check the shape of the arrays we have created so far, |
|
162 |
|
163 .. #[Puneeth: s/object/method ?] |
|
164 |
108 :: |
165 :: |
109 |
166 |
110 a1.shape |
167 a1.shape |
111 |
168 |
112 ``a1.shape`` object is a tuple, and since a1 is a single dimensional |
169 ``a1.shape`` object is a tuple, and since a1 is a single dimensional array, |
113 array, it returned a tuple (4,). |
170 it returned a tuple (4,). |
|
171 |
|
172 .. #[Puneeth: first show a 2D array, so that it becomes easier to explain. |
|
173 .. Also, the word ``tuple`` need not be mentioned. ] |
114 |
174 |
115 {{{ switch to the next slide, unsolved exercise 2 }}} |
175 {{{ switch to the next slide, unsolved exercise 2 }}} |
116 |
176 |
117 Find out the shape of the other arrays that we have created. |
177 Find out the shape of the other arrays that we have created. |
118 |
178 |
|
179 .. #[Puneeth: solution missing.] |
|
180 |
119 {{{ Array can have only a single type of data }}} |
181 {{{ Array can have only a single type of data }}} |
120 |
182 |
121 Now let us try to create a new array with a mix of elements and see |
183 .. #[Puneeth: I guess, this whole section can be skipped. If you want to |
122 what will happen, |
184 .. keep this, just briefly mention that arrays are homogeneous in the |
|
185 .. intro, don't explain it there.] |
|
186 |
|
187 Now let us try to create a new array with a mix of elements and see what |
|
188 will happen, |
|
189 |
123 :: |
190 :: |
124 |
191 |
125 a4 = array([1,2,3,'a string']) |
192 a4 = array([1,2,3,'a string']) |
126 |
193 |
127 Well, we expected an error as previously I said that an array can have |
194 Well, we expected an error as previously I said that an array can have only |
128 only homogeneous elements, but it didn't give an error. Let us check |
195 homogeneous elements, but it didn't give an error. Let us check the values |
129 the values in the new array created. In your IPython terminal type, |
196 in the new array created. In your IPython terminal type, |
130 :: |
197 :: |
131 |
198 |
132 a4 |
199 a4 |
133 |
200 |
134 Did you notice it, |
201 Did you notice it, |
135 |
202 |
136 {{{ switch to next slide, implicit type casting }}} |
203 {{{ switch to next slide, implicit type casting }}} |
137 |
204 |
138 {{{ highlight all the array elements one by one using mouse |
205 .. #[Puneeth: typecasting may be unnecessary. (Also too advanced?) an |
139 movements }}} |
206 .. average guy wouldn't use arrays with strings.] |
140 |
207 |
141 all the elements have been implicitly type casted as string, though |
208 .. #[Puneeth: You may want to mention that float is the default dtype.] |
142 our first three elements were integers. |
209 |
|
210 {{{ highlight all the array elements one by one using mouse movements }}} |
|
211 |
|
212 all the elements have been implicitly type casted as string, though our |
|
213 first three elements were integers. |
|
214 |
|
215 .. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand |
|
216 .. what it is, can you explain? ;)] |
143 |
217 |
144 {{{ switch to the next slide, identity & zeros methods }}} |
218 {{{ switch to the next slide, identity & zeros methods }}} |
145 |
219 |
146 An identity matrix is a square matrix in which all the diagonal |
220 .. #[Puneeth: something needs to motivate this. why are we suddenly talking |
147 elements are one and rest of the elements zero. We can create an |
221 .. of an identity matrix?] |
148 identity matrix using the method ``identity()``. |
222 |
|
223 An identity matrix is a square matrix in which all the diagonal elements |
|
224 are one and rest of the elements zero. We can create an identity matrix |
|
225 using the method ``identity()``. |
149 |
226 |
150 The function ``identity()`` takes an integer argument, |
227 The function ``identity()`` takes an integer argument, |
|
228 |
151 :: |
229 :: |
152 |
230 |
153 identity(3) |
231 identity(3) |
154 |
232 |
155 As you can see the identity method returned a three by three square |
233 As you can see the identity method returned a three by three square array |
156 array with all the diagonal elements as one and the rest of the |
234 with all the diagonal elements as one and the rest of the elements as zero. |
157 elements as zero. |
235 |
158 |
236 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up. |
159 ``zeros()`` function accepts a tuple, which is the order of the array |
237 .. Clarify, explicitly.] |
160 we want to create, and it generates an array with all elements zero. |
238 |
161 |
239 ``zeros()`` function accepts a tuple, which is the order of the array we |
162 {{{ switch to the next slide, problem statement of the solved exercise |
240 want to create, and it generates an array with all elements zero. |
163 1 }}} |
241 |
164 |
242 {{{ switch to the next slide, problem statement of solved exercise 1 }}} |
165 Let us creates an array of the order four by five with all the |
243 |
166 elements zero. We can do it using the method zeros, |
244 Let us creates an array of the order four by five with all the elements |
167 :: |
245 zero. We can do it using the method zeros, :: |
168 |
246 |
169 zeros((4,5)) |
247 zeros((4,5)) |
170 |
248 |
171 Notice that we passed a tuple to the function zeros. |
249 Notice that we passed a tuple to the function zeros. |
172 |
250 |