32 |
32 |
33 Welcome to the spoken tutorial on getting started with arrays. |
33 Welcome to the spoken tutorial on getting started with arrays. |
34 |
34 |
35 {{{ switch to next slide, outline slide }}} |
35 {{{ switch to next slide, outline slide }}} |
36 |
36 |
37 In this tutorial, we will learn about arrays, how to convert a list into an |
37 In this tutorial, we will learn about arrays, we will learn how to convert |
38 array and also why an array is preferred over lists. And array operations. |
38 a list into an array array operations and also why an array is preferred |
|
39 over lists. |
39 |
40 |
40 .. #[Puneeth: Fix the grammar above.] |
41 .. #[Puneeth: Fix the grammar above.] |
41 |
42 |
42 {{{ switch to next slide on overview of array }}} |
43 {{{ switch to next slide on overview of array }}} |
43 |
44 |
44 Arrays are homogeneous data structures, unlike lists, arrays cannot have |
45 Arrays are homogeneous data structures. Unlike lists, arrays cannot have |
45 heterogeneous data elements, that is, it can have only one type of data |
46 heterogeneous data elements, that is, it can have only one type of data |
46 type, either all integers, or strings, or float, and not a mix. |
47 type, either all integers, or strings, or float, and not a mix. |
47 |
48 |
48 .. #[Puneeth: Use multiple short sentences, rather than one long sentence |
49 .. #[Puneeth: Use multiple short sentences, rather than one long sentence |
49 I would've written something like this. |
50 I would've written something like this. |
50 |
51 |
51 Unlike lists, arrays are homogeneous data structures. They can have only |
52 Unlike lists, arrays are homogeneous data structures. They can have only |
52 type of data, ....] |
53 type of data, ....] |
53 |
54 |
54 Arrays are really fast in mathematical operations when compared to lists, |
55 Arrays are really fast in mathematical operations when compared to lists, |
55 it is at least 80 to 100 times faster than lists. |
56 because of the same type of data in arrays. |
56 |
57 |
57 .. #[Puneeth: For what size of an array is that the comparison? |
58 .. #[Puneeth: For what size of an array is that the comparison? |
58 |
59 |
59 {{{ switch to the next slide, creating arrays }}} |
60 {{{ switch to the next slide, creating arrays }}} |
60 |
61 |
61 Now let us see how to create arrays. |
62 Now let us see how to create arrays. |
62 |
63 |
63 I am assuming that you have your IPython interpreter running with the |
64 Run your IPython interpreter with ``-pylab`` option, to load the required |
64 ``-pylab`` option, so that you have the required modules loaded. |
65 modules to work with arrays. |
|
66 {{{ take terminal and run the following command }}} |
|
67 :: |
|
68 |
|
69 ipython -pylab |
65 |
70 |
66 .. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it |
71 .. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it |
67 .. is not open?] |
72 .. is not open?] |
68 |
73 |
69 To create an array we will use the function ``array()`` as, |
74 To create an array we will use the function ``array()`` as, |
71 :: |
76 :: |
72 |
77 |
73 a1 = array([1,2,3,4]) |
78 a1 = array([1,2,3,4]) |
74 |
79 |
75 Notice that here we created a one dimensional array. Also notice the object |
80 Notice that here we created a one dimensional array. Also notice the object |
76 we passed to create an array. Now let us see how to create a two |
81 we passed to create an array. We passed a list to create an array. |
77 dimensional array. Pause here and try to do it yourself before looking at |
82 |
78 the solution. |
83 Now let us see how to create a two dimensional array. Pause here and try to |
|
84 do it yourself before looking at the solution. |
|
85 |
|
86 {{{ switch to next slide, creating two dimensional arrays }}} |
79 |
87 |
80 .. #[Puneeth: I don't think this question can be solved by an average |
88 .. #[Puneeth: I don't think this question can be solved by an average |
81 .. viewer. Questions during the tutorial, should generally be to re-iterate |
89 .. viewer. Questions during the tutorial, should generally be to re-iterate |
82 .. concepts learnt? ] |
90 .. concepts learnt? ] |
83 |
91 |
84 .. #[Puneeth: Also, you didn't even point out that we are converting a |
92 .. #[Puneeth: Also, you didn't even point out that we are converting a |
85 .. list, using the ``array`` function. Bring the later section about |
93 .. list, using the ``array`` function. Bring the later section about |
86 .. converting a list, here. A separate section is not necessary, IMHO.] |
94 .. converting a list, here. A separate section is not necessary, IMHO.] |
87 |
95 |
88 This is how we create two dimensional arrays. |
96 We create two dimensional array by converting a list of lists to an array |
|
97 as, |
89 |
98 |
90 :: |
99 :: |
91 |
100 |
92 a2 = array([[1,2,3,4],[5,6,7,8]]) |
101 a2 = array([[1,2,3,4],[5,6,7,8]]) |
93 |
102 |
94 .. #[Puneeth: Again, you could explain a bit about the fact that we are |
103 .. #[Puneeth: Again, you could explain a bit about the fact that we are |
95 .. converting a list of lists.] |
104 .. converting a list of lists.] |
96 |
105 |
97 Let us see an easy method of creating an array with elements 1 to 8. |
106 Now let us use ``arange()`` function to create the same array as before. |
98 |
107 |
99 :: |
108 :: |
100 |
109 |
101 ar = arange(1,9) |
110 ar = arange(1,9) |
102 |
111 |
103 .. #[Puneeth: say, creating the same array as before. for some time I got |
112 .. #[Puneeth: say, creating the same array as before. for some time I got |
104 .. confused .] |
113 .. confused .] |
105 |
114 |
106 And it created a single dimensional array of elements from 1 to 8. |
115 And we obtained a single dimensional array with elements from 1 to 8. |
107 |
116 |
108 :: |
117 :: |
109 |
118 |
110 print ar |
119 print ar |
111 |
120 |
156 |
165 |
157 .. #[Puneeth: s/object/method ?] |
166 .. #[Puneeth: s/object/method ?] |
158 |
167 |
159 :: |
168 :: |
160 |
169 |
161 a1.shape |
170 a2.shape |
162 |
171 |
163 ``a1.shape`` object is a tuple, and since a1 is a single dimensional array, |
172 ``a2.shape`` object is a tuple, and it returned a tuple (2, 4). |
164 it returned a tuple (4,). |
|
165 |
173 |
166 .. #[Puneeth: first show a 2D array, so that it becomes easier to explain. |
174 .. #[Puneeth: first show a 2D array, so that it becomes easier to explain. |
167 .. Also, the word ``tuple`` need not be mentioned. ] |
175 .. Also, the word ``tuple`` need not be mentioned. ] |
168 |
176 |
169 {{{ switch to the next slide, unsolved exercise 2 }}} |
177 {{{ switch to the next slide, unsolved exercise 2 }}} |
170 |
178 |
171 Find out the shape of the other arrays that we have created. |
179 Find out the shape of the other arrays that we have created. |
172 |
180 |
173 .. #[Puneeth: solution missing.] |
181 .. #[Puneeth: solution missing.] |
|
182 |
|
183 It can be done as, |
|
184 :: |
|
185 |
|
186 a1.shape |
|
187 a3.shape |
|
188 ar.shape |
174 |
189 |
175 {{{ Array can have only a single type of data }}} |
190 {{{ Array can have only a single type of data }}} |
176 |
191 |
177 .. #[Puneeth: I guess, this whole section can be skipped. If you want to |
192 .. #[Puneeth: I guess, this whole section can be skipped. If you want to |
178 .. keep this, just briefly mention that arrays are homogeneous in the |
193 .. keep this, just briefly mention that arrays are homogeneous in the |
212 {{{ switch to the next slide, identity & zeros methods }}} |
227 {{{ switch to the next slide, identity & zeros methods }}} |
213 |
228 |
214 .. #[Puneeth: something needs to motivate this. why are we suddenly talking |
229 .. #[Puneeth: something needs to motivate this. why are we suddenly talking |
215 .. of an identity matrix?] |
230 .. of an identity matrix?] |
216 |
231 |
217 An identity matrix is a square matrix in which all the diagonal elements |
232 Now let us see how to create identity matrix, an identity matrix is a |
218 are one and rest of the elements zero. We can create an identity matrix |
233 square matrix in which all the diagonal elements are one and rest of the |
219 using the method ``identity()``. |
234 elements zero. We can create an identity matrix using the method |
|
235 ``identity()``. |
220 |
236 |
221 The function ``identity()`` takes an integer argument, |
237 The function ``identity()`` takes an integer argument, |
222 |
238 |
223 :: |
239 :: |
224 |
240 |
225 identity(3) |
241 identity(3) |
226 |
242 |
227 As you can see the identity method returned a three by three square array |
243 As you can see the identity method returned a three by three square matrix |
228 with all the diagonal elements as one and the rest of the elements as zero. |
244 with all the diagonal elements as one and the rest of the elements as zero. |
229 |
245 |
230 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up. |
246 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up. |
231 .. Clarify, explicitly.] |
247 .. Clarify, explicitly.] |
232 |
248 |
304 does not perform matrix multiplication. |
320 does not perform matrix multiplication. |
305 |
321 |
306 {{{ switch to next slide, summary slide }}} |
322 {{{ switch to next slide, summary slide }}} |
307 |
323 |
308 So this brings us to the end of this tutorial, in this tutorial we covered |
324 So this brings us to the end of this tutorial, in this tutorial we covered |
309 basics of arrays, how to create an array, converting a list to an array, |
325 basics of arrays, learned how to create an array, saw how to convert a list |
310 basic array operations etc. |
326 to an array, and basic array operations etc. |
311 |
327 |
312 .. #[Puneeth: s/how to create an array/creating an array] |
328 .. #[Puneeth: s/how to create an array/creating an array] |
313 |
329 |
314 {{{ switch to next slide, thank you }}} |
330 {{{ switch to next slide, thank you }}} |
315 |
331 |