1 00:00:00,180 --> 00:00:02,402 When developing learning algorithms, very 2 00:00:02,402 --> 00:00:04,066 often a few simple plots 3 00:00:04,066 --> 00:00:05,279 can give you a better 4 00:00:05,279 --> 00:00:06,593 sense of what the algorithm 5 00:00:06,593 --> 00:00:08,423 is doing and just sanity check 6 00:00:08,423 --> 00:00:09,503 that everything is going okay 7 00:00:09,510 --> 00:00:12,405 and the algorithms doing what is supposed to. 8 00:00:12,410 --> 00:00:13,924 For example, in an earlier 9 00:00:13,924 --> 00:00:15,244 video, I talked about how 10 00:00:15,244 --> 00:00:16,826 plotting the cost function J 11 00:00:16,826 --> 00:00:18,433 of theta can help you 12 00:00:18,433 --> 00:00:21,383 make sure that gradient descent is converging. 13 00:00:21,383 --> 00:00:23,084 Often, plots of the data 14 00:00:23,084 --> 00:00:24,795 or of all the learning algorithm outputs 15 00:00:24,810 --> 00:00:26,422 will also give you ideas 16 00:00:26,422 --> 00:00:29,391 for how to improve your learning algorithm. 17 00:00:29,391 --> 00:00:31,312 Fortunately, Octave has very 18 00:00:31,330 --> 00:00:33,045 simple tools to generate lots 19 00:00:33,070 --> 00:00:34,534 of different plots and when 20 00:00:34,534 --> 00:00:36,518 I use learning algorithms, I find 21 00:00:36,518 --> 00:00:38,082 that plotting the data, plotting 22 00:00:38,082 --> 00:00:40,175 the learning algorithm and so 23 00:00:40,175 --> 00:00:42,057 on are often an important 24 00:00:42,060 --> 00:00:43,165 part of how I get 25 00:00:43,165 --> 00:00:45,937 ideas for improving the 26 00:00:45,980 --> 00:00:47,199 algorithms and in this video, 27 00:00:47,199 --> 00:00:48,482 I'd like to show you some 28 00:00:48,482 --> 00:00:52,773 of these Octave tools for plotting and visualizing your data. 29 00:00:53,700 --> 00:00:55,301 Here's my Octave window. 30 00:00:55,301 --> 00:00:57,471 Let's quickly generate some data 31 00:00:57,471 --> 00:00:58,646 for us to plot. 32 00:00:58,646 --> 00:00:59,724 So I'm going to set T 33 00:00:59,740 --> 00:01:02,181 to be equal to, you know, this array of numbers. 34 00:01:02,210 --> 00:01:03,828 Here's T, set of 35 00:01:03,828 --> 00:01:06,685 numbers going from 0 up to .98. 36 00:01:06,700 --> 00:01:09,048 Let's set y1 equals sine 37 00:01:09,060 --> 00:01:11,340 of 2 pie 40 and 38 00:01:12,540 --> 00:01:16,102 if I want to plot the sine function, it's very easy. 39 00:01:16,102 --> 00:01:17,918 I just type plot T comma Y 40 00:01:17,918 --> 00:01:20,304 1 and hit enter. 41 00:01:20,320 --> 00:01:22,233 And up comes this plot 42 00:01:22,233 --> 00:01:24,270 where the horizontal axis is 43 00:01:24,270 --> 00:01:25,515 the T variable and the vertical 44 00:01:25,515 --> 00:01:26,946 axis is y1, which 45 00:01:26,960 --> 00:01:30,577 is the sine you saw in the function that we just computed. 46 00:01:30,990 --> 00:01:32,281 Let's set y2 to be 47 00:01:32,281 --> 00:01:34,401 equal to the cosine 48 00:01:34,410 --> 00:01:38,735 of two pi, four T, like so. 49 00:01:38,750 --> 00:01:41,403 And if I plot 50 00:01:41,403 --> 00:01:43,835 T comma y2, what octave 51 00:01:43,835 --> 00:01:45,045 will I do is I'll take my 52 00:01:45,060 --> 00:01:46,988 sine plot and it 53 00:01:46,988 --> 00:01:48,681 will replace with this cosine 54 00:01:48,690 --> 00:01:51,322 function and now, you know, cosine of xi of 1. 55 00:01:51,330 --> 00:01:53,006 Now, what if I 56 00:01:53,010 --> 00:01:54,581 want to have both 57 00:01:54,610 --> 00:01:56,981 the sine and the cosine plots on top of each other? 58 00:01:56,990 --> 00:01:59,702 What I'm going to do is I'm 59 00:01:59,702 --> 00:02:01,164 going to type plot t,y1. 60 00:02:01,164 --> 00:02:03,332 So here's my sine function, and then 61 00:02:03,332 --> 00:02:06,958 I'm going to use the function hold on. 62 00:02:06,958 --> 00:02:08,908 And what hold does it closes 63 00:02:08,920 --> 00:02:10,247 octaves to now 64 00:02:10,270 --> 00:02:11,490 figures on top of the 65 00:02:11,490 --> 00:02:13,772 old one and let 66 00:02:13,772 --> 00:02:15,249 me now plot t y2. 67 00:02:15,249 --> 00:02:19,812 I'm going to plot the cosine function in a different color. 68 00:02:19,850 --> 00:02:22,166 So, let me put there 69 00:02:22,180 --> 00:02:24,093 r in quotation marks there 70 00:02:24,093 --> 00:02:25,339 and instead of replacing 71 00:02:25,339 --> 00:02:26,615 the current figure, I'll plot the 72 00:02:26,620 --> 00:02:28,499 cosine function on top and 73 00:02:28,499 --> 00:02:32,915 the r indicates the what is an event color. 74 00:02:32,915 --> 00:02:35,166 And here additional commands - x 75 00:02:35,166 --> 00:02:39,157 label times, to label the X axis, or the horizontal axis. 76 00:02:39,160 --> 00:02:41,451 And Y label values A, 77 00:02:41,451 --> 00:02:44,688 to label the vertical axis value, 78 00:02:44,688 --> 00:02:47,032 and I can also 79 00:02:54,532 --> 00:02:57,616 label my two lines 80 00:02:57,620 --> 00:03:01,514 with this command: legend sine cosine 81 00:03:01,514 --> 00:03:02,860 and this puts this 82 00:03:02,890 --> 00:03:04,125 legend up on the upper 83 00:03:04,125 --> 00:03:05,122 right showing what the 2 84 00:03:05,122 --> 00:03:08,285 lines are, and finally title 85 00:03:08,290 --> 00:03:12,753 my plot is the title at the top of this figure. 86 00:03:12,753 --> 00:03:13,835 Lastly, if you want to save 87 00:03:13,835 --> 00:03:18,197 this figure, you type print -dpng 88 00:03:18,197 --> 00:03:20,128 myplot 89 00:03:20,128 --> 00:03:21,505 .png. 90 00:03:21,505 --> 00:03:23,292 So PNG is a graphics 91 00:03:23,292 --> 00:03:25,170 file format, and if you 92 00:03:25,170 --> 00:03:27,612 do this it will let you save this as a file. 93 00:03:27,612 --> 00:03:28,902 If I do that, 94 00:03:28,920 --> 00:03:31,287 let me actually change directory to, 95 00:03:31,320 --> 00:03:35,114 let's see, like 96 00:03:35,130 --> 00:03:39,180 that, and then I will print that out. 97 00:03:39,230 --> 00:03:41,692 So this will take a 98 00:03:41,700 --> 00:03:43,869 while depending on how 99 00:03:43,890 --> 00:03:46,193 your Octave configuration is setup, 100 00:03:46,230 --> 00:03:48,891 may take a few seconds, but change 101 00:03:48,900 --> 00:03:50,730 directory to my desktop and Octave 102 00:03:50,730 --> 00:03:53,943 is now taking a few seconds to save this. 103 00:03:54,750 --> 00:03:57,635 If I now go to my desktop, Let's hide these windows. 104 00:03:57,670 --> 00:03:59,358 Here's myplot.png 105 00:03:59,370 --> 00:04:00,720 which Octave has saved, and you 106 00:04:00,740 --> 00:04:03,481 know, there's the figure saved as the PNG file. 107 00:04:03,481 --> 00:04:05,530 Octave can save thousand other formats as well. 108 00:04:05,530 --> 00:04:07,468 So, you can type help plot, 109 00:04:07,468 --> 00:04:09,497 if you want to see the 110 00:04:09,510 --> 00:04:11,512 other file formats, rather than 111 00:04:11,530 --> 00:04:13,377 PNG, that you can save 112 00:04:13,377 --> 00:04:15,149 figures in. 113 00:04:15,149 --> 00:04:16,471 And lastly, if you want 114 00:04:16,471 --> 00:04:18,507 to get rid of the plot, the 115 00:04:18,540 --> 00:04:23,867 close command causes the figure to go away. 116 00:04:23,867 --> 00:04:24,963 As I figure if I type 117 00:04:24,963 --> 00:04:26,628 close, that figure just 118 00:04:26,628 --> 00:04:30,153 disappeared from my desktop. 119 00:04:30,640 --> 00:04:33,372 Octave also lets you specify a figure and numbers. 120 00:04:33,372 --> 00:04:36,935 You type figure 1 plots t, y1. 121 00:04:36,935 --> 00:04:39,582 That starts up 122 00:04:39,670 --> 00:04:41,959 first figure, and that plots t, y1. 123 00:04:41,970 --> 00:04:45,075 And then if you want a second figure, you specify a different figure number. 124 00:04:45,075 --> 00:04:47,765 So figure two, plot t, 125 00:04:47,780 --> 00:04:49,924 y2 like so, and 126 00:04:49,924 --> 00:04:53,084 now on my desktop, I actually have 2 figures. 127 00:04:53,084 --> 00:04:54,625 So, figure 1 and figure 128 00:04:54,625 --> 00:04:55,874 2 thus 1 plotting the sine 129 00:04:55,874 --> 00:04:59,169 function, 1 plotting the cosine function. 130 00:04:59,170 --> 00:05:00,498 Here's one other neat command that 131 00:05:00,498 --> 00:05:02,825 I often use, which is the subplot command. 132 00:05:02,825 --> 00:05:05,401 So, we're going to use subplot 1 2 1. 133 00:05:05,401 --> 00:05:07,958 What it does it sub-divides 134 00:05:07,958 --> 00:05:11,200 the plot into a 135 00:05:11,780 --> 00:05:13,760 one-by-two grid with the 136 00:05:13,820 --> 00:05:16,010 first 2 parameters are, and 137 00:05:16,010 --> 00:05:17,607 it starts to access the 138 00:05:17,620 --> 00:05:19,335 first element. That's 139 00:05:19,340 --> 00:05:21,714 what the final parameter 1 is, right? 140 00:05:21,714 --> 00:05:23,568 So, divide my figure into a 141 00:05:23,568 --> 00:05:24,913 one by two grid, and I 142 00:05:24,913 --> 00:05:26,585 want to access the first 143 00:05:26,585 --> 00:05:27,948 element right now. 144 00:05:27,970 --> 00:05:30,435 And so, if I type that 145 00:05:30,435 --> 00:05:32,722 in, this product, this figure, is on the left. 146 00:05:32,760 --> 00:05:35,291 And if I plot t, 147 00:05:35,350 --> 00:05:37,682 y1, it now fills 148 00:05:37,682 --> 00:05:40,462 up this first element. 149 00:05:40,462 --> 00:05:42,565 And if I I'll do subplot 122. 150 00:05:42,565 --> 00:05:44,456 I'm going to start to 151 00:05:44,456 --> 00:05:48,724 access the second element and plot t, y2. 152 00:05:49,270 --> 00:05:51,323 Well, throw in y2 in 153 00:05:51,323 --> 00:05:54,875 the right hand side, or in the second element. 154 00:05:54,910 --> 00:05:56,114 And last command, you can 155 00:05:56,114 --> 00:05:58,165 also change the axis scales 156 00:05:58,165 --> 00:06:00,308 and change axis these to 1.51 157 00:06:00,330 --> 00:06:02,892 minus 1 1 and this 158 00:06:02,892 --> 00:06:05,071 sets the x range 159 00:06:05,071 --> 00:06:07,448 and y range for the 160 00:06:07,448 --> 00:06:09,874 figure on the right, 161 00:06:09,890 --> 00:06:12,381 and concretely, it assess the horizontal 162 00:06:12,381 --> 00:06:13,668 major values in the figure 163 00:06:13,670 --> 00:06:14,856 on the right to make sure 0.5 164 00:06:14,856 --> 00:06:16,334 to 1, and the vertical 165 00:06:16,340 --> 00:06:19,572 axis values use the range from minus one to one. 166 00:06:19,572 --> 00:06:21,736 And, you know, you don't need to memorize all these commands. 167 00:06:21,736 --> 00:06:23,178 If you ever need to 168 00:06:23,178 --> 00:06:24,773 change the access or you 169 00:06:24,780 --> 00:06:25,703 need to know is that, you know, there's an 170 00:06:25,703 --> 00:06:26,628 access command and you can 171 00:06:26,628 --> 00:06:28,364 already get the details 172 00:06:28,364 --> 00:06:31,590 from the usual octave help command. 173 00:06:31,600 --> 00:06:32,861 Finally, just a couple last 174 00:06:32,861 --> 00:06:35,449 commands CLF clear is 175 00:06:35,450 --> 00:06:38,362 a figure and here's one unique trait. 176 00:06:38,362 --> 00:06:39,772 Let's set a to be equal 177 00:06:39,772 --> 00:06:42,076 to a 5 by 5 178 00:06:42,076 --> 00:06:43,375 magic squares a. So, a 179 00:06:43,380 --> 00:06:45,290 is now this 5 by 5 180 00:06:45,310 --> 00:06:47,581 matrix does a neat 181 00:06:47,581 --> 00:06:49,341 trick that I sometimes use to 182 00:06:49,350 --> 00:06:51,582 visualize the matrix, which is 183 00:06:51,582 --> 00:06:54,792 I can use image sc 184 00:06:54,800 --> 00:06:56,362 of a what this will 185 00:06:56,370 --> 00:06:58,056 do is plot a five 186 00:06:58,056 --> 00:07:03,925 by five matrix, a five by five grid of color. 187 00:07:03,925 --> 00:07:05,739 where the different colors correspond to 188 00:07:05,739 --> 00:07:09,011 the different values in the A matrix. 189 00:07:09,060 --> 00:07:13,262 So concretely, I can also do color bar. 190 00:07:13,630 --> 00:07:14,903 Let me use a 191 00:07:14,903 --> 00:07:16,715 more sophisticated command, and image sc 192 00:07:16,715 --> 00:07:19,608 A color bar 193 00:07:19,608 --> 00:07:22,454 color map gray. 194 00:07:22,454 --> 00:07:24,757 This is actually running three commands at a time. 195 00:07:24,760 --> 00:07:26,286 I'm running image sc then running 196 00:07:26,286 --> 00:07:28,943 color bar, then running color map gray. 197 00:07:28,943 --> 00:07:30,142 And what this does, is it sets 198 00:07:30,160 --> 00:07:31,355 a color map, so a 199 00:07:31,355 --> 00:07:32,749 gray color map, and on the 200 00:07:32,749 --> 00:07:35,333 right it also puts in this color bar. 201 00:07:35,360 --> 00:07:37,525 And so this color bar 202 00:07:37,550 --> 00:07:40,701 shows what the different shades of color correspond to. 203 00:07:40,720 --> 00:07:42,704 Concretely, the upper left 204 00:07:42,704 --> 00:07:44,494 element of the A matrix 205 00:07:44,494 --> 00:07:46,358 is 17, and so that corresponds 206 00:07:46,358 --> 00:07:49,297 to kind of a mint shade of gray. 207 00:07:49,297 --> 00:07:52,012 Whereas in contrast the second 208 00:07:52,012 --> 00:07:53,210 element of A--sort of the 209 00:07:53,280 --> 00:07:55,640 1 2 element of A--is 24. 210 00:07:55,640 --> 00:07:57,716 Right, so it's A 1 2 is 24. 211 00:07:57,716 --> 00:07:59,683 So that corresponds to 212 00:07:59,690 --> 00:08:01,343 this square out here, which is 213 00:08:01,360 --> 00:08:03,677 nearly a shade of white. 214 00:08:03,677 --> 00:08:05,640 And the small value, say 215 00:08:05,690 --> 00:08:08,657 A--what is that? A 216 00:08:08,657 --> 00:08:12,260 4 5, you know, is a value 217 00:08:12,300 --> 00:08:14,346 3 over here that corresponds-- 218 00:08:14,360 --> 00:08:15,548 you can see on my color bar 219 00:08:15,548 --> 00:08:16,618 that it corresponds to a 220 00:08:16,618 --> 00:08:19,499 much darker shade in this image. 221 00:08:19,499 --> 00:08:21,141 So here's another example, 222 00:08:21,141 --> 00:08:23,228 I can plot a larger, you 223 00:08:23,230 --> 00:08:24,768 know, here's a magic 15 that 224 00:08:24,770 --> 00:08:26,029 gives you a 15 by 15 225 00:08:26,029 --> 00:08:27,675 magic square and this 226 00:08:27,680 --> 00:08:29,504 gives me a plot of what 227 00:08:29,504 --> 00:08:33,675 my 15 by 15 magic squares values looks like. 228 00:08:33,700 --> 00:08:35,225 And finally to wrap 229 00:08:35,225 --> 00:08:37,075 up this video, what you've 230 00:08:37,075 --> 00:08:38,318 seen me do here is 231 00:08:38,318 --> 00:08:41,917 use comma chaining of function calls. 232 00:08:41,940 --> 00:08:43,195 Here's how you actually do this. 233 00:08:43,210 --> 00:08:44,638 If I type A equals 234 00:08:44,690 --> 00:08:46,613 1, B equals 2, C equals 235 00:08:46,613 --> 00:08:48,620 3, and hit Enter, then 236 00:08:48,620 --> 00:08:50,628 this is actually carrying out 237 00:08:50,628 --> 00:08:52,039 three commands at the same time. 238 00:08:52,040 --> 00:08:53,490 Or really carrying out three 239 00:08:53,490 --> 00:08:55,849 commands, one after another, 240 00:08:55,849 --> 00:08:57,521 and it prints out all three results. 241 00:08:57,521 --> 00:08:58,417 And this is a lot like 242 00:08:58,417 --> 00:09:00,489 A equals 1, B equals 243 00:09:00,489 --> 00:09:01,755 2, C equals 3, except 244 00:09:01,755 --> 00:09:03,532 that if I use semicolons instead 245 00:09:03,540 --> 00:09:05,854 of a comma, it doesn't print out anything. 246 00:09:05,854 --> 00:09:07,195 So, this, you know, 247 00:09:07,210 --> 00:09:08,865 this thing here we call comma 248 00:09:08,870 --> 00:09:12,185 chaining of commands, or comma chaining of function calls. 249 00:09:12,240 --> 00:09:13,755 And, it's just another 250 00:09:13,755 --> 00:09:15,520 convenient way in Octave to 251 00:09:15,520 --> 00:09:17,778 put multiple commands like image sc 252 00:09:17,778 --> 00:09:19,358 color bar, colon map 253 00:09:19,360 --> 00:09:22,919 to put multi-commands on the same line. 254 00:09:22,930 --> 00:09:24,104 So, that's it. 255 00:09:24,104 --> 00:09:25,281 You now know how to plot 256 00:09:25,281 --> 00:09:27,504 different figures and octave, and 257 00:09:27,504 --> 00:09:29,270 in next video the 258 00:09:29,280 --> 00:09:30,430 next main piece that I want 259 00:09:30,460 --> 00:09:31,985 to tell you about is how to 260 00:09:31,985 --> 00:09:33,622 write control statements like if, 261 00:09:33,630 --> 00:09:35,294 while, for statements and 262 00:09:35,294 --> 00:09:39,426 octave as well as hard to define and use functions