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