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