55 In []: a.shape |
55 In []: a.shape |
56 Note that a is one dimensional and has 10 elements, as expected. |
56 Note that a is one dimensional and has 10 elements, as expected. |
57 |
57 |
58 We could also use a.shape to change the shape of the array a. |
58 We could also use a.shape to change the shape of the array a. |
59 In []: a.shape = 2,5 |
59 In []: a.shape = 2,5 |
60 Note that the total size of new array must be unchanged. |
60 Note that the total size(number of elements) of new array must |
|
61 be unchanged. |
61 |
62 |
62 We type a, to see what it looks like |
63 We check re-shaped 'a' by |
63 In []: a |
64 In []: a |
64 |
65 |
65 ones command can be used to get an array with all the entries as |
66 'ones' function 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 1s. We pass it the shape of the required array. For ex. |
67 |
68 |
68 In []: b = ones((3, 4)) |
69 In []: b = ones((3, 4)) |
69 |
70 |
70 Look at b, by printing it out. |
71 b is 3(cross)4 array with all 1s |
71 In []: b |
72 In []: b |
72 |
73 |
73 To create an array with all entries as ones, with a shape similar to |
74 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 an already existing array, we use the ones_like function. |
75 In []: b = ones_like(a) |
76 In []: b = ones_like(a) |
76 |
77 |
77 zeros and zeros_like are similar commands that can give you arrays |
78 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 all zeros. empty and empty_like give you empty arrays (arrays |
79 with no initialization done.) |
80 with no initialization done.) |
89 can be used. Look at the documentation of eye (using eye?) for |
90 can be used. Look at the documentation of eye (using eye?) for |
90 more info. |
91 more info. |
91 |
92 |
92 --- |
93 --- |
93 |
94 |
94 Now that we have learnt how to create arrays, let move on to |
95 Now that we have learnt how to create arrays, lets move on to |
95 accessing elements and changing them. |
96 accessing elements and changing them. |
96 |
97 |
97 Let's work with the c, array which we had already created. |
98 Let's work with the c, array which we had already created. |
98 |
99 |
99 In []: c |
100 In []: c |
100 |
101 |
101 Let's say we want to access the element 23 in c, we say |
102 Let's say we want to access the element 23 in c(second row |
|
103 third column), we say |
102 |
104 |
103 In []: c[1][2] |
105 In []: c[1][2] |
104 Note that this is similar to accessing an element inside a list of |
106 Note that this is similar to accessing an element inside a list of |
105 lists. Also, note that counting again starts from 0. |
107 lists. Also, note that counting again starts from 0. |
106 |
108 |
107 But arrays provide a more convenient way to access the elements. |
109 Additionally arrays provide a more convenient way to access the |
|
110 elements. |
108 In []: c[1, 2] |
111 In []: c[1, 2] |
109 |
112 |
110 Now, we can also change the element using a simple assignment. |
113 Now, we can also change the element using a simple assignment. |
111 In []: c[1, 2] = -23 |
114 In []: c[1, 2] = -23 |
112 |
115 |
124 So, we could use a more explicit way to access a row of a |
127 So, we could use a more explicit way to access a row of a |
125 In []: c[1,:] |
128 In []: c[1,:] |
126 |
129 |
127 We could use the colon to access specific portions of an array. |
130 We could use the colon to access specific portions of an array. |
128 In []: c[1,1:2] |
131 In []: c[1,1:2] |
|
132 second column, from second row(1) till third(2) and excluding it |
129 In []: c[1:2,1] |
133 In []: c[1:2,1] |
130 In []: c[1:2,1:2] |
134 In []: c[1:2,1:2] |
131 ... |
135 ... |
132 [Oh, by the way this is termed as slicing. :)] |
136 [Oh, by the way this is termed as slicing. :)] |
133 {How many examples should we show here?} |
137 {How many examples should we show here?} |