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().

3 00:00:26 --> 00:00:34 So Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists". 4 00:00:35 --> 00:00:38 Arrays are homogeneous data structures. 5 00:00:39 --> 00:00:44 Unlike lists, arrays cannot have heterogeneous data elements. 6 00:00:45 --> 00:00:54 They can have only one type of data as their entries, be them all integers, strings, or maybe floats, but not a mix. 7 00:00:55 --> 00:01:06 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. 8 00:01:07 --> 00:01:10 Let us see how to create arrays. 9 00:01:11 --> 00:01:20 Run your IPython interpreter with space hypen pylab option, to load the required modules to work with arrays. 10 00:01:21 --> 00:01:27 So type ipython space hypen pylab 11 00:01:28 --> 00:01:44 To create an array we will use the function array() as,a1 = array within brackets square brackets 1 comma 2 comma 3 comma 4 12 00:01:45 --> 00:01:48 Notice that we created a one dimensional array here. 13 00:01:49 --> 00:01:53 Also notice that the object we passed to create an array is a list. 14 00:01:54 --> 00:01:57 Now let us see how to create a two dimensional array. 15 00:01:58 --> 00:02:37 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.

16 00:02:38 --> 00:02:44 Now let us use arrange() function to create the same array as before. 17 00:02:45 --> 00:03:01 For that type ar = arrange within bracket 1 comma 9 and hit enter,so typeprint ar to get the output.

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

35 00:05:37 --> 00:05:44 Now let us try to create a new array with a mix of elements and see what will happen, 36 00:05:45 --> 00:06:06 So type a4 = array within brackets then square bracket 1 comma 2 comma 3 comma and single quote a string and hit enter 37 00:06:07 --> 00:06:15 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. 38 00:06:16 --> 00:06:18 Let us check the values in the new array created. 39 00:06:19 --> 00:06:26 So Type a4 in the terminal, 40 00:06:27 --> 00:06:36 Did you notice it, all the elements have been implicitly type casted as strings, though our first three elements were meant to be integers. 41 00:06:37 --> 00:06:43 Also,if you have noticed,we got something like 'dtype S8' in the output. 42 00:06:44 --> 00:06:51 dtype is nothing but the data type which is the minimum type required to hold the objects in the sequence. 43 00:06:52 --> 00:06:58 Let us now move on to study functions like zeros() function and ones() function. 44 00:06:59 --> 00:07:01 For this ,we will have to create a matrix. 45 00:07:02 --> 00:07:12 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. 46 00:07:13 --> 00:07:17 We can create an identity matrix using the function identity() . 47 00:07:18 --> 00:07:26 The function identity() takes an integer argument which specifies the size of the desired matrix, 48 00:07:27 --> 00:07:42 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. 49 00:07:43 --> 00:07:47 So type identity within brackets 3 50 00:07:48 --> 00:07:58 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. 51 00:07:59 --> 00:08:05 Let us create an array of the order four by five with all the elements zero. 52 00:08:06 --> 00:08:09 We can do it using the method zeros(), 53 00:08:10 --> 00:08:20 So type zeros within brackets then again in bracket 4 comma 5 and hit enter 54 00:08:21 --> 00:08:24 Notice that we passed a tuple to the function zeros. 55 00:08:25 --> 00:08:32 Pause the video here, try out the following exercise and resume the video. 56 00:08:33 --> 00:08:37 So now we learnt two functions identity and zeros 57 00:08:38 --> 00:08:40and hit enter

76 00:10:45 --> 00:11:06 then a2 = array within brackets square brackets 1 comma 2 comma 3 comma 4 then type a1 + a2 and hit enter 77 00:11:07 --> 00:11:14 This returns an array with element by addition 78 00:11:15 --> 00:11:22 So try out a1 into a2 79 00:11:23 --> 00:11:30 a1 into a2 returns an array with element by element multiplication, 80 00:11:31 --> 00:11:36 And notice that it does not perform matrix multiplication. 81 00:11:37 --> 00:11:40 This brings us to the end of the end of this tutorial. 82 00:11:41 --> 00:11:45 In this tutorial, we have learnt to, Create an array using the array() function. 83 00:11:46 --> 00:11:48 Convert a list to an array. 84 00:11:49 --> 00:11:52 Perform some basic operations on arrays like addition,multiplication. 85 00:11:53 --> 00:12:04 Use functions like - .shape - arrange() - .reshape - zeros() & zeros underscore like() - ones() & ones underscore like() 86 00:12:05 --> 00:12:08 Here are some self assessment questions for you to solve 87 00:12:09 --> 00:12:22 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 88 00:12:23 --> 00:12:26 So IS True or False? 89 00:12:27 --> 00:12:30 What does the ones underscore like() function do? 90 00:12:31 --> 00:12:36 Returns an array of ones with the same shape and type as a given array. 91 00:12:37 --> 00:12:42 Return a new array of given shape and type, filled with ones. 92 00:12:43 --> 00:12:46 Now Read the statements and answer, 93 00:12:47 --> 00:12:48 Only statement A is correct. 94 00:12:49 --> 00:12:50 Only statement B is correct. 95 00:12:51 --> 00:12:52 Both statement A and B are correct. 96 00:12:53 --> 00:12:55 Both statement A and B are incorrect. 97 00:12:56 --> 00:12:58 So let's find the answers, 98 00:12:59 --> 00:13:01 The answer is False. 99 00:13:02 --> 00:13:09 The correct way would be to assign the elements as a list of lists and then convert it to an array 100 00:13:10 --> 00:13:20 Type x = array within brackets square brackets 1 comma 2 comma 3 comma within brackets 5 comma 6 comma 7 101 00:13:21 --> 00:13:28 The function ones underscores like() returns an array of ones with the same shape and type as a given array. 102 00:13:29 --> 00:13:30 Hope you have enjoyed this tutorial and found it useful. 103 00:13:31 --> 00:13:36 Thank you!