|
1 .. 4.1 LO: getting started with arrays (2) [anoop] |
|
2 .. ------------------------------------------------ |
|
3 .. * why arrays |
|
4 .. + speed - simply say |
|
5 .. + array level operations |
|
6 .. * creating arrays |
|
7 .. + direct data |
|
8 .. + list conversion |
|
9 .. + homogeneous |
|
10 .. + builtins - identitiy, zeros, |
|
11 .. * array operations |
|
12 .. + =+ - * /= |
|
13 |
|
14 =========================== |
|
15 Getting started with Arrays |
|
16 =========================== |
|
17 |
|
18 {{{ show the welcome slide }}} |
|
19 |
|
20 Welcome to the spoken tutorial on getting started with arrays. |
|
21 |
|
22 {{{ switch to next slide, outline slide }}} |
|
23 |
|
24 In this tutorial, we will learn about arrays, how to convert a list |
|
25 into an array and also why an array is preferred over lists. And array |
|
26 operations. |
|
27 |
|
28 {{{ switch to next slide on overview of array }}} |
|
29 |
|
30 Arrays are homogeneous data structures, unlike lists, arrays cannot |
|
31 have heterogeneous data elements, that is, it can have only one type |
|
32 of data type, either all integers, or strings, or float, and not a |
|
33 mix. |
|
34 |
|
35 Arrays are really fast in mathematical operations when compared to |
|
36 lists, it is at least 80 to 100 times faster than lists. |
|
37 |
|
38 {{{ switch to the next slide, creating arrays }}} |
|
39 |
|
40 Now let us see how to create arrays. |
|
41 |
|
42 I am assuming that you have your IPython interpreter running with the |
|
43 ``-pylab`` option, so that you have the required modules loaded. |
|
44 |
|
45 To create an array we will use the function ``array()`` as, |
|
46 :: |
|
47 |
|
48 a1 = array([1,2,3,4]) |
|
49 |
|
50 Notice that here we created a one dimensional array. Also notice the |
|
51 object we passed to create an array. Now let us see how to create a |
|
52 two dimensional array. Pause here and try to do it yourself before |
|
53 looking at the solution. |
|
54 |
|
55 This is how we create two dimensional arrays. |
|
56 :: |
|
57 |
|
58 a2 = array([[1,2,3,4],[5,6,7,8]]) |
|
59 |
|
60 Let us see an easy method of creating an array with elements 1 to 8. |
|
61 :: |
|
62 |
|
63 ar = arange(1,9) |
|
64 |
|
65 And it created a single dimensional array of elements from 1 to 8. |
|
66 :: |
|
67 |
|
68 print ar |
|
69 |
|
70 And how can we make it a two dimensional array of order 2 by 4. Pause |
|
71 here and try to do it yourself, try ``ar.tab`` and find a suitable |
|
72 method for that. |
|
73 |
|
74 We can use the function ``reshape()`` for that purpose and it can be |
|
75 done as, |
|
76 :: |
|
77 |
|
78 ar.reshape(2,4) |
|
79 ar.reshape(4,2) |
|
80 ar = ar.reshape(2,4) |
|
81 |
|
82 Now, let us see how to convert a list object to an array. As you have |
|
83 already seen, in both of the previous statements we have passed a |
|
84 list, so creating an array can be done so, first let us create a list |
|
85 ``l1`` |
|
86 :: |
|
87 |
|
88 l1 = [1,2,3,4] |
|
89 |
|
90 Now we can convert the list to an array as, |
|
91 :: |
|
92 |
|
93 a3 = array(l1) |
|
94 |
|
95 |
|
96 {{{ switch to the next slide, problem statement of unsolved exercise 1 }}} |
|
97 |
|
98 Create a three dimensional array of the order (2,2,4). |
|
99 |
|
100 {{{ switch to the next slide, shape of an array }}} |
|
101 |
|
102 To find the shape of an array we can use the object ``.shape``, let us |
|
103 check the shape of the arrays we have created so far, |
|
104 :: |
|
105 |
|
106 a1.shape |
|
107 |
|
108 ``a1.shape`` object is a tuple, and since a1 is a single dimensional |
|
109 array, it returned a tuple (4,). |
|
110 |
|
111 {{{ switch to the next slide, unsolved exercise 2 }}} |
|
112 |
|
113 Find out the shape of the other two arrays that we have created. |
|
114 |
|
115 {{{ Array can have only a single type of data }}} |
|
116 |
|
117 Now let us try to create a new array with a mix of elements and see |
|
118 what will happen, |
|
119 :: |
|
120 |
|
121 a4 = array([1,2,3,'a string']) |
|
122 |
|
123 Well, we expected an error as previously I said that an array can have |
|
124 only homogeneous elements, but it didn't give an error. Let us check |
|
125 the values in the new array created. In your IPython terminal type, |
|
126 :: |
|
127 |
|
128 a4 |
|
129 |
|
130 Did you notice it, |
|
131 |
|
132 {{{ highlight all the array elements one by one using mouse |
|
133 movements }}} |
|
134 |
|
135 all the elements have been implicitly type casted as string, though |
|
136 our first three elements were integers. |
|
137 |
|
138 {{{ switch to the next slide, identity & zeros methods }}} |
|
139 |
|
140 An identity matrix is a square matrix in which all the diagonal |
|
141 elements are one and rest of the elements zero. We can create an |
|
142 identity matrix using the method ``identity()``. |
|
143 |
|
144 The function ``identity()`` takes an integer argument, |
|
145 :: |
|
146 |
|
147 identity(3) |
|
148 |
|
149 As you can see the identity method returned a three by three square |
|
150 array with all the diagonal elements as one and the rest of the |
|
151 elements as zero. |
|
152 |
|
153 ``zeros()`` function accepts a tuple, which is the order of the array |
|
154 we want to create, and it generates an array with all elements zero. |
|
155 |
|
156 {{{ switch to the next slide, problem statement of the solved exercise |
|
157 1 }}} |
|
158 |
|
159 Let us creates an array of the order four by five with all the |
|
160 elements zero. We can do it using the method zeros, |
|
161 :: |
|
162 |
|
163 zeros((4,5)) |
|
164 |
|
165 Notice that we passed a tuple to the function zeros. |
|
166 |
|
167 {{{ switch to next slide, learning exercise }}} |
|
168 |
|
169 We learned two functions ``identity()`` and ``zeros()``, find out more |
|
170 about the functions ``zeros_like()``, ``ones()``, ``ones_like()``. |
|
171 |
|
172 {{{ switch to next slide, array operations }}} |
|
173 |
|
174 Try the following, first check the value of a1, |
|
175 :: |
|
176 |
|
177 a1 |
|
178 |
|
179 ``a1`` is a single dimensional array, and now try, |
|
180 :: |
|
181 |
|
182 a1 * 2 |
|
183 |
|
184 It returned a new array with all the elements multiplied by 2. |
|
185 :: |
|
186 |
|
187 a1 |
|
188 |
|
189 note that the value of a1 still remains the same. |
|
190 |
|
191 Similarly with addition, |
|
192 :: |
|
193 |
|
194 a1 + 2 |
|
195 |
|
196 it returns a new array, with all the elements summed with two. But |
|
197 again notice that the value of a1 has not been changed. |
|
198 :: |
|
199 |
|
200 a1 |
|
201 |
|
202 You may change the value of a1 by simply assigning the newly returned |
|
203 array as, |
|
204 :: |
|
205 |
|
206 a1 += 2 |
|
207 |
|
208 Notice the change in elements of a, |
|
209 :: |
|
210 |
|
211 a |
|
212 |
|
213 We can use all the mathematical operations with arrays, Now let us try |
|
214 this |
|
215 :: |
|
216 |
|
217 a1 = array([1,2,3,4]) |
|
218 a2 = array([1,2,3,4]) |
|
219 a1 + a2 |
|
220 |
|
221 Returns an array with element by element addition, |
|
222 :: |
|
223 |
|
224 a1 * a2 |
|
225 |
|
226 Returns an array with element by element multiplication, notice that |
|
227 it does not perform matrix multiplication. |
|
228 |
|
229 {{{ switch to next slide, recap slide }}} |
|
230 |
|
231 So this brings us to the end of this tutorial, in this tutorial we |
|
232 covered basics of arrays, how to create an array, converting a list to |
|
233 an array, basic array operations etc. |
|
234 |
|
235 {{{ switch to next slide, thank you }}} |
|
236 |
|
237 Thank you! |
|
238 |
|
239 .. Author: Anoop Jacob Thomas <anoop@fossee.in> |
|
240 Reviewer 1: |
|
241 Reviewer 2: |
|
242 External reviewer: |