Python/C3/Getting-started-with-arrays/English-timed

From Script | Spoken-Tutorial
Revision as of 16:32, 2 January 2013 by Minal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
0:01 Hello friends and welcome to the spoken tutorial on 'Getting started with arrays'.
0:06 At the end of this tutorial, you will be able to,
  1. Create arrays using data.
  2. Create arrays from lists.
  3. Perform basic array operations.
  4. Create identity matrix.
  5. Use functions zeros(), zeros underscore like(), ones(), ones underscore like().
0:26 So Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
0:35 Arrays are homogeneous data structures.
0:39 Unlike lists, arrays cannot have heterogeneous data elements.
0:45 They can have only one type of data as their entries, be them all integers, strings, or maybe floats, but not a mix.
0:55 Arrays of a given length are comparatively much faster in mathematical operations than lists of the same length, because of the fact that they are homogeneous data structures.
1:07 Let us see how to create arrays.
1:11 Run your IPython interpreter with space hypen pylab option, to load the required modules to work with arrays.
1:21 So type ipyhton space hypen pylab
1:28 To create an array we will use the function array() as,a1 = array within brackets square brackets 1 comma 2 comma 3 comma 4
1:45 Notice that we created a one dimensional array here.
1:49 Also notice that the object we passed to create an array is a list.
1:54 Now let us see how to create a two dimensional array.
1:58 We create two dimensional array by converting a list of lists to an array as,

a2 = array within brackets within square brackets 1 comma 2 comma 3 comma 4 comma then again within square brackets 5 comma 6 comma 7 comma 8 and hit enter.

2:38 Now let us use arange() function to create the same array as before.
2:45 For that type ar = arange within bracket 1 comma 9 and hit enter,so type

print ar to get the output.

3:02 So now, As you can see, we obtained a one dimensional array with elements from 1 to 8.
3:12 Now can we make it a two dimensional array of order 2 by 4? Yes,we can.
3:20 For this we will have to use the function reshape(),
3:24 So type ar.reshape brackets 2 comma 4 and hit enter
3:33 So type ar.reshape within brackets 4 comma 2

then ar = ar dot reshape within brackets 2,4

3:55 Hence,we got our two-dimensional array.
3:58 Now, let us see how to convert a list object to an array.
4:02 We define a list,say l1
4:07 So type l1 = within square brackets 1 comma 2 comma 3 comma 4 and hit enter
4:16 Now to convert this list to an array,we use the array function as, a3 = array within brackets l1
4:30 To find the shape of an array we can use the method dot shape,
4:36 let us check the shape of the arrays we have created so far,
4:44 a2 dot shape object is a tuple, and it returned a tuple (2 comma 4).
4:52 A tuple is nothing but an ordered list of elements.
4:56 So Pause the video here, try out the following exercise and resume the video.
5:12 Find out the shape of the other arrays i.e. a1 comma a3 comma ar that we have created.
5:22 It can be done as

a1 dot shape a3 dot shape ar dot shape in the terminal

5:37 Now let us try to create a new array with a mix of elements and see what will happen,
5:45 So type a4 = array within brackets then square bracket 1 comma 2 comma 3 comma and single quote a string and hit enter
6:07 Well, we would expect an error as it has been previously mentioned that arrays handle elements with the same datatype, but it didn't raise an error.
6:16 Let us check the values in the new array created.
6:19 So Type a4 in the terminal,
6:27 Did you notice it, all the elements have been implicitly type casted as strings, though our first three elements were meant to be integers.
6:37 Also,if you have noticed,we got something like 'dtype S8' in the output.
6:44 dtype is nothing but the datatype which is the minimum type required to hold the objects in the sequence.
6:52 Let us now move on to study functions like zeros() function and ones() function.
6:59 For this ,we will have to create a matrix.
7:02 let us see how to create an identity matrix of a given size, this is a two-dimensional array in which all the diagonal elements are ones and rest of the elements are zeros.
7:13 We can create an identity matrix using the function identity().
7:18 The function identity() takes an integer argument which specifies the size of the desired matrix,
7:27 As you can see the identity function returned a three by three square matrix with all the diagonal elements as one and the rest of the elements as zeros.
7:43 So type identity within brackets 3
7:48 zeros() function accepts a tuple, which is the order of the array that we want to create, and it generates an array with all elements as zeros.
7:59 Let us create an array of the order four by five with all the elements zero.
8:06 We can do it using the method zeros(),
8:10 So type zeros within brackets then again in bracket 4 comma 5 and hit enter
8:21 Notice that we passed a tuple to the function zeros.
8:25 Pause the video here, try out the following exercise and resume the video.
8:33 So now we learnt two functions identity and zeros
8:38 Find out about the functions
8:41 zeros underscore like()
8:43 ones()
8:44 ones underscore like()
8:48 Try the following, first check the value of a1,
8:52 We see that a1 is a single dimensional array,
8:56 So type a1 and hit enter
9:01 So next now try a1 into 2
9:05 So type a1 into 2
9:09 It returned a new array with all the elements multiplied by 2.
9:13 Now let us again check the contents of a1
9:19 note that the value of a1 still remains the same.
9:23 Similarly with addition,so type a1 plus 2 and hit enter and then type a1
9:36 it returns a new array, with all the elements summed with two.
9:41 But again notice that the value of a1 has not been changed.
9:46 You may change the value of a1 by simply assigning the newly returned array as,a1 space plus=2
10:03 Notice the change in elements of a by typing 'a' so type a and hit enter
10:13 so type a1 to get the output
10:18 We can use all the mathematical operations with arrays,
10:22 Now let us try this a1 = array within brackets square brackets 1 comma 2 comma 3 comma 4

and hit enter

10:45 then a2 = array within brackets square brackets 1 comma 2 comma 3 comma 4

then type a1 + a2 and hit enter

11:07 This returns an array with element by addition
11:15 So try out a1 into a2
11:23 a1 into a2 returns an array with element by element multiplication,
11:31 And notice that it does not perform matrix multiplication.
11:37 This brings us to the end of the end of this tutorial.
11:41 In this tutorial, we have learnt to,

1. Create an array using the array() function.

11:46 2. Convert a list to an array.
11:49 3. Perform some basic operations on arrays like addition,multiplication.
11:53 4. Use functions like - .shape - arrange() - .reshape - zeros() & zeros underscore like() - ones() & ones underscore like()
12:05 Here are some self assessment questions for you to solve
12:09 1.First one, x = array within brackets square bracket 1 comma 2 comma 3 comma square bracket 5 comma 6 comma 7 is a valid statement
12:23 So IS True or False?
12:27 2. What does the ones underscore like() function do?
12:31 A. Returns an array of ones with the same shape and type as a given array.
12:37 B. Return a new array of given shape and type, filled with ones.
12:43 Now Read the statements and answer,
12:47 Only statement A is correct.
12:49 Only statement B is correct.
12:51 Both statement A and B are correct.
12:53 Both statement A and B are incorrect.
12:56 So And the answers,
12:59 1. The answer is False.
13:02 The correct way would be to assign the elements as a list of lists and then convert it to an array
13:10 Type x = array within brackets square brackets 1 comma 2 comma 3 comma square brackets 5 comma 6 comma 7
13:21 1. The function ones underscores like() returns an array of ones with the same shape and type as a given array.
13:29 Hope you have enjoyed this tutorial and found it useful.
13:31 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha