175
|
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 |
I am assuming that you have your IPython interpreter running with the
|
|
41 |
``-pylab`` option, so that you have the required modules loaded.
|
|
42 |
|
|
43 |
To create an array we will use the function ``array()`` as,
|
|
44 |
::
|
|
45 |
|
|
46 |
a1 = array([1,2,3,4])
|
|
47 |
|
|
48 |
Notice that here we created a one dimensional array. Also notice the
|
|
49 |
object we passed to create an array. Now let us see how to create a
|
|
50 |
two dimensional array.
|
|
51 |
::
|
|
52 |
|
|
53 |
a2 = array([[1,2,3,4],[5,6,7,8]])
|
|
54 |
|
|
55 |
Now, let us see how to convert a list object to an array. As you have
|
|
56 |
already seen, in both of the previous statements we have passed a
|
|
57 |
list, so creating an array can be done so, first let us create a list
|
|
58 |
``l1``
|
|
59 |
::
|
|
60 |
|
|
61 |
l1 = [1,2,3,4]
|
|
62 |
|
|
63 |
Now we can convert the list to an array as,
|
|
64 |
::
|
|
65 |
|
|
66 |
a3 = array(l1)
|
|
67 |
|
|
68 |
|
|
69 |
{{{ switch to the next slide, problem statement of unsolved exercise 1 }}}
|
|
70 |
|
|
71 |
Create a three dimensional array of the order (2,2,4).
|
|
72 |
|
|
73 |
{{{ switch to the next slide, shape of an array }}}
|
|
74 |
|
|
75 |
To find the shape of an array we can use the object ``.shape``, let us
|
|
76 |
check the shape of the arrays we have created so far,
|
|
77 |
::
|
|
78 |
|
|
79 |
a1.shape
|
|
80 |
|
|
81 |
``a1.shape`` object is a tuple, and since a1 is a single dimensional
|
|
82 |
array, it returned a tuple (4,).
|
|
83 |
|
|
84 |
{{{ switch to the next slide, unsolved exercise 2 }}}
|
|
85 |
|
|
86 |
Find out the shape of the other two arrays that we have created.
|
|
87 |
|
|
88 |
{{{ Array can have only a single type of data }}}
|
|
89 |
|
|
90 |
Now let us try to create a new array with a mix of elements and see
|
|
91 |
what will happen,
|
|
92 |
::
|
|
93 |
|
|
94 |
a4 = array([1,2,3,'a string'])
|
|
95 |
|
|
96 |
Well, we expected an error as previously I said that an array can have
|
|
97 |
only homogeneous elements, but it didn't give an error. Let us check
|
|
98 |
the values in the new array created. In your IPython terminal type,
|
|
99 |
::
|
|
100 |
|
|
101 |
a4
|
|
102 |
|
|
103 |
Did you notice it,
|
|
104 |
|
|
105 |
{{{ highlight all the array elements one by one using mouse
|
|
106 |
movements }}}
|
|
107 |
|
|
108 |
all the elements have been implicitly type casted as string, though
|
|
109 |
our first three elements were integers.
|
|
110 |
|
|
111 |
{{{ switch to the next slide, identity & zeros methods }}}
|
|
112 |
|
|
113 |
An identity matrix is a square matrix in which all the diagonal
|
|
114 |
elements are one and rest of the elements zero. We can create an
|
|
115 |
identity matrix using the method ``identity()``.
|
|
116 |
|
|
117 |
The function ``identity()`` takes an integer argument,
|
|
118 |
::
|
|
119 |
|
|
120 |
identity(3)
|
|
121 |
|
|
122 |
As you can see the identity method returned a three by three square
|
|
123 |
array with all the diagonal elements as one and the rest of the
|
|
124 |
elements as zero.
|
|
125 |
|
|
126 |
``zeros()`` function accepts a tuple, which is the order of the array
|
|
127 |
we want to create, and it generates an array with all elements zero.
|
|
128 |
|
|
129 |
{{{ switch to the next slide, problem statement of the solved exercise
|
|
130 |
1 }}}
|
|
131 |
|
|
132 |
Let us creates an array of the order four by five with all the
|
|
133 |
elements zero. We can do it using the method zeros,
|
|
134 |
::
|
|
135 |
|
|
136 |
zeros((4,5))
|
|
137 |
|
|
138 |
Notice that we passed a tuple to the function zeros.
|
|
139 |
|
|
140 |
{{{ switch to next slide, learning exercise }}}
|
|
141 |
|
|
142 |
We learned two functions ``identity()`` and ``zeros()``, find out more
|
|
143 |
about the functions ``zeros_like()``, ``ones()``, ``ones_like()``.
|
|
144 |
|
|
145 |
{{{ switch to next slide, array operations }}}
|
|
146 |
|
|
147 |
Try the following, first check the value of a1,
|
|
148 |
::
|
|
149 |
|
|
150 |
a1
|
|
151 |
|
|
152 |
``a1`` is a single dimensional array, and now try,
|
|
153 |
::
|
|
154 |
|
|
155 |
a1 * 2
|
|
156 |
|
|
157 |
It returned a new array with all the elements multiplied by 2.
|
|
158 |
::
|
|
159 |
|
|
160 |
a1
|
|
161 |
|
|
162 |
note that the value of a1 still remains the same.
|
|
163 |
|
|
164 |
Similarly with addition,
|
|
165 |
::
|
|
166 |
|
|
167 |
a1 + 2
|
|
168 |
|
|
169 |
it returns a new array, with all the elements summed with two. But
|
|
170 |
again notice that the value of a1 has not been changed.
|
|
171 |
::
|
|
172 |
|
|
173 |
a1
|
|
174 |
|
|
175 |
You may change the value of a1 by simply assigning the newly returned
|
|
176 |
array as,
|
|
177 |
::
|
|
178 |
|
|
179 |
a1 += 2
|
|
180 |
|
|
181 |
Notice the change in elements of a,
|
|
182 |
::
|
|
183 |
|
|
184 |
a
|
|
185 |
|
|
186 |
We can use all the mathematical operations with arrays, Now let us try
|
|
187 |
this
|
|
188 |
::
|
|
189 |
|
|
190 |
a1 = array([1,2,3,4])
|
|
191 |
a2 = array([1,2,3,4])
|
|
192 |
a1 + a2
|
|
193 |
|
|
194 |
Returns an array with element by element addition,
|
|
195 |
::
|
|
196 |
|
|
197 |
a1 * a2
|
|
198 |
|
|
199 |
Returns an array with element by element multiplication, notice that
|
|
200 |
it does not perform matrix multiplication.
|
|
201 |
|
|
202 |
{{{ switch to next slide, recap slide }}}
|
|
203 |
|
|
204 |
So this brings us to the end of this tutorial, in this tutorial we covered basics of arrays, how to create an array, converting a list to an array, basic array operations etc.
|
|
205 |
|
|
206 |
{{{ switch to next slide, thank you }}}
|
|
207 |
|
|
208 |
Thank you!
|
|
209 |
|
|
210 |
.. Author: Anoop Jacob Thomas <anoop@fossee.in>
|
|
211 |
Reviewer 1:
|
|
212 |
Reviewer 2:
|
|
213 |
External reviewer:
|