|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you will be able to |
|
5 |
|
6 .. 1. Create arrays using data |
|
7 .. #. Create arrays from lists |
|
8 .. #. Basic array operations |
|
9 .. #. Creating identity matrix using ``identity()`` function. |
|
10 .. #. Learn about ``zeros()``, ``zeros_like()``, ``ones()``, |
|
11 ``ones_like()`` functions. |
|
12 |
|
13 .. Prerequisites |
|
14 .. ------------- |
|
15 |
|
16 .. 1. should have ``ipython`` and ``pylab`` installed. |
|
17 .. #. getting started with ``ipython``. |
|
18 .. #. getting started with lists. |
|
19 |
|
20 .. Author: Anoop Jacob Thomas <anoop@fossee.in> |
|
21 Internal Reviewer : Puneeth |
|
22 External Reviewer : |
|
23 Language Reviewer : Bhanukiran |
|
24 Checklist OK? : <11-11-2010,Anand, OK > [2010-10-05] |
|
25 |
|
26 =========================== |
|
27 Getting started with Arrays |
|
28 =========================== |
|
29 |
|
30 .. #[Puneeth: Prerequisites and Objectives are missing. Fill them in] |
|
31 |
|
32 {{{ show the welcome slide }}} |
|
33 |
|
34 Welcome to the spoken tutorial on getting started with arrays. |
|
35 |
|
36 {{{ switch to next slide, outline slide }}} |
|
37 |
|
38 In this tutorial, we will learn about the data structure called an array, how to convert |
|
39 a list into an array, operations on arrays and also why an array is preferred |
|
40 to lists. |
|
41 |
|
42 .. #[Puneeth: Fix the grammar above.] |
|
43 |
|
44 {{{ switch to next slide on overview of array }}} |
|
45 |
|
46 Arrays are homogeneous data structures. Unlike lists, arrays cannot have |
|
47 heterogeneous data elements, that is, they can have only one type of data |
|
48 as their entries, be them all integers, strings, or maybe floats, but not a mix. |
|
49 |
|
50 .. #[Puneeth: Use multiple short sentences, rather than one long sentence |
|
51 I would've written something like this. |
|
52 |
|
53 Unlike lists, arrays are homogeneous data structures. They can have only |
|
54 type of data, ....] |
|
55 |
|
56 Arrays of a given length are comparatively much faster in mathematical |
|
57 operations than lists of the same length, because of the fact that they are |
|
58 homogeneous data structures. |
|
59 |
|
60 .. #[Puneeth: For what size of an array is that the comparison? |
|
61 |
|
62 {{{ switch to the next slide, creating arrays }}} |
|
63 |
|
64 Now let us see how to create arrays. |
|
65 |
|
66 Run your IPython interpreter with ``-pylab`` option, to load the required |
|
67 modules to work with arrays. |
|
68 {{{ take terminal and run the following command }}} |
|
69 :: |
|
70 |
|
71 ipython -pylab |
|
72 |
|
73 .. #[Puneeth: 'I am assuming' doesn't sound right. Ask them to open if it |
|
74 .. is not open?] |
|
75 |
|
76 To create an array we will use the function ``array()`` as, |
|
77 |
|
78 :: |
|
79 |
|
80 a1 = array([1,2,3,4]) |
|
81 |
|
82 Notice that we created a one dimensional array here. Also notice the object |
|
83 we passed to create an array. We passed a list to create an array. |
|
84 |
|
85 Now let us see how to create a two dimensional array. Pause here and try to |
|
86 do it yourself before looking at the solution. |
|
87 |
|
88 {{{ switch to next slide, creating two dimensional arrays }}} |
|
89 |
|
90 .. #[Puneeth: I don't think this question can be solved by an average |
|
91 .. viewer. Questions during the tutorial, should generally be to re-iterate |
|
92 .. concepts learnt? ] |
|
93 |
|
94 .. #[Puneeth: Also, you didn't even point out that we are converting a |
|
95 .. list, using the ``array`` function. Bring the later section about |
|
96 .. converting a list, here. A separate section is not necessary, IMHO.] |
|
97 |
|
98 We create two dimensional array by converting a list of lists to an array |
|
99 as, |
|
100 |
|
101 :: |
|
102 |
|
103 a2 = array([[1,2,3,4],[5,6,7,8]]) |
|
104 |
|
105 .. #[Puneeth: Again, you could explain a bit about the fact that we are |
|
106 .. converting a list of lists.] |
|
107 |
|
108 Now let us use ``arange()`` function to create the same array as before. |
|
109 |
|
110 :: |
|
111 |
|
112 ar = arange(1,9) |
|
113 |
|
114 .. #[Puneeth: say, creating the same array as before. for some time I got |
|
115 .. confused .] |
|
116 |
|
117 And we obtained a one dimensional array with elements from 1 to 8. |
|
118 |
|
119 :: |
|
120 |
|
121 print ar |
|
122 |
|
123 .. #[Puneeth: be consistent with voice. say, we obtained... or something.] |
|
124 |
|
125 And how can we make it a two dimensional array of order 2 by 4? Pause here |
|
126 and try to do it yourself, try ``ar.tab`` and find a suitable method for |
|
127 that. |
|
128 |
|
129 {{{ switch to next slide, reshape() method }}} |
|
130 |
|
131 We can use the function ``reshape()`` for that purpose and it can be done |
|
132 as, |
|
133 |
|
134 :: |
|
135 |
|
136 ar.reshape(2,4) |
|
137 ar.reshape(4,2) |
|
138 ar = ar.reshape(2,4) |
|
139 |
|
140 {{{ switch to next slide, creating array from list}}} |
|
141 |
|
142 Now, let us see how to convert a list object to an array. As you have |
|
143 already seen, in both of the previous statements we have passed a list, so |
|
144 creating an array can be done so, first let us create a list ``l1`` |
|
145 |
|
146 :: |
|
147 |
|
148 l1 = [1,2,3,4] |
|
149 |
|
150 Now we can convert the list to an array as, |
|
151 |
|
152 :: |
|
153 |
|
154 a3 = array(l1) |
|
155 |
|
156 |
|
157 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}} |
|
158 |
|
159 Create a three dimensional array of the shape (2,2,4). |
|
160 |
|
161 .. #[Puneeth: s/order/shape or size ?] |
|
162 |
|
163 {{{ switch to the next slide, shape of an array }}} |
|
164 |
|
165 To find the shape of an array we can use the method ``.shape``, let us |
|
166 check the shape of the arrays we have created so far, |
|
167 |
|
168 .. #[Puneeth: s/object/method ?] |
|
169 |
|
170 :: |
|
171 |
|
172 a2.shape |
|
173 |
|
174 ``a2.shape`` object is a tuple, and it returned a tuple (2, 4). |
|
175 |
|
176 .. #[Puneeth: first show a 2D array, so that it becomes easier to explain. |
|
177 .. Also, the word ``tuple`` need not be mentioned. ] |
|
178 |
|
179 {{{ switch to the next slide, unsolved exercise 2 }}} |
|
180 |
|
181 Find out the shape of the other arrays that we have created. |
|
182 |
|
183 .. #[Puneeth: solution missing.] |
|
184 |
|
185 It can be done as, |
|
186 :: |
|
187 |
|
188 a1.shape |
|
189 a3.shape |
|
190 ar.shape |
|
191 |
|
192 {{{ Array can have only a single type of data }}} |
|
193 |
|
194 .. #[Puneeth: I guess, this whole section can be skipped. If you want to |
|
195 .. keep this, just briefly mention that arrays are homogeneous in the |
|
196 .. intro, don't explain it there.] |
|
197 |
|
198 Now let us try to create a new array with a mix of elements and see what |
|
199 will happen, |
|
200 |
|
201 :: |
|
202 |
|
203 a4 = array([1,2,3,'a string']) |
|
204 |
|
205 Well, we would expect an error as it has been previously mentioned that arrays handle |
|
206 elements with the same datatype, but it didn't raise an error. Let us check the values |
|
207 in the new array created. In your IPython terminal type, |
|
208 :: |
|
209 |
|
210 a4 |
|
211 |
|
212 Did you notice it, |
|
213 |
|
214 {{{ switch to next slide, implicit type casting }}} |
|
215 |
|
216 .. #[Puneeth: typecasting may be unnecessary. (Also too advanced?) an |
|
217 .. average guy wouldn't use arrays with strings.] |
|
218 |
|
219 .. #[Puneeth: You may want to mention that float is the default dtype.] |
|
220 |
|
221 {{{ highlight all the array elements one by one using mouse movements }}} |
|
222 |
|
223 all the elements have been implicitly type casted as strings, though our |
|
224 first three elements were meant to be integers. |
|
225 |
|
226 .. #[Puneeth: when I type a4 it says some ``dtype`` etc. I don't understand |
|
227 .. what it is, can you explain? ;)] |
|
228 |
|
229 {{{ switch to the next slide, identity & zeros methods }}} |
|
230 |
|
231 .. #[Puneeth: something needs to motivate this. why are we suddenly talking |
|
232 .. of an identity matrix?] |
|
233 |
|
234 Now let us see how to create an identity matrix of a given size, that is a |
|
235 two-dimensional array in which all the diagonal elements are ones and rest of the |
|
236 elements are zeros. We can create an identity matrix using the function |
|
237 ``identity()``. |
|
238 |
|
239 The function ``identity()`` takes an integer argument which specifies the |
|
240 size of the desired matrix, |
|
241 |
|
242 :: |
|
243 |
|
244 identity(3) |
|
245 |
|
246 As you can see the identity function returned a three by three square matrix |
|
247 with all the diagonal elements as ones and the rest of the elements as zeros. |
|
248 |
|
249 .. #[Puneeth: You say array here, matrix there -- it's a bit messed up. |
|
250 .. Clarify, explicitly.] |
|
251 |
|
252 ``zeros()`` function accepts a tuple, which is the order of the array that we |
|
253 want to create, and it generates an array with all elements as zeros. |
|
254 |
|
255 {{{ switch to the next slide, problem statement of solved exercise 1 }}} |
|
256 |
|
257 Let us creates an array of the order four by five with all the elements |
|
258 zero. We can do it using the method zeros, :: |
|
259 |
|
260 zeros((4,5)) |
|
261 |
|
262 Notice that we passed a tuple to the function zeros. |
|
263 |
|
264 {{{ switch to next slide, learning exercise }}} |
|
265 |
|
266 We learned two functions ``identity()`` and ``zeros()``, find out more |
|
267 about the functions ``zeros_like()``, ``ones()``, ``ones_like()``. |
|
268 |
|
269 {{{ switch to next slide, array operations }}} |
|
270 |
|
271 Try the following, first check the value of a1, |
|
272 :: |
|
273 |
|
274 a1 |
|
275 |
|
276 ``a1`` is a single dimensional array, and now try, |
|
277 :: |
|
278 |
|
279 a1 * 2 |
|
280 |
|
281 It returned a new array with all the elements multiplied by 2. |
|
282 :: |
|
283 |
|
284 a1 |
|
285 |
|
286 note that the value of a1 still remains the same. |
|
287 |
|
288 Similarly with addition, |
|
289 :: |
|
290 |
|
291 a1 + 2 |
|
292 |
|
293 it returns a new array, with all the elements summed with two. But |
|
294 again notice that the value of a1 has not been changed. |
|
295 :: |
|
296 |
|
297 a1 |
|
298 |
|
299 You may change the value of a1 by simply assigning the newly returned |
|
300 array as, |
|
301 :: |
|
302 |
|
303 a1 += 2 |
|
304 |
|
305 Notice the change in elements of a, |
|
306 :: |
|
307 |
|
308 a |
|
309 |
|
310 We can use all the mathematical operations with arrays, Now let us try this |
|
311 :: |
|
312 |
|
313 a1 = array([1,2,3,4]) |
|
314 a2 = array([1,2,3,4]) |
|
315 a1 + a2 |
|
316 |
|
317 Returns an array with element by element addition, |
|
318 :: |
|
319 |
|
320 a1 * a2 |
|
321 |
|
322 Returns an array with element by element multiplication, notice that it |
|
323 does not perform matrix multiplication. |
|
324 |
|
325 {{{ switch to next slide, summary slide }}} |
|
326 |
|
327 So this brings us to the end of this tutorial, in this tutorial we covered |
|
328 basics of arrays, learned how to create an array, saw how to convert a list |
|
329 to an array, and basic array operations etc. |
|
330 |
|
331 .. #[Puneeth: s/how to create an array/creating an array] |
|
332 |
|
333 {{{ switch to next slide, thank you }}} |
|
334 |
|
335 Thank you! |
|
336 |
|
337 .. |
|
338 Local Variables: |
|
339 mode: rst |
|
340 indent-tabs-mode: nil |
|
341 sentence-end-double-space: nil |
|
342 fill-column: 75 |
|
343 End: |