From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Hello friends and welcome to the spoken tutorial on 'Getting started with arrays'.
00:06 At the end of this tutorial, you will be able to,

Create arrays using data. Create arrays from lists. Perform basic array operations. Create identity matrix. Use functions zeros(), zeros underscore like(), ones(), ones underscore like().

00:26 So Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
00:35 Arrays are homogeneous data structures.
00:39 Unlike lists, arrays cannot have heterogeneous data elements.
00:45 They can have only one type of data as their entries, be them all integers, strings, or maybe floats, but not a mix.
00: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.
01:07 Let us see how to create arrays.
01:11 Run your IPython interpreter with space hypen pylab option, to load the required modules to work with arrays.
01:21 So type ipython space hypen pylab
01: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
01:45 Notice that we created a one dimensional array here.
01:49 Also notice that the object we passed to create an array is a list.
01:54 Now let us see how to create a two dimensional array.
01: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.

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

print ar to get the output.

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

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

05:37 Now let us try to create a new array with a mix of elements and see what will happen,
05:45 So type a4 = array within brackets then square bracket 1 comma 2 comma 3 comma and single quote a string and hit enter
06:07 Well, we would expect an error as it has been previously mentioned that arrays handle elements with the same data type, but it didn't raise an error.
06:16 Let us check the values in the new array created.
06:19 So Type a4 in the terminal,
06: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.
06:37 Also,if you have noticed,we got something like 'dtype S8' in the output.
06:44 dtype is nothing but the data type which is the minimum type required to hold the objects in the sequence.
06:52 Let us now move on to study functions like zeros() function and ones() function.
06:59 For this ,we will have to create a matrix.
07: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.
07:13 We can create an identity matrix using the function identity() .
07:18 The function identity() takes an integer argument which specifies the size of the desired matrix,
07: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.
07:43 So type identity within brackets 3
07: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.
07:59 Let us create an array of the order four by five with all the elements zero.
08:06 We can do it using the method zeros(),
08:10 So type zeros within brackets then again in bracket 4 comma 5 and hit enter
08:21 Notice that we passed a tuple to the function zeros.
08:25 Pause the video here, try out the following exercise and resume the video.
08:33 So now we learnt two functions identity and zeros
08:38 Find out about the functions
08:41 zeros underscore like()
08:43 ones(), ones underscore like()
08:48 Try the following, first check the value of a1,
08:52 We see that a1 is a single dimensional array,
08:56 So type a1 and hit enter
09:01 So next now try a1 into 2
09:05 So type a1 into 2
09:09 It returned a new array with all the elements multiplied by 2.
09:13 Now let us again check the contents of a1
09:19 note that the value of a1 still remains the same.
09:23 Similarly with addition,so type a1 plus 2 and hit enter and then type a1
09:36 it returns a new array, with all the elements summed with two.
09:41 But again notice that the value of a1 has not been changed.
09: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 = arrays 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, Create an array using the array() function.
11:46 Convert a list to an array.
11:49 Perform some basic operations on arrays like addition,multiplication.
11:53 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 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 What does the ones underscore like() function do?
12:31 Returns an array of ones with the same shape and type as a given array.
12:37 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 let's find the answers,
12:59 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 within brackets 5 comma 6 comma 7
13:21 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