|
1 * Arrays |
|
2 *** Outline |
|
3 ***** Introduction |
|
4 ******* Why do we want to do that? |
|
5 ******* We shall use arrays (introduced before) for matrices |
|
6 ******* Arsenal Required |
|
7 ********* working knowledge of lists |
|
8 ***** Initializing matrices |
|
9 ******* Entering element-wise |
|
10 ******* using special functions |
|
11 ***** Accessing and Changing elements |
|
12 ******* Slicing |
|
13 ******* Striding |
|
14 ***** {{Simple operations??}} |
|
15 *** Script |
|
16 Welcome to the Tutorial on arrays. |
|
17 |
|
18 As mentioned in [the previous tutorial] arrays are much faster and |
|
19 more efficient. In this tutorial we shall look at creating arrays, |
|
20 accessing elements and changing them. |
|
21 |
|
22 --- |
|
23 |
|
24 Let's start with creating simple arrays. We've already seen how to |
|
25 convert lists to arrays. Entering an array is similar to that. |
|
26 |
|
27 a = array([5, 8, 10, 13]) |
|
28 |
|
29 We enter a multi-dimensional array this way - |
|
30 |
|
31 In []: c = array([[11,12,13], |
|
32 [21,22,23], |
|
33 [31,32,33]]) |
|
34 |
|
35 To see what c is, we just type c in the prompt. |
|
36 |
|
37 In []: c |
|
38 |
|
39 To see the dimensions of the array c, we use c.shape |
|
40 In []: c.shape |
|
41 |
|
42 Now let us look at some special methods of creating an |
|
43 array. There are various functions that allow us to create special |
|
44 arrays. |
|
45 |
|
46 The first one we shall look at is, arange. arange is similar to |
|
47 the range command, except that it returns an array and accepts |
|
48 float arguments. [is range covered?] |
|
49 |
|
50 In []: a = arange(10) |
|
51 |
|
52 In []: a |
|
53 This is the array we just created. |
|
54 |
|
55 In []: a.shape |
|
56 Note that a is one dimensional and has 10 elements, as expected. |
|
57 |
|
58 We could also use a.shape to change the shape of the array a. |
|
59 In []: a.shape = 2,5 |
|
60 Note that the total size of new array must be unchanged. |
|
61 |
|
62 We type a, to see what it looks like |
|
63 In []: a |
|
64 |
|
65 ones command can be used to get an array with all the entries as |
|
66 1s. We pass it the shape of the array that we require. |
|
67 |
|
68 In []: b = ones((3, 4)) |
|
69 |
|
70 Look at b, by printing it out. |
|
71 In []: b |
|
72 |
|
73 To create an array with all entries as ones, with a shape similar to |
|
74 an already existing array, we use the ones_like command. |
|
75 In []: b = ones_like(a) |
|
76 |
|
77 zeros and zeros_like are similar commands that can give you arrays |
|
78 with all zeros. empty and empty_like give you empty arrays (arrays |
|
79 with no initialization done.) |
|
80 |
|
81 {Do an up arrow and quickly show them a couple of examples?} |
|
82 |
|
83 The identity command can be used to obtain a square array with |
|
84 ones on the main diagonal. |
|
85 |
|
86 In []: identity(3) |
|
87 |
|
88 To obtain a 2-D array, that is not necessarily square, eye command |
|
89 can be used. Look at the documentation of eye (using eye?) for |
|
90 more info. |
|
91 |
|
92 --- |
|
93 |
|
94 Now that we have learnt how to create arrays, let move on to |
|
95 accessing elements and changing them. |
|
96 |
|
97 Let's work with the c, array which we had already created. |
|
98 |
|
99 In []: c |
|
100 |
|
101 Let's say we want to access the element 23 in c, we say |
|
102 |
|
103 In []: c[1][2] |
|
104 Note that this is similar to accessing an element inside a list of |
|
105 lists. Also, note that counting again starts from 0. |
|
106 |
|
107 But arrays provide a more convenient way to access the elements. |
|
108 In []: c[1, 2] |
|
109 |
|
110 Now, we can also change the element using a simple assignment. |
|
111 In []: c[1, 2] = -23 |
|
112 |
|
113 Let's look at accessing more than one elements at a time. We begin |
|
114 with accessing rows. |
|
115 In []: c[1] gives us the second row. (counting starts from 0) |
|
116 |
|
117 To get a column, we use a syntax that is similar to the one used |
|
118 to access a single element. |
|
119 In []: c[:,1], gives us the first column. |
|
120 |
|
121 The colon is a way to tell python to get all elements in that |
|
122 dimension from the array. |
|
123 |
|
124 So, we could use a more explicit way to access a row of a |
|
125 In []: c[1,:] |
|
126 |
|
127 We could use the colon to access specific portions of an array. |
|
128 In []: c[1,1:2] |
|
129 In []: c[1:2,1] |
|
130 In []: c[1:2,1:2] |
|
131 ... |
|
132 [Oh, by the way this is termed as slicing. :)] |
|
133 {How many examples should we show here?} |
|
134 |
|
135 --- |
|
136 |
|
137 You may have observed the similarity of the semi-colon notation to |
|
138 the range command. As expected, the semi-colon notation also |
|
139 provides a way to specify a jump. This {concept/idea} is termed as |
|
140 Striding. |
|
141 |
|
142 To get every alternate row of c, starting from the first one, we say |
|
143 In []: c[::2,:] |
|
144 |
|
145 To get every alternate row of c, starting from the second one, we |
|
146 say |
|
147 In []: c[1::2,:] |
|
148 |
|
149 |
|
150 In []: c[:,::2] |
|
151 In []: c[::2,::2] |
|
152 {How many examples should we show here?} |
|
153 |
|
154 --- |
|
155 |
|
156 We come to the end of this tutorial on arrays. In this tutorial, |
|
157 you've learnt how to create arrays and access, change elements. |
|
158 |
|
159 Thank you. |
|
160 |
|
161 *** Notes |