1 00:00:00,000 --> 00:00:03,970 We can create numpy arrays with more than one dimension. 2 00:00:03,970 --> 00:00:07,500 This section will focus only on 2D arrays but 3 00:00:07,500 --> 00:00:09,030 you can use numpy to build 4 00:00:09,030 --> 00:00:11,380 arrays of much higher dimensions. 5 00:00:11,380 --> 00:00:13,440 In this video, we will cover 6 00:00:13,440 --> 00:00:16,350 the basics and array creation in 2D, 7 00:00:16,350 --> 00:00:18,930 indexing and slicing in 2D, 8 00:00:18,930 --> 00:00:21,790 and basic operations in 2D. 9 00:00:21,790 --> 00:00:23,830 Consider the list a, 10 00:00:23,830 --> 00:00:27,850 the list contains three nested lists each of equal size. 11 00:00:27,850 --> 00:00:30,780 Each list is color-coded for simplicity. 12 00:00:30,780 --> 00:00:34,090 We can cast the list to a numpy array as follows. 13 00:00:34,090 --> 00:00:36,780 It is helpful to visualize the numpy array as 14 00:00:36,780 --> 00:00:38,810 a rectangular array each 15 00:00:38,810 --> 00:00:40,580 nested lists corresponds to 16 00:00:40,580 --> 00:00:42,630 a different row of the matrix. 17 00:00:42,630 --> 00:00:45,860 We can use the attribute ndim to obtain 18 00:00:45,860 --> 00:00:49,620 the number of axes or dimensions referred to as the rank. 19 00:00:49,620 --> 00:00:52,160 The term rank does not refer to the number of 20 00:00:52,160 --> 00:00:55,100 linearly independent columns like a matrix. 21 00:00:55,100 --> 00:00:56,510 It's useful to think of 22 00:00:56,510 --> 00:00:59,090 ndim as the number of nested lists. 23 00:00:59,090 --> 00:01:02,240 The first list represents the first dimension. 24 00:01:02,240 --> 00:01:04,970 This list contains another set of lists. 25 00:01:04,970 --> 00:01:08,360 This represents the second dimension or axis. 26 00:01:08,360 --> 00:01:11,420 The number of lists the list contains does not 27 00:01:11,420 --> 00:01:14,570 have to do with the dimension but the shape of the list. 28 00:01:14,570 --> 00:01:16,310 As with a 1D array, 29 00:01:16,310 --> 00:01:18,690 the attribute shape returns a tuple. 30 00:01:18,690 --> 00:01:19,820 It's helpful to use 31 00:01:19,820 --> 00:01:22,220 the rectangular representation as well. 32 00:01:22,220 --> 00:01:24,040 The first element in the tuple 33 00:01:24,040 --> 00:01:26,790 corresponds to the number of nested lists contained 34 00:01:26,790 --> 00:01:28,960 in the original list or the number of 35 00:01:28,960 --> 00:01:31,400 rows in the rectangular representation, 36 00:01:31,400 --> 00:01:33,290 in this case three. 37 00:01:33,290 --> 00:01:36,220 The second element corresponds to the size of each of 38 00:01:36,220 --> 00:01:38,230 the nested list or the number of 39 00:01:38,230 --> 00:01:40,940 columns in the rectangular array zero. 40 00:01:40,940 --> 00:01:43,330 The convention is to label this axis 41 00:01:43,330 --> 00:01:46,870 zero and this axis one as follows. 42 00:01:46,870 --> 00:01:49,360 We can also use the attribute size 43 00:01:49,360 --> 00:01:51,050 to get the size of the array. 44 00:01:51,050 --> 00:01:54,210 We see there are three rows and three columns. 45 00:01:54,210 --> 00:01:57,160 Multiplying the number of columns and rows together, 46 00:01:57,160 --> 00:01:59,340 we get the total number of elements, 47 00:01:59,340 --> 00:02:01,220 in this case nine. 48 00:02:01,220 --> 00:02:03,190 Check out the labs for arrays of 49 00:02:03,190 --> 00:02:05,480 different shapes and other attributes. 50 00:02:05,480 --> 00:02:07,730 We can use rectangular brackets to 51 00:02:07,730 --> 00:02:10,100 access the different elements of the array. 52 00:02:10,100 --> 00:02:12,920 The following image demonstrates the relationship between 53 00:02:12,920 --> 00:02:14,450 the indexing conventions for 54 00:02:14,450 --> 00:02:16,460 the lists like representation. 55 00:02:16,460 --> 00:02:18,980 The index in the first bracket corresponds to 56 00:02:18,980 --> 00:02:22,070 the different nested lists each a different color. 57 00:02:22,070 --> 00:02:24,650 The second bracket corresponds to the index 58 00:02:24,650 --> 00:02:27,470 of a particular element within the nested list. 59 00:02:27,470 --> 00:02:30,070 Using the rectangular representation, 60 00:02:30,070 --> 00:02:33,410 the first index corresponds to the row index. 61 00:02:33,410 --> 00:02:37,190 The second index corresponds to the column index. 62 00:02:37,190 --> 00:02:39,560 We could also use a single bracket 63 00:02:39,560 --> 00:02:41,720 to access the elements as follows. 64 00:02:41,720 --> 00:02:44,210 Consider the following syntax. 65 00:02:44,210 --> 00:02:47,090 This index corresponds to the second row, 66 00:02:47,090 --> 00:02:49,350 and this index the third column, 67 00:02:49,350 --> 00:02:51,630 the value is 23. 68 00:02:51,630 --> 00:02:55,460 Consider this example, this index corresponds to 69 00:02:55,460 --> 00:02:56,890 the first row and 70 00:02:56,890 --> 00:02:59,680 the second index corresponds to the first column, 71 00:02:59,680 --> 00:03:01,850 and a value of 11. 72 00:03:01,850 --> 00:03:05,020 We can also use slicing in numpy arrays. 73 00:03:05,020 --> 00:03:08,080 The first index corresponds to the first row. 74 00:03:08,080 --> 00:03:11,900 The second index accesses the first two columns. 75 00:03:11,900 --> 00:03:13,890 Consider this example, 76 00:03:13,890 --> 00:03:17,330 the first index corresponds to the first two rows. 77 00:03:17,330 --> 00:03:20,590 The second index accesses the last column. 78 00:03:20,590 --> 00:03:22,770 We can also add arrays, 79 00:03:22,770 --> 00:03:25,670 the process is identical to matrix addition. 80 00:03:25,670 --> 00:03:27,480 Consider the matrix X, 81 00:03:27,480 --> 00:03:29,520 each element is colored differently. 82 00:03:29,520 --> 00:03:31,260 Consider the matrix Y. 83 00:03:31,260 --> 00:03:34,050 Similarly, each element is colored differently. 84 00:03:34,050 --> 00:03:35,910 We can add the matrices. 85 00:03:35,910 --> 00:03:37,580 This corresponds to adding 86 00:03:37,580 --> 00:03:39,210 the elements in the same position, 87 00:03:39,210 --> 00:03:41,330 i.e adding elements contained 88 00:03:41,330 --> 00:03:43,380 in the same color boxes together. 89 00:03:43,380 --> 00:03:45,500 The result is a new matrix that has 90 00:03:45,500 --> 00:03:48,060 the same size as matrix Y or X. 91 00:03:48,060 --> 00:03:50,540 Each element in this new matrix is the sum of 92 00:03:50,540 --> 00:03:53,160 the corresponding elements in X and Y. 93 00:03:53,160 --> 00:03:55,370 To add two arrays in numpy, 94 00:03:55,370 --> 00:03:57,710 we define the array in this case X. 95 00:03:57,710 --> 00:04:01,950 Then we define the second array Y, we add the arrays. 96 00:04:01,950 --> 00:04:04,700 The result is identical to matrix addition. 97 00:04:04,700 --> 00:04:06,740 Multiplying a numpy array by 98 00:04:06,740 --> 00:04:08,450 a scalar is identical to 99 00:04:08,450 --> 00:04:10,580 multiplying a matrix by a scalar. 100 00:04:10,580 --> 00:04:12,440 Consider the matrix Y. 101 00:04:12,440 --> 00:04:15,560 If we multiply the matrix by this scalar two, 102 00:04:15,560 --> 00:04:18,830 we simply multiply every element in the matrix by two. 103 00:04:18,830 --> 00:04:20,570 The result is a new matrix of 104 00:04:20,570 --> 00:04:23,750 the same size where each element is multiplied by two. 105 00:04:23,750 --> 00:04:25,570 Consider the array Y. 106 00:04:25,570 --> 00:04:27,280 We first define the array, 107 00:04:27,280 --> 00:04:29,390 we multiply the array by a scalar as 108 00:04:29,390 --> 00:04:32,030 follows and assign it to the variable Z. 109 00:04:32,030 --> 00:04:33,680 The result is a new array 110 00:04:33,680 --> 00:04:36,190 where each element is multiplied by two. 111 00:04:36,190 --> 00:04:38,930 Multiplication of two arrays corresponds to 112 00:04:38,930 --> 00:04:42,110 an element-wise product, or Hadamard product. 113 00:04:42,110 --> 00:04:44,210 Consider array X and array 114 00:04:44,210 --> 00:04:47,840 Y. Hadamard product corresponds to multiplying each 115 00:04:47,840 --> 00:04:50,200 of the elements in the same position i.e 116 00:04:50,200 --> 00:04:51,800 multiplying elements contained in 117 00:04:51,800 --> 00:04:53,790 the same color boxes together. 118 00:04:53,790 --> 00:04:55,940 The result is a new matrix that is 119 00:04:55,940 --> 00:04:58,530 the same size as matrix Y or X. 120 00:04:58,530 --> 00:05:01,130 Each element in this new matrix is 121 00:05:01,130 --> 00:05:04,330 the product of the corresponding elements in X and Y. 122 00:05:04,330 --> 00:05:06,910 Consider the array X and Y. 123 00:05:06,910 --> 00:05:08,390 We can find the product of 124 00:05:08,390 --> 00:05:10,990 two arrays X and Y in one line, 125 00:05:10,990 --> 00:05:13,780 and assign it to the variable Z as follows. 126 00:05:13,780 --> 00:05:16,860 The result is identical to Hadamard product. 127 00:05:16,860 --> 00:05:18,290 We can also perform 128 00:05:18,290 --> 00:05:20,930 matrix multiplication with Numpy arrays. 129 00:05:20,930 --> 00:05:23,270 Matrix multiplication is a little more 130 00:05:23,270 --> 00:05:26,240 complex but let's provide a basic overview. 131 00:05:26,240 --> 00:05:28,130 Consider the matrix A 132 00:05:28,130 --> 00:05:30,310 where each row is a different color. 133 00:05:30,310 --> 00:05:33,060 Also, consider the matrix B 134 00:05:33,060 --> 00:05:35,300 where each column is a different color. 135 00:05:35,300 --> 00:05:37,220 In linear algebra, before we 136 00:05:37,220 --> 00:05:39,770 multiply matrix A by matrix B, 137 00:05:39,770 --> 00:05:41,390 we must make sure that the number of 138 00:05:41,390 --> 00:05:43,190 columns in matrix A in 139 00:05:43,190 --> 00:05:44,700 this case three is 140 00:05:44,700 --> 00:05:47,330 equal to the number of rows in matrix B, 141 00:05:47,330 --> 00:05:48,910 in this case three. 142 00:05:48,910 --> 00:05:51,170 From matrix multiplication, to obtain 143 00:05:51,170 --> 00:05:54,860 the ith row and jth column of the new matrix, 144 00:05:54,860 --> 00:05:57,330 we take the dot product of the ith row 145 00:05:57,330 --> 00:06:00,580 of a with the jth columns of B. 146 00:06:00,580 --> 00:06:02,140 For the first column, 147 00:06:02,140 --> 00:06:04,800 first row we take the dot product of 148 00:06:04,800 --> 00:06:08,890 the first row of A with the first column of B as follows. 149 00:06:08,890 --> 00:06:10,980 The result is zero. 150 00:06:10,980 --> 00:06:12,500 For the first row and the 151 00:06:12,500 --> 00:06:14,330 second column of the new matrix, 152 00:06:14,330 --> 00:06:18,080 we take the dot product of the first row of the matrix A, 153 00:06:18,080 --> 00:06:21,020 but this time we use the second column of matrix B, 154 00:06:21,020 --> 00:06:22,770 the result is two. 155 00:06:22,770 --> 00:06:24,110 For the second row and 156 00:06:24,110 --> 00:06:25,920 the first column of the new matrix, 157 00:06:25,920 --> 00:06:27,530 we take the dot product of 158 00:06:27,530 --> 00:06:29,550 the second row of the matrix A. 159 00:06:29,550 --> 00:06:32,070 With the first column of matrix B, 160 00:06:32,070 --> 00:06:33,550 the result is zero. 161 00:06:33,550 --> 00:06:35,660 Finally, for the second row 162 00:06:35,660 --> 00:06:37,620 and the second column of the new matrix, 163 00:06:37,620 --> 00:06:40,040 we take the dot product of the second row of 164 00:06:40,040 --> 00:06:43,290 the matrix A with the second column of matrix B, 165 00:06:43,290 --> 00:06:44,920 the result is two. 166 00:06:44,920 --> 00:06:48,890 In numpy, we can define the numpy arrays A and B. 167 00:06:48,890 --> 00:06:51,410 We can perform matrix multiplication and 168 00:06:51,410 --> 00:06:54,080 assign it to array C. The result is 169 00:06:54,080 --> 00:06:56,540 the array C. It corresponds to 170 00:06:56,540 --> 00:07:00,120 the matrix multiplication of array A and B. 171 00:07:00,120 --> 00:07:03,660 There is a lot more you can do with it in numpy. 172 00:07:03,660 --> 00:07:05,850 Checkout numpy.org. 173 00:07:05,850 --> 00:07:08,550 Thanks for watching this video. 174 00:07:08,550 --> 00:07:13,000 (Music)