author | Shantanu <shantanu@fossee.in> |
Thu, 15 Apr 2010 11:31:32 +0530 | |
changeset 61 | 38ef280b1408 |
parent 56 | 86c862b3dbef |
child 67 | 806cca3b7231 |
permissions | -rw-r--r-- |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
1 |
Hello and welcome to the tutorial on Matrices. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
2 |
All matrices operations are done using arrays. |
61 | 3 |
We have already seen in previous session that how arrays are better suited for particular mathematical operations. In this session we are going to cover more details on Arrays(matrices), how to create/initialize them, how to manipulate and use them for solving given problem. |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
4 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
5 |
First thing first, we start with creating a normal array by: |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
6 |
a (equal to)= array([5, 8, 10, 13]) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
7 |
|
61 | 8 |
and we have a as array, check the value by |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
9 |
a |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
10 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
11 |
Here a is single dimension array, that is it has only one row. We can create multi-dimensional arrays by |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
12 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
13 |
c = array([[11,12,13], [21,22,23], [31,32,33]]) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
14 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
15 |
both c and a are arrays but with different dimensions or shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
16 |
we can check shape of arrays by using shape attribute of arrays. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
17 |
a.shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
18 |
c.shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
19 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
20 |
some other handy array initialization methods are also available to make life easier. |
61 | 21 |
say we want to create an array of size 3x4 with all the values initialized to be 1, we can use |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
22 |
b = ones((3, 4)) |
61 | 23 |
and b will be |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
24 |
b |
61 | 25 |
similarly, if we already have an array, and we want to create one more array with the same shape and initial values to be one, for that we will use |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
26 |
d = ones_like(c) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
27 |
and d will be 3x3 array with all values 1 |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
28 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
29 |
Similarly there are functions like zeros and zeros_like which initialize array with all values being 0. One more useful function available is 'identity', it create identity matrix(array) of given order |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
30 |
i = identity(3) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
31 |
i |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
32 |
i = identity(5) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
33 |
i |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
34 |
|
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
35 |
---------------- |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
36 |
Now that we have covered creation of arrays, we will see how to access and change values of particular elements. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
37 |
We created one 3x3 matrix earlier, |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
38 |
c |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
39 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
40 |
to access the element 23 we type |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
41 |
c[1][2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
42 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
43 |
It is second row third column of c. Note that index values of arrays also start from 0. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
44 |
Alternative and popular way of doing this is |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
45 |
c[1, 2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
46 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
47 |
here ',' is used as separator for row and column value. Similarly any value from the array can be accessed. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
48 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
49 |
to access particular row completely we simply skip the column value |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
50 |
c[1] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
51 |
will give us the entire second row. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
52 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
53 |
The way by which we access one element of array, we use that itself to assign new value |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
54 |
c[1, 1] = -22 |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
55 |
c |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
56 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
57 |
and same thing can be used for entire row |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
58 |
c[1] = 0 |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
59 |
c |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
60 |
as we can see, second row all elements are now 0 |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
61 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
62 |
Accessing a row is straight forward we skip column part |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
63 |
but to access whole column we have to use |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
64 |
c[:,2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
65 |
will return third column. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
66 |
here the ':' part mentioned for row value symbolises entire row. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
67 |
the c[1] we were using earlier can also be written as c[1,:] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
68 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
69 |
':' actually takes two value. for any row or column we can mention |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
70 |
start:end values, and rows/columns starting for 'start' till 'end' will be returned. Lets try some examples for better understanding |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
71 |
c[0:2,:] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
72 |
will result in rows starting from first(0) till second and all columns. Note here that 'end' in our case, '2' would not be included in resulting array. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
73 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
74 |
c[1:3,:] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
75 |
gives second and third row. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
76 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
77 |
similarly we can try this on columns also: |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
78 |
c[:, 0:2] gives us first two column |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
79 |
This whole concept of accessing chunks of arrays is known as 'slicing' |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
80 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
81 |
There is one more interesting and handy feature of slicing. We saw earlier that how only ':' means entire row or column. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
82 |
It actually means if we don't specify start and end part of slice default is from zero to end. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
83 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
84 |
so |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
85 |
c[:, :2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
86 |
will also give us first two columns |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
87 |
and c[:, 1:] will return last columns. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
88 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
89 |
c[1:, :2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
90 |
returns first two columns of last two rows |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
91 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
92 |
Now we will look into one more powerful feature of arrays: 'striding'. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
93 |
Striding allows us to jump or skip rows or columns by certain interval. We can specify the step size. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
94 |
c[:,:] will give us entire array |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
95 |
we add one more ':' to row or column part to specify a step size. |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
96 |
c[:, ::2] |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
97 |
will give us first and third column. Since step size is two, it start with first column(blank before : means 0) and then we jump one column and then third(blank after : means end) |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
98 |
similarly |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
99 |
c[::2,:] returns 2x3 array with first and third row |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
100 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
101 |
and c[::2, ::2] will give us 2x2 array with first and third row and column |
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
102 |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
103 |
Lets us try to use these concepts of slicing and striding for doing some basic image manipulation |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
104 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
105 |
pylab has a function imread to read images. We will use '(in)famous' lena image for our experimentation. Its there on desktop. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
106 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
107 |
a = imread('lena.png') |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
108 |
a is a numpy array with the 'RGB' values of each pixel |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
109 |
a.shape |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
110 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
111 |
its a 512x512x3 array. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
112 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
113 |
to view the image write |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
114 |
imshow(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
115 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
116 |
lets try to crop the image to top left quarter. Since a is a normal array we can use slicing to get top left quarter by |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
117 |
imshow(a[:255,:255]) (half of 512 is 256) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
118 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
119 |
But hat is not 'interesting' part of lena. Lets crop the image so that only her face is visible. for that we will need some rough estimates of pixels. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
120 |
imshow(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
121 |
now move your mouse cursor over the image, it will give us x, y coordinates where ever we take our cursor. We can get rough estimate of lena's face now cropping to those boundaries is simple |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
122 |
imshow(a[200:400, 200:400]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
123 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
124 |
Next we will try striding on this image. We will resize the image by skipping each alternate pixel. We have already seen how to skip alternate elements so, |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
125 |
imshow(a[::2, ::2]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
126 |
note now the size of image is just 256x256 and still quality of image is not much compromised. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
127 |
------------------------- |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
128 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
129 |
Till now we have covered initializing and accessing elements of arrays. Now we shall concentrate on functions available for arrays. We start this by creating 4x4 array by |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
130 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
131 |
a = array([[ 1, 1, 2, -1],[ 2, 5, -1, -9], [ 2, 1, -1, 3], [ 1, -3, 2, 7]]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
132 |
a |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
133 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
134 |
To get transpose of this matrix write |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
135 |
a.T |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
136 |
sum() function returns sum of all the elements of a matrix. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
137 |
sum(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
138 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
139 |
lets create one more array for checking more operations |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
140 |
b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
141 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
142 |
+ will take care of matrix additions |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
143 |
a + b |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
144 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
145 |
lets try multiplication now, |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
146 |
a * b will return element wise product of two matrices. |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
147 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
148 |
To get matrix product of a and b we use |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
149 |
dot(a, b) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
150 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
151 |
and to get inverse of matrix |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
152 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
153 |
inv(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
154 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
155 |
det(a) returns determinant of matrix a |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
156 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
157 |
we shall create one array e |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
158 |
e = array([[3,2,4],[2,0,2],[4,2,3]]) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
159 |
and then to evaluate eigenvalues of array |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
160 |
eig(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
161 |
it returns both eigen values and eigen vector of given matrix |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
162 |
to get only eigen values use |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
163 |
eigvals(a) |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
164 |
|
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
165 |
This brings us to end of this session. We have covered Matrices |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
166 |
Initialization |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
167 |
Slicing |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
168 |
Striding |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
169 |
A bit of image processing |
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
170 |
Functions available for arrays |
61 | 171 |
|
56
86c862b3dbef
First cut for arrays and matrices.
Shantanu <shantanu@fossee.in>
parents:
55
diff
changeset
|
172 |
Thank you |
55
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
173 |
|
1fe734b20950
Added slicing and striding part.
Shantanu <shantanu@fossee.in>
parents:
54
diff
changeset
|
174 |
---------------- |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
175 |
We have seen |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
176 |
Welcome to the Tutorial on arrays. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
177 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
178 |
As mentioned in [the previous tutorial] arrays are much faster and |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
179 |
more efficient. In this tutorial we shall look at creating arrays, |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
180 |
accessing elements and changing them. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
181 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
182 |
--- |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
183 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
184 |
Let's start with creating simple arrays. We've already seen how to |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
185 |
convert lists to arrays. Inputting a new array is similar to that. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
186 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
187 |
In []: |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
188 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
189 |
Type /a/, to see what it is. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
190 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
191 |
In []: a |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
192 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
193 |
We enter a multi-dimensional array this way - |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
194 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
195 |
In []: c = array([[11,12,13], |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
196 |
[21,22,23], |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
197 |
[31,32,33]]) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
198 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
199 |
To see what c is, we just type c in the prompt. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
200 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
201 |
In []: c |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
202 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
203 |
To see the dimensions of the array c, we use c.shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
204 |
In []: c.shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
205 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
206 |
Now let us look at some special methods of creating an |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
207 |
array. There are various functions that allow us to create special |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
208 |
arrays. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
209 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
210 |
The first one we shall look at is, /arange/. /arange/ is similar to |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
211 |
the range command, except that it returns an array and accepts |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
212 |
float arguments. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
213 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
214 |
In []: a = arange(10) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
215 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
216 |
In []: a |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
217 |
This is the array we just created. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
218 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
219 |
In []: a.shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
220 |
Note that /a/ is one dimensional and has 10 elements, as expected. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
221 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
222 |
We could also use a.shape to change the shape of the array a. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
223 |
In []: a.shape = 2,5 |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
224 |
Note that the total size of new array must be unchanged. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
225 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
226 |
We type a, to see what it looks like |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
227 |
In []: a |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
228 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
229 |
ones command can be used to get an array with all the entries as |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
230 |
ones. We pass it the shape of the array that we require. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
231 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
232 |
In []: |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
233 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
234 |
Look at b, by printing it out. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
235 |
In []: b |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
236 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
237 |
To create an array with all entries as ones, with it's shape |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
238 |
similar to an already existing array, we use the ones_like |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
239 |
command. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
240 |
In []: b = ones_like(a) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
241 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
242 |
zeros and zeros_like are similar commands that can give you arrays |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
243 |
with all zeros. empty and empty_like give you empty arrays (arrays |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
244 |
with no initialization done.) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
245 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
246 |
In []: b = zeros((3, 4)) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
247 |
In []: b = zeros_like(a) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
248 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
249 |
The identity command can be used to obtain a square array with |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
250 |
ones on the main diagonal. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
251 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
252 |
In []: identity(3) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
253 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
254 |
To obtain a 2-D array, that is not necessarily square, eye command |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
255 |
can be used. Look at the documentation of eye (using eye?) for |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
256 |
more info. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
257 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
258 |
--- |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
259 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
260 |
Now that we have learnt how to create arrays, let's move on to |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
261 |
accessing elements and changing them. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
262 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
263 |
Let's work with the c, array which we had already created. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
264 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
265 |
In []: c |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
266 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
267 |
Let's say we want to access the element 23 in c, we say |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
268 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
269 |
In []: c[1][2] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
270 |
Note that this is similar to accessing an element inside a list of |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
271 |
lists. Also, note that counting again starts from 0. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
272 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
273 |
But arrays provide a more convenient way to access the elements. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
274 |
In []: c[1, 2] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
275 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
276 |
Now, we can also change the element using a simple assignment. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
277 |
In []: c[1, 2] = -23 |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
278 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
279 |
Let's look at accessing more than one elements at a time. We begin |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
280 |
with accessing rows. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
281 |
In []: c[1] gives us the second row. (counting starts from 0) |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
282 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
283 |
To get a column, we use a syntax that is similar to the one used |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
284 |
to access a single element. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
285 |
In []: c[:,1], gives us the first column. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
286 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
287 |
The colon specifies that we wish to obtain all elements in that |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
288 |
dimension from the array. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
289 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
290 |
So, we could use a more explicit way to access the second row of |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
291 |
the array. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
292 |
In []: c[1,:] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
293 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
294 |
The colon can be used to access specific portions of the array, |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
295 |
similar to the way we do with lists. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
296 |
In []: c[1,1:3] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
297 |
Observe that we get the second and third columns from the second |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
298 |
row. As with lists, the number after the colon is excluded when |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
299 |
slicing a portion of the array. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
300 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
301 |
In []: c[1:3,1] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
302 |
Now, we get the second and third rows from the first column. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
303 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
304 |
In []: c[1:3,1:3] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
305 |
We get the second and third rows and the second and third |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
306 |
columns. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
307 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
308 |
The numbers before and after the colons are optional. If the |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
309 |
number before the colon is omitted, it is assumed to be zero by |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
310 |
default. If the element after the colon is omitted, it is assumed |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
311 |
to be until the end. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
312 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
313 |
In []: c[1:, 1:] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
314 |
This is essentially similar to the previous example. We are using |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
315 |
the default value i.e, the end, instead of specifying 3, |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
316 |
explicitly. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
317 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
318 |
In []: c[:2, :2] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
319 |
We have omitted specifying the zero before the colon, explicitly. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
320 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
321 |
--- |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
322 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
323 |
You may have observed the similarity of the semi-colon notation to |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
324 |
the notation used in lists. As expected, the semi-colon notation |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
325 |
also provides a way to specify a jump. This {concept/idea} is |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
326 |
termed as Striding. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
327 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
328 |
To get every alternate row of c, starting from the first one, we say |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
329 |
In []: c[::2,:] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
330 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
331 |
To get every alternate row of c, starting from the second one, we |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
332 |
say |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
333 |
In []: c[1::2,:] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
334 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
335 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
336 |
In []: c[:,::2] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
337 |
In []: c[::2,::2] |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
338 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
339 |
--- |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
340 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
341 |
We come to the end of this tutorial on arrays. In this tutorial, |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
342 |
you've learnt how to create arrays and access, change elements. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
343 |
|
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
344 |
Thank you. |
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
345 |
|
40
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
346 |
Hello friends and welcome to the second tutorial in the series of spoken tutorials on Python for Scientific computing. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
347 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
348 |
In the previous tutorial we learnt about arrays and we told you that numpy arrays are faster and more efficient . In this tutorial we shall look at creating arrays, accessing elements and changing them. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
349 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
350 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
351 |
Let's start with creating simple arrays. We've already seen how to convert lists to arrays. Inputting a new array is similarto that. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
352 |
|
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
353 |
On your Ipython terminal type a = array open parenthesis and then open square brackets 5,8,10,13 close square brackets and close parenthesis . This create an array a . You can see what a is by typing a on the terminal . |
40
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
354 |
Now we will try to create a multi-dimensional array type in your ipython terminal |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
355 |
c= array open parenthesis , then open square brackets 11,12,13 close square bracket 'comma' start square bracket 21 , 22 ,23close square bracket 'comma' open 31,32,33 close square bracket another close square bracket which closes the first sqaure bracket and parenthesis which closes the first parenthesis . Now to see the dimensions of the array c we will do c.shape . We can see that c is a 3 by 3 matrix . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
356 |
|
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
357 |
There are other special methods of creating arrays as well we will now look at them . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
358 |
The first one is the command arange which is similar to range except that it returns an array. |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
359 |
We will type on our Ipython interpreter a = arange(10). We will see what a is now . Type a . As we can see This returns us an array of one dimension and has 10 elements . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
360 |
Ones can be use to get all entries as ones . We can pass it the shape of the array as required . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
361 |
type b=ones open parenthesis , another open parenthesis , 3,4 , close second parenthesis and close first parenthesis . Look at b , by printing it out . |
6e3526275a00
Adding arrays.txt file , script for arrays tutorial , session 4
amit@thunder
parents:
diff
changeset
|
362 |
To create an array with all entries as ones, with it's shape similar to an already existing array, we use the ones_like |
54
46a3575919f5
Work on session 4, Arrays started.
Shantanu <shantanu@fossee.in>
parents:
40
diff
changeset
|
363 |
command. type b= ones_like in parenthesis a . |