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