**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Start a free Courses trial

to watch this video

Arrays can have multiple dimensions, let's see how to create and use them to our advantage.

#### Learn more

#### My Notes for Creating the Study Log

```
## About data types
* By choosing the proper [data type](https://docs.scipy.org/doc/numpy-1.14.0/user/basics.types.html) you can greatly reduce the size required to store objects
* Data types are maintained by wrapping values in a [scalar representation](https://docs.scipy.org/doc/numpy-1.14.0/reference/arrays.scalars.html)
* `np.zeros` is a handy way to create an empty array filled with zeros.
```

Multidimensional is such an [LAUGH] intimidating sounding word, isn't it? 0:00 It sounds futuristic and outer spacey, I mean what does many dimensions even me, 0:04 especially in the context of an array? 0:08 Well, the answer is kind of boring in comparison to how cool it sounds. 0:11 If you think about what we've been doing so far with our arrays, 0:15 we've basically been building a container where each slot represents something. 0:18 For instance, in the grade point average example, 0:21 each element represented a single year of school. 0:24 So we could say that this is the year dimension. 0:27 Now let's imagine that we wanted to track every student in my graduating class. 0:30 Now of course, we could add a separate variable for each student. 0:34 But wouldn't it be nice to store that all in the same variable. 0:38 Well, you can. 0:42 What you do is you add a new dimension, the Student dimension. 0:43 Now, we essentially have rows, where each row represents a student, and 0:46 each column here represents a year. 0:50 And would you look at this? 0:52 We have two dimension. 0:54 And really, a two dimensional array is just an array of arrays. 0:56 Right, I can access the student I want, like the third one here, and 1:00 then I could select which year I want. 1:04 I want the sophomore or second year. 1:06 There is a term that is used to discuss how many dimensions an array has. 1:09 This is called rank. 1:13 So this GPA example is currently rank 2. 1:15 Now, fun fact about two-dimensional arrays, 1:19 you remember how single dimension arrays are often referred to as a vector? 1:21 Well, a two-dimensional array is referred to as a Matrix. 1:25 >> [SOUND] Whoa. 1:28 >> Now, remember these are called 1:30 ndarrays. 1:32 And again, the n represents any number because you can have as many dimensions 1:33 and ranks as your heart desires. 1:37 Like we could add another dimension of School, 1:39 where each school had all of their students. 1:41 So that's 3D, and most likely you'll need [SOUND] to wear these glasses to see that. 1:43 Sorry, that's a bad joke. 1:48 But see, now we have an array. 1:51 I'll choose the first element, and that is our array that contains arrays. 1:53 All right, not as intimidating as it sounds. 1:57 But I bet you feel like you need some practice to fully grok it, right? 2:00 Let's do it. 2:03 So first, let's review our notes. 2:05 So here's mine. 2:08 About data types. 2:09 So by choosing the proper data type you can greatly reduce the size required to 2:10 store objects. 2:13 Remember we saw that and again, I put the link here 2:14 just to jump off the data type page whenever we need it to review later. 2:17 Think that's handy bookmark, sort of. 2:21 Data types are maintained by wrapping values in scalar representation. 2:23 Remember when we pulled that value out of the array, 2:27 it still knew the size that it was even though it kinda looked like it didn't and 2:29 it was a singular value, which is scalar. 2:33 Np.zeros is a handy way to create an empty array filled with zeroes, 2:35 like we did right here. 2:39 So we did this and we pushed the data type of the u unsigned integer 16. 2:40 Awesome. 2:45 Okay, so let's return to our gpa example, which is here. 2:46 I'm gonna click in here, put it in command mode, and add one below, so b. 2:50 And what I'd like to see here is a new array. 2:56 We will make students_gpas, so it's a list of students GPAs and 3:00 we'll make a multidimensional array. 3:04 So I'll say np.array and we're gonna pass in an iterable again and 3:07 we are going to make this iterable just be a list of lists. 3:13 So first we'll push mine through here so we have this. 3:17 I'll Copy that, paste this here. 3:21 And I'm gonna do a comma, and I'm gonna add another list. 3:26 So let's add another student, her name is Vlada. 3:29 She struggled at first. 3:33 English was not her first language but then she totally nailed it. 3:34 So she had a 3.2 her first year. 3:37 And then she started getting a lot better 3.8. 3:38 And then by the end was just sailing, 4.0, 4.0. 3:42 Awesome, and let's see, one more. 3:47 Let's add Quasey. 3:50 He was on fire all through school. 3:52 He started with a 3.96. 3:54 He got a 3.92 his sophomore year and he ended out with 4.0, 4.0. 3:56 Awesome. 4:02 So let's make sure that we grab the right data type for this. 4:03 So right now, the second parameter we're leaving empty, 4:07 let's say np., and then, what do you think that was? 4:11 Let's see, let's use this link one more time. 4:14 We'll click this, I already have it open here in the tab so, 4:16 this data type, let's say we, is that float64, we definitely don't need that. 4:20 We should be totally fine with just this float 16, so let's use float16. 4:25 So we will build our student GPA with a float16. 4:31 And then let's that a look at it too. 4:42 We'll say, students_gpas. 4:43 So, this is really just an interval. 4:46 It's a list of lists. 4:49 So each element of that list is another list, okay. 4:51 List of lists, multidimensional list. 4:54 So let's go ahead and take a look at it. 4:56 Awesome and it looks exactly like we want. 4:59 It's an array of float16 and it has all of the values there. 5:03 Now, it's possible that we would have run in to a bit of a precision error but 5:08 we didn't. 5:13 We haven't run that here but just in case because we did set a data type. 5:13 It might change precision. 5:17 Check the teacher's notes for more information on that. 5:19 So we now have a two dimensional array, right? 5:22 We have rows and we also have columns, right? 5:26 We have these columns here. 5:31 You can always look at the property on an array to see how many are there and 5:34 we have .ndim for number of dimensions. 5:40 You see that it has two. 5:43 If we wanted to see how many elements are in each dimension, 5:47 we can actually view that. 5:49 That's exposed using what is known as shape. 5:50 So you can say students_gpas.shape. 5:53 This is saying, three by four, and there are three rows and four columns. 5:58 Each element in this two pull represents the length of each axis. 6:07 So that's another term that you'll see used to describe dimensions. 6:13 The plural for axis is axes, not like what Paul Bunion had a bunch of, he had axis. 6:16 This axes, A-X-E-S. 6:23 So in our case, the first axis is students and the second axis is years. 6:27 Axes are zero-based, so axis zero is 3, and axis one is 4. 6:35 Now, one thing that might bite you if you aren't careful, 6:43 is that if you use the size property, so if we say students_gpas.size, 6:46 it's gonna tell you all of them. 6:52 Not just the first dimension. 6:55 If you wanna see just the first dimension, you can still use len, 6:57 and that will just show you how many students there are. 7:00 Three. 7:04 There's also a property that we can peek at called item size. 7:05 So again, students_gpas.itemize, and 7:08 that shows you how many bytes are being used for each item. 7:13 Which means now we can calculate how much space we're 7:19 taking up if we did students_gpas.itemsize times student_gpas.size. 7:24 We're taking up 24 bytes. 7:32 Which lines up with exactly what you would see if you were to use the whos command. 7:36 So let's say whos, and then if you follow a type ndarray, 7:41 it will show us all the ndarrays. 7:44 So here's the 4 by 4, this is the singular one that we had and 7:46 look that was 32 bytes and here is 3 by 4 and it's 24 bytes, it's already smaller. 7:49 Data types savings for the win, right. 7:56 This whos is a Jupiter Notebook special command, but you could also grab this 7:58 information and any information of a numpy object using a function named info, 8:03 np.info, and we can pass in students_gpas. 8:08 And if we take a look, we could see, there's the shape, 3 by 4, 8:13 there's our item size, and here's the type, and some more information. 8:15 All right, let's access some things. 8:21 So if I wanted to get access to the third student's record, I could just do this. 8:23 I could say, students_gpas [2]. 8:28 And that is the third student. 8:36 That's Quasey's grades. 8:40 And see how that returns me an array. 8:41 So of course if I wanted to, I could just chain off of it, right, 8:43 because it is an array. 8:47 Let's say I wanted to get the last year here, so I could just get this last year, 8:49 this last four here. 8:53 So I could say, students_gpas, 8:54 [2], so that will get us the student there, and 9:00 then I just get [3], and it should return back 4, awesome. 9:04 This is just one way of accessing this value in the two-dimensional space, and 9:09 remember, this 2D, or 9:14 two-dimensional example is synonymous with the term matrix. 9:15 Whoah. 9:22 Sorry for making mutlidimensionality seem a little less futuristic 9:25 than you probably anticipated. 9:28 It still sounds cool, though, right? 9:29 So make sure to drop it when someone asks what you're learning. 9:31 Just coding in a couple of dimensions. 9:33 I do hope it's pretty straightforward, and we'll get lots of practice working in in 9:36 dimensions through the rest of this course. 9:39 Now that you've got a handle on the main data type and 9:42 most of its terminology, let's go ahead and wrap up this first stage. 9:44 We'll go deeper in the remainder of this course, and 9:47 I'll show off some great shortcuts, 9:50 as well as how to use these powerful multidimensional arrays to your advantage. 9:51 For now though, why don't you make sure you reflect a bit 9:55 on multidimensional arrays and add some thought to your notebook. 9:58 And please remember to take a break and let all your learning sink in. 10:01 You deserve it. 10:05 See you in a bit. 10:06

You need to sign up for Treehouse in order to download course files.

Sign up