Creating the Study Log11:19 with Craig Dennis
Let's create a study log that we will use to track the minutes we will study during the #100DaysOfCode challenge
My Notes for Introducing Arrays
## Differences between lists and NumPy Arrays * An array's size is immutable. You cannot append, insert or remove elements, like you can with a list. * All of an array's elements must be of the same [data type](https://docs.scipy.org/doc/numpy-1.14.0/user/basics.types.html). * A NumPy array behaves in a Pythonic fashion. You can `len(my_array)` just like you would assume.
So how did you do with your reflection? 0:00 Now, here's what I came up with. 0:02 So, my first point is, an arrays size is immutable, you can't change it right? 0:04 So you can't append, insert, or 0:09 remove elements like you can with a list and we showed that down here. 0:11 All of an array's elements must be of the same data type. 0:16 I went and did a search for the different data types that are available, and 0:19 I made a little link here. 0:22 I like to use those as bookmarks, and 0:23 we'll actually use this one here just in a bit. 0:24 And then finally, a NumPy array behaves in a Pythonic fashion. 0:26 You can use len(my_array) just like you'd assume. 0:31 Awesome, how did you do? 0:34 That feel good? 0:36 All right, are you ready? 0:37 Let's build out our study log. 0:38 We should track how many minutes we studied for each day. 0:40 Now we know that there are exactly 100 days in the challenge and 0:44 since we can't add or 0:48 remove entries we know that the array that we create will need to be 100 elements. 0:49 Now before when we created our array we did it from an interable, right? 0:55 Remember we did the this np.array and we passed in this list here. 0:59 But in this case, we don't have an interable. 1:03 We don't really have any data, but we do know that we eventually will. 1:06 This is a common enough problem that there is a provided solution. 1:11 What we what basically is 100 element array where each element will 1:15 represent the minutes that we coded each day. 1:20 And we want each of those elements to be initialized with the value of 0. 1:23 Lucky for us, there's a helper method called zeros that does just that. 1:29 So, let's do it, let's create a new array called study_minutes. 1:34 And it's right off of that package, so np.zeros. 1:38 And the first parameter is the shape or size, so we want 100, awesome. 1:42 And if we look at our study minute array, 1:48 we can do that just by putting it right underneath here. 1:51 So study_minutes, and there we go. 1:54 100 elements, a 100 zeros. 1:57 And you'll see that these 0s have this dot here and 1:59 that's showing that these are floating point numbers. 2:03 Now, one thing that we can do with a Jupyter Notebook is to get some additional 2:06 information. 2:10 And we can do that using the whos command. 2:11 W-H-O-S, this magic command, whos. 2:14 If we do that, we can find the study_minutes array and 2:18 we can see that it's 100 elements and it's of type float64. 2:20 That must be with the default that's happening, all right. 2:24 So it takes 64-bits or 8 bytes to store each of those. 2:26 But we know something about these values, don't we? 2:31 We know that there are 60 minutes in an hour and that there are 24 hours in a day. 2:34 And therefore, we know that the max value could only be 1440, 2:40 which really isn't too large of a number. 2:44 Let's go ahead and use that link from before and 2:49 see what other data types are available. 2:51 So let's come back up to these notes, I'm gonna go ahead and click this. 2:53 Okay, so these are the different ranges of values that things have and 2:59 we're at float64 right now. 3:03 So double precision float, there is signed bit. 3:06 We don't need all of that, that's a lot of precision. 3:09 We just need minutes up to 1440, not portions of the minute. 3:12 So we don't need a floating point of all. 3:16 So we're probably up in this integer area up here, right? 3:18 So integers, right? 3:20 Those are whole numbers and in fact, you can't study negative minutes in a day, 3:22 so we need an unsigned integer that will be always positive. 3:27 So these go negative, we only need zero forward, right? 3:30 And so we need 1440, so 3:34 this first one is uint that's again unsigned integer of 8 bits. 3:38 We can probably get it here, right, because this 1440's in between here. 3:44 So we can use this uint16, so 3:47 the data types are available as np.the name of the thing afterwards. 3:53 So we can use that, so let's flip back to our example. 3:57 We're down here at our study_minutes and 4:02 let's go ahead an we can put in our data 4:06 type is np.uint16. 4:10 And now remember this was at 800 bytes. 4:14 So it took 800 bytes before so let's go ahead and let's specify the data type. 4:19 Let me go ahead and run this one more time, and 4:23 you'll see the dots are now gone. 4:24 Now if I rerun this who is with a Ctrl+Enter, 4:26 we'll see that it's now 200 bytes. 4:30 Now, this time what we did is probably a little bit of premature optimization. 4:34 We only have 100 entries, 4:38 but I want you to imagine these arrays as enormously large. 4:40 They tend to get that way as you work with larger and larger data sets. 4:45 Constraining that the proper type can save a lot of space, and 4:48 in turn, speed by specifying a data type to meet your needs. 4:52 I've started this challenge already, and on the first day I plugged two and 4:56 a half hours into it. 5:00 Now you only need to do one hour each day, but I was super into the challenge, and 5:02 I hope you will be too. 5:06 So 2.5 hours is 60 plus 60, 120, plus 30, 150, okay. 5:07 So I'm gonna set my minutes for the first day. 5:13 So an array element can be set by its index just like list. 5:17 And just like list, the indices are zero based, right? 5:21 Remember, the first element is 0, the second is 1 and so on. 5:25 So let's do that, let's set this. 5:28 We'll say study_minutes and we'll set that to our 150 and 5:31 you can retrieve the value also by using the index. 5:39 So we'll say first_day_minutes 5:44 = study_minutes . 5:50 And if we take a look at first_day_minutes, see that 150 came back. 5:54 That's interesting, though, isn't it? 6:01 We know that when we put our value of 150, when we put this in here, 6:03 we know that it got coerced or transformed into an unsigned integer 16, right? 6:08 So we know that it is a uint16, but this looks like it's just one 150. 6:15 Why don't we take a deeper look at this? 6:19 So let's say type of first_day_minutes and we'll see what comes back. 6:22 Nice, it's just cleverly wrapped in what is known as a scalar data type. 6:29 Here you see that the scalar data type is uint16 which is what we declared 6:35 each of the elements to be. 6:40 Scalar is a term that comes from mathematics and for 6:42 our purposes right now, it can be thought of as representing a singular value. 6:44 Now, you'll hear the term scalar thrown around for values that 6:49 are a singular element, and the term vector for those that are multiple. 6:52 You'll also hear arrays and vectors used interchangeably. 6:56 Now this naming comes from their common mathematical use cases, 6:58 which we're gonna touch on here in a bit. 7:02 For now though, I want you to just know that the single values pulled out of 7:04 a numpy array are scalar and wrapped in their specific data type object. 7:08 These scalar objects provide the same APIs as arrays. 7:14 This is so that scalar objects can interact with other arrays easily. 7:18 Again, don't fret too much about this, 7:22 we'll get into it as the course progresses. 7:24 But check the teacher's notes for 7:26 more information on scalars, if you're interested. 7:27 Let's go ahead and fill in the second day of the challenge. 7:30 I was able to sneak in some code after I got my kids to bed. 7:33 I've been working on this micro controller project and 7:36 I was able to get some progress on it and the challenge encouraged me to do it. 7:40 So I spent about an hour on it, okay so how would I track that? 7:45 How would I go about doing that? 7:50 So I wanna put 60 minutes into the second day of the challenge, 7:53 hey how about you give that a shot? 7:58 You know, just to make sure this stuff is sinking in. 8:00 So, I'll do a to do here and you do it. 8:02 So TODO, add 60 minutes to the second 8:04 day in the study_minutes array. 8:10 Pause me and give it a go, and then after you get it, unpause me and 8:17 I'll show you how I did it. 8:20 Are you ready, pause me. 8:21 Okay, so I can't tell you how many times this bites me. 8:24 I said second day so I fell like it should be index 2, but it isn't is it? 8:29 It's actually index 1, and I'm gonna set that to 60. 8:34 Now you might be wondering if you can assign multiple values to an array in 8:41 a single line, in a single statement. 8:45 If you remember your collections slicing skills, they work here too. 8:46 So, I've got four more days of data to enter. 8:50 I kept plugging away and let's see, I had 80 minutes, 8:53 on day three, and then I squeezed in another hour of 60. 8:58 And I only got a half hour on day five. 9:02 So it doesn't actually meet the challenge criteria of doing an hour a day. 9:04 But you are allowed one day of wiggle room, so I didn't break the challenge. 9:09 So, I kept going and I made up for it and I got 90 here. 9:12 So, I want to add these into the study minutes array and 9:15 I wanna start at the third day. 9:20 So we'll say study_minutes, we'll start at the third day so again that's 2, 9:21 there are four of them so I wanna go up to the 6th day. 9:26 So the way that slices work remember is they are exclusive it does 9:31 not include that seven. 9:34 So we're gonna say 2 to the 6th, 9:36 so it's not the seventh day, there we go and that should do it. 9:41 That will set it and if we go ahead and 9:45 take a look at it afterwards, we say study_minutes. 9:48 Give that a run, we will see, boom there they are, so we got 150 on our first day, 9:52 60 on the second and here's the values, 80, 60, 30, 90. 9:57 Awesome, if your slicing muscles are out of shape, don't worry. 10:01 We'll exercise them throughout this course, and 10:04 you'll be super fit when we're done. 10:06 But one thing we didn't go over earlier when we ran this Jupyter Notebook 10:08 whos command, is that you'll notice that data type here is ndarray. 10:13 Now this nd, it stands for n dimensional, 10:18 meaning the array can have many dimensions. 10:21 Which is to say that these arrays can be multi-dimensional, 10:25 which I realize sounds a lot like a science fiction term. 10:30 We'll get to what that means exactly right after a quick break. 10:33 Hey, during that break, why don't you do a little reflection? 10:36 Specifically surrounding what we went over about datatypes and 10:40 choosing the right one and how it can save space. 10:43 I'm going to put my notes in here right above this call, 10:47 right above this call to zero. 10:53 So I'm going to press Esc, I'm gonna get back into command mode. 10:55 I'm gonna press above, then I'm gonna get this into Markdown. 10:58 There's probably a keyboard shortcut for that, share about that. 11:04 So I'm gonna say, About data types. 11:07 And again, just like before, I'll share mine right after the break. 11:13 Take some notes about what we learned here, see you soon. 11:16
You need to sign up for Treehouse in order to download course files.Sign up