1 00:00:00,950 --> 00:00:02,400 So how did you do with your reflection? 2 00:00:02,400 --> 00:00:04,760 Now, here's what I came up with. 3 00:00:04,760 --> 00:00:09,950 So, my first point is, an array's size is immutable, you can't change it right? 4 00:00:09,950 --> 00:00:11,180 So you can't append, insert, or 5 00:00:11,180 --> 00:00:16,380 remove elements like you can with a list and we showed that down here. 6 00:00:16,380 --> 00:00:19,400 All of an array's elements must be of the same data type. 7 00:00:19,400 --> 00:00:22,350 I went and did a search for the different data types that are available, and 8 00:00:22,350 --> 00:00:23,440 I made a little link here. 9 00:00:23,440 --> 00:00:24,910 I like to use those as bookmarks, and 10 00:00:24,910 --> 00:00:26,840 we'll actually use this one here just in a bit. 11 00:00:26,840 --> 00:00:31,270 And then finally, a NumPy array behaves in a Pythonic fashion. 12 00:00:31,270 --> 00:00:34,750 You can use len(my_array) just like you'd assume. 13 00:00:34,750 --> 00:00:36,540 Awesome, how did you do? 14 00:00:36,540 --> 00:00:37,530 That feel good? 15 00:00:37,530 --> 00:00:38,610 All right, are you ready? 16 00:00:38,610 --> 00:00:40,420 Let's build out our study log. 17 00:00:40,420 --> 00:00:44,180 We should track how many minutes we studied for each day. 18 00:00:44,180 --> 00:00:48,510 Now we know that there are exactly 100 days in the challenge and 19 00:00:48,510 --> 00:00:49,700 since we can't add or 20 00:00:49,700 --> 00:00:54,700 remove entries we know that the array that we create will need to be 100 elements. 21 00:00:55,770 --> 00:00:59,690 Now before when we created our array we did it from an iterable, right? 22 00:00:59,690 --> 00:01:03,560 Remember we did the this np.array and we passed in this list here. 23 00:01:03,560 --> 00:01:06,520 But in this case, we don't have an iterable. 24 00:01:06,520 --> 00:01:11,770 We don't really have any data, but we do know that we eventually will. 25 00:01:11,770 --> 00:01:15,480 This is a common enough problem that there is a provided solution. 26 00:01:15,480 --> 00:01:20,030 What we want basically is 100 element array where each element will 27 00:01:20,030 --> 00:01:23,048 represent the minutes that we coded each day. 28 00:01:23,048 --> 00:01:29,240 And we want each of those elements to be initialized with the value of 0. 29 00:01:29,240 --> 00:01:34,190 Lucky for us, there's a helper method called zeros that does just that. 30 00:01:34,190 --> 00:01:38,430 So, let's do it, let's create a new array called study_minutes. 31 00:01:38,430 --> 00:01:42,600 And it's right off of that package, so np.zeros. 32 00:01:42,600 --> 00:01:47,621 And the first parameter is the shape or size, so we want 100, awesome. 33 00:01:48,950 --> 00:01:51,590 And if we look at our study minute array, 34 00:01:51,590 --> 00:01:54,620 we can do that just by putting it right underneath here. 35 00:01:54,620 --> 00:01:57,538 So study_minutes, and there we go. 36 00:01:57,538 --> 00:01:59,790 100 elements, a 100 zeros. 37 00:01:59,790 --> 00:02:03,720 And you'll see that these 0s have this dot here and 38 00:02:03,720 --> 00:02:06,070 that's showing that these are floating point numbers. 39 00:02:06,070 --> 00:02:10,816 Now, one thing that we can do with a Jupyter Notebook is to get some additional 40 00:02:10,816 --> 00:02:11,773 information. 41 00:02:11,773 --> 00:02:14,355 And we can do that using the whos command. 42 00:02:14,355 --> 00:02:18,000 W-H-O-S, this magic command, whos. 43 00:02:18,000 --> 00:02:20,630 If we do that, we can find the study_minutes array and 44 00:02:20,630 --> 00:02:24,380 we can see that it's 100 elements and it's of type float64. 45 00:02:24,380 --> 00:02:26,770 That must be with the default that's happening, all right. 46 00:02:26,770 --> 00:02:31,850 So it takes 64-bits or 8 bytes to store each of those. 47 00:02:31,850 --> 00:02:34,880 But we know something about these values, don't we? 48 00:02:34,880 --> 00:02:40,450 We know that there are 60 minutes in an hour and that there are 24 hours in a day. 49 00:02:40,450 --> 00:02:44,600 And therefore, we know that the max value could only be 1440, 50 00:02:44,600 --> 00:02:49,080 which really isn't too large of a number. 51 00:02:49,080 --> 00:02:51,340 Let's go ahead and use that link from before and 52 00:02:51,340 --> 00:02:53,530 see what other data types are available. 53 00:02:53,530 --> 00:02:58,220 So let's come back up to these notes, I'm gonna go ahead and click this. 54 00:02:59,920 --> 00:03:03,680 Okay, so these are the different ranges of values that things have and 55 00:03:03,680 --> 00:03:06,200 we're at float64 right now. 56 00:03:06,200 --> 00:03:09,150 So double precision float, there is signed bit. 57 00:03:09,150 --> 00:03:12,540 We don't need all of that, that's a lot of precision. 58 00:03:12,540 --> 00:03:16,800 We just need minutes up to 1440, not portions of the minute. 59 00:03:16,800 --> 00:03:18,490 So we don't need a floating point of all. 60 00:03:18,490 --> 00:03:20,800 So we're probably up in this integer area up here, right? 61 00:03:20,800 --> 00:03:22,100 So integers, right? 62 00:03:22,100 --> 00:03:27,460 Those are whole numbers and in fact, you can't study negative minutes in a day, 63 00:03:27,460 --> 00:03:30,790 so we need an unsigned integer that will be always positive. 64 00:03:30,790 --> 00:03:34,900 So these go negative, we only need zero forward, right? 65 00:03:34,900 --> 00:03:38,240 And so we need 1440, so 66 00:03:38,240 --> 00:03:44,400 this first one is uint that's again unsigned integer of 8 bits. 67 00:03:44,400 --> 00:03:47,950 We can probably get it here, right, because this 1440's in between here. 68 00:03:47,950 --> 00:03:49,520 So we can use this uint16, so 69 00:03:53,530 --> 00:03:57,870 the data types are available as np.the name of the thing afterwards. 70 00:03:57,870 --> 00:04:02,010 So we can use that, so let's flip back to our example. 71 00:04:02,010 --> 00:04:06,210 We're down here at our study_minutes and 72 00:04:06,210 --> 00:04:10,464 let's go ahead an we can put in our data 73 00:04:10,464 --> 00:04:14,982 type is np.uint16. 74 00:04:14,982 --> 00:04:19,190 And now remember this was at 800 bytes. 75 00:04:19,190 --> 00:04:23,320 So it took 800 bytes before so let's go ahead and let's specify the data type. 76 00:04:23,320 --> 00:04:24,710 Let me go ahead and run this one more time, and 77 00:04:24,710 --> 00:04:26,490 you'll see the dots are now gone. 78 00:04:26,490 --> 00:04:30,090 Now if I rerun this who is with a Ctrl+Enter, 79 00:04:30,090 --> 00:04:32,220 we'll see that it's now 200 bytes. 80 00:04:34,700 --> 00:04:38,840 Now, this time what we did is probably a little bit of premature optimization. 81 00:04:38,840 --> 00:04:40,900 We only have 100 entries, 82 00:04:40,900 --> 00:04:45,230 but I want you to imagine these arrays as enormously large. 83 00:04:45,230 --> 00:04:48,760 They tend to get that way as you work with larger and larger data sets. 84 00:04:48,760 --> 00:04:52,090 Constraining that the proper type can save a lot of space, and 85 00:04:52,090 --> 00:04:56,830 in turn, speed by specifying a data type to meet your needs. 86 00:04:56,830 --> 00:05:00,630 I've started this challenge already, and on the first day I plugged two and 87 00:05:00,630 --> 00:05:02,260 a half hours into it. 88 00:05:02,260 --> 00:05:06,420 Now you only need to do one hour each day, but I was super into the challenge, and 89 00:05:06,420 --> 00:05:07,940 I hope you will be too. 90 00:05:07,940 --> 00:05:13,909 So 2.5 hours is 60 plus 60, 120, plus 30, 150, okay. 91 00:05:13,909 --> 00:05:17,590 So I'm gonna set my minutes for the first day. 92 00:05:17,590 --> 00:05:21,310 So an array element can be set by its index just like list. 93 00:05:21,310 --> 00:05:25,160 And just like list, the indices are zero based, right? 94 00:05:25,160 --> 00:05:28,850 Remember, the first element is 0, the second is 1 and so on. 95 00:05:28,850 --> 00:05:31,631 So let's do that, let's set this. 96 00:05:31,631 --> 00:05:39,093 We'll say study_minutes[0] and we'll set that to our 150 and 97 00:05:39,093 --> 00:05:44,660 you can retrieve the value also by using the index. 98 00:05:44,660 --> 00:05:50,010 So we'll say first_day_minutes 99 00:05:50,010 --> 00:05:54,300 = study_minutes [0]. 100 00:05:54,300 --> 00:06:00,270 And if we take a look at first_day_minutes, see that 150 came back. 101 00:06:01,900 --> 00:06:03,660 That's interesting, though, isn't it? 102 00:06:03,660 --> 00:06:08,450 We know that when we put our value of 150, when we put this in here, 103 00:06:08,450 --> 00:06:15,690 we know that it got coerced or transformed into an unsigned integer 16, right? 104 00:06:15,690 --> 00:06:19,900 So we know that it is a uint16, but this looks like it's just one 150. 105 00:06:19,900 --> 00:06:22,530 Why don't we take a deeper look at this? 106 00:06:22,530 --> 00:06:27,380 So let's say type of first_day_minutes and we'll see what comes back. 107 00:06:29,210 --> 00:06:34,510 Nice, it's just cleverly wrapped in what is known as a scalar data type. 108 00:06:35,750 --> 00:06:40,160 Here you see that the scalar data type is uint16 which is what we declared 109 00:06:40,160 --> 00:06:41,160 each of the elements to be. 110 00:06:42,245 --> 00:06:44,990 Scalar is a term that comes from mathematics and for 111 00:06:44,990 --> 00:06:49,460 our purposes right now, it can be thought of as representing a singular value. 112 00:06:49,460 --> 00:06:52,270 Now, you'll hear the term scalar thrown around for values that 113 00:06:52,270 --> 00:06:56,110 are a singular element, and the term vector for those that are multiple. 114 00:06:56,110 --> 00:06:58,900 You'll also hear arrays and vectors used interchangeably. 115 00:06:58,900 --> 00:07:02,670 Now this naming comes from their common mathematical use cases, 116 00:07:02,670 --> 00:07:04,340 which we're gonna touch on here in a bit. 117 00:07:04,340 --> 00:07:08,540 For now though, I want you to just know that the single values pulled out of 118 00:07:08,540 --> 00:07:14,980 a numpy array are scalar and wrapped in their specific data type object. 119 00:07:14,980 --> 00:07:18,265 These scalar objects provide the same APIs as arrays. 120 00:07:18,265 --> 00:07:22,500 This is so that scalar objects can interact with other arrays easily. 121 00:07:22,500 --> 00:07:24,240 Again, don't fret too much about this, 122 00:07:24,240 --> 00:07:26,230 we'll get into it as the course progresses. 123 00:07:26,230 --> 00:07:27,330 But check the teacher's notes for 124 00:07:27,330 --> 00:07:30,180 more information on scalars, if you're interested. 125 00:07:30,180 --> 00:07:33,410 Let's go ahead and fill in the second day of the challenge. 126 00:07:33,410 --> 00:07:36,480 I was able to sneak in some code after I got my kids to bed. 127 00:07:36,480 --> 00:07:40,120 I've been working on this micro controller project and 128 00:07:40,120 --> 00:07:45,920 I was able to get some progress on it and the challenge encouraged me to do it. 129 00:07:45,920 --> 00:07:50,480 So I spent about an hour on it, okay so how would I track that? 130 00:07:50,480 --> 00:07:53,030 How would I go about doing that? 131 00:07:53,030 --> 00:07:57,210 So I wanna put 60 minutes into the second day of the challenge, 132 00:07:58,240 --> 00:08:00,680 hey how about you give that a shot? 133 00:08:00,680 --> 00:08:02,600 You know, just to make sure this stuff is sinking in. 134 00:08:02,600 --> 00:08:04,440 So, I'll do a to do here and you do it. 135 00:08:04,440 --> 00:08:10,435 So TODO, add 60 minutes to the second 136 00:08:10,435 --> 00:08:15,710 day in the study_minutes array. 137 00:08:17,870 --> 00:08:20,860 Pause me and give it a go, and then after you get it, unpause me and 138 00:08:20,860 --> 00:08:21,940 I'll show you how I did it. 139 00:08:21,940 --> 00:08:24,675 Are you ready, pause me. 140 00:08:24,675 --> 00:08:29,270 Okay, so I can't tell you how many times this bites me. 141 00:08:29,270 --> 00:08:34,470 I said second day so I fell like it should be index 2, but it isn't is it? 142 00:08:34,470 --> 00:08:41,120 It's actually index 1, and I'm gonna set that to 60. 143 00:08:41,120 --> 00:08:45,080 Now you might be wondering if you can assign multiple values to an array in 144 00:08:45,080 --> 00:08:46,820 a single line, in a single statement. 145 00:08:46,820 --> 00:08:50,370 If you remember your collections slicing skills, they work here too. 146 00:08:50,370 --> 00:08:53,620 So, I've got four more days of data to enter. 147 00:08:53,620 --> 00:08:57,250 I kept plugging away and let's see, I had 80 minutes, 148 00:08:58,860 --> 00:09:02,110 on day three, and then I squeezed in another hour of 60. 149 00:09:02,110 --> 00:09:04,970 And I only got a half hour on day five. 150 00:09:04,970 --> 00:09:09,000 So it doesn't actually meet the challenge criteria of doing an hour a day. 151 00:09:09,000 --> 00:09:12,490 But you are allowed one day of wiggle room, so I didn't break the challenge. 152 00:09:12,490 --> 00:09:15,560 So, I kept going and I made up for it and I got 90 here. 153 00:09:15,560 --> 00:09:20,140 So, I want to add these into the study minutes array and 154 00:09:20,140 --> 00:09:21,980 I wanna start at the third day. 155 00:09:21,980 --> 00:09:26,800 So we'll say study_minutes, we'll start at the third day so again that's 2, 156 00:09:26,800 --> 00:09:31,550 there are four of them so I wanna go up to the 6th day. 157 00:09:31,550 --> 00:09:34,440 So the way that slices work remember is they are exclusive it does 158 00:09:34,440 --> 00:09:36,050 not include that seven. 159 00:09:36,050 --> 00:09:41,730 So we're gonna say 2 to the 6th, 160 00:09:41,730 --> 00:09:45,700 so it's not the seventh day, there we go and that should do it. 161 00:09:45,700 --> 00:09:48,450 That will set it and if we go ahead and 162 00:09:48,450 --> 00:09:52,677 take a look at it afterwards, we say study_minutes. 163 00:09:52,677 --> 00:09:57,292 Give that a run, we will see, boom there they are, so we got 150 on our first day, 164 00:09:57,292 --> 00:10:01,220 60 on the second and here's the values, 80, 60, 30, 90. 165 00:10:01,220 --> 00:10:04,660 Awesome, if your slicing muscles are out of shape, don't worry. 166 00:10:04,660 --> 00:10:06,690 We'll exercise them throughout this course, and 167 00:10:06,690 --> 00:10:08,900 you'll be super fit when we're done. 168 00:10:08,900 --> 00:10:13,876 But one thing we didn't go over earlier when we ran this Jupyter Notebook 169 00:10:13,876 --> 00:10:18,703 whos command, is that you'll notice that data type here is ndarray. 170 00:10:18,703 --> 00:10:21,691 Now this nd, it stands for n dimensional, 171 00:10:21,691 --> 00:10:25,610 meaning the array can have many dimensions. 172 00:10:25,610 --> 00:10:30,150 Which is to say that these arrays can be multi-dimensional, 173 00:10:30,150 --> 00:10:33,130 which I realize sounds a lot like a science fiction term. 174 00:10:33,130 --> 00:10:36,550 We'll get to what that means exactly right after a quick break. 175 00:10:36,550 --> 00:10:40,790 Hey, during that break, why don't you do a little reflection? 176 00:10:40,790 --> 00:10:43,930 Specifically surrounding what we went over about datatypes and 177 00:10:43,930 --> 00:10:47,220 choosing the right one and how it can save space. 178 00:10:47,220 --> 00:10:51,200 I'm going to put my notes in here right above this call, 179 00:10:53,060 --> 00:10:55,920 right above this call to zero. 180 00:10:55,920 --> 00:10:58,670 So I'm going to press Esc, I'm gonna get back into command mode. 181 00:10:58,670 --> 00:11:02,580 I'm gonna press above, then I'm gonna get this into Markdown. 182 00:11:04,500 --> 00:11:07,960 There's probably a keyboard shortcut for that, share about that. 183 00:11:07,960 --> 00:11:11,380 So I'm gonna say, About data types. 184 00:11:13,550 --> 00:11:16,230 And again, just like before, I'll share mine right after the break. 185 00:11:16,230 --> 00:11:19,270 Take some notes about what we learned here, see you soon.