Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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/numpy1.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/numpy1.14.0/reference/arrays.scalars.html)
* `np.zeros` is a handy way to create an empty array filled with zeros.

0:00
Multidimensional is such an [LAUGH] intimidating sounding word, isn't it?

0:04
It sounds futuristic and outer spacey, I mean what does many dimensions even me,

0:08
especially in the context of an array?

0:11
Well, the answer is kind of boring in comparison to how cool it sounds.

0:15
If you think about what we've been doing so far with our arrays,

0:18
we've basically been building a container where each slot represents something.

0:21
For instance, in the grade point average example,

0:24
each element represented a single year of school.

0:27
So we could say that this is the year dimension.

0:30
Now let's imagine that we wanted to track every student in my graduating class.

0:34
Now of course, we could add a separate variable for each student.

0:38
But wouldn't it be nice to store that all in the same variable.

0:42
Well, you can.

0:43
What you do is you add a new dimension, the Student dimension.

0:46
Now, we essentially have rows, where each row represents a student, and

0:50
each column here represents a year.

0:52
And would you look at this?

0:54
We have two dimension.

0:56
And really, a two dimensional array is just an array of arrays.

1:00
Right, I can access the student I want, like the third one here, and

1:04
then I could select which year I want.

1:06
I want the sophomore or second year.

1:09
There is a term that is used to discuss how many dimensions an array has.

1:13
This is called rank.

1:15
So this GPA example is currently rank 2.

1:19
Now, fun fact about twodimensional arrays,

1:21
you remember how single dimension arrays are often referred to as a vector?

1:25
Well, a twodimensional array is referred to as a Matrix.

1:28
>> [SOUND] Whoa.

1:30
>> Now, remember these are called

1:32
ndarrays.

1:33
And again, the n represents any number because you can have as many dimensions

1:37
and ranks as your heart desires.

1:39
Like we could add another dimension of School,

1:41
where each school had all of their students.

1:43
So that's 3D, and most likely you'll need [SOUND] to wear these glasses to see that.

1:48
Sorry, that's a bad joke.

1:51
But see, now we have an array.

1:53
I'll choose the first element, and that is our array that contains arrays.

1:57
All right, not as intimidating as it sounds.

2:00
But I bet you feel like you need some practice to fully grok it, right?

2:03
Let's do it.

2:05
So first, let's review our notes.

2:08
So here's mine.

2:09
About data types.

2:10
So by choosing the proper data type you can greatly reduce the size required to

2:13
store objects.

2:14
Remember we saw that and again, I put the link here

2:17
just to jump off the data type page whenever we need it to review later.

2:21
Think that's handy bookmark, sort of.

2:23
Data types are maintained by wrapping values in scalar representation.

2:27
Remember when we pulled that value out of the array,

2:29
it still knew the size that it was even though it kinda looked like it didn't and

2:33
it was a singular value, which is scalar.

2:35
Np.zeros is a handy way to create an empty array filled with zeroes,

2:39
like we did right here.

2:40
So we did this and we pushed the data type of the u unsigned integer 16.

2:45
Awesome.

2:46
Okay, so let's return to our gpa example, which is here.

2:50
I'm gonna click in here, put it in command mode, and add one below, so b.

2:56
And what I'd like to see here is a new array.

3:00
We will make students_gpas, so it's a list of students GPAs and

3:04
we'll make a multidimensional array.

3:07
So I'll say np.array and we're gonna pass in an iterable again and

3:13
we are going to make this iterable just be a list of lists.

3:17
So first we'll push mine through here so we have this.

3:21
I'll Copy that, paste this here.

3:26
And I'm gonna do a comma, and I'm gonna add another list.

3:29
So let's add another student, her name is Vlada.

3:33
She struggled at first.

3:34
English was not her first language but then she totally nailed it.

3:37
So she had a 3.2 her first year.

3:38
And then she started getting a lot better 3.8.

3:42
And then by the end was just sailing, 4.0, 4.0.

3:47
Awesome, and let's see, one more.

3:50
Let's add Quasey.

3:52
He was on fire all through school.

3:54
He started with a 3.96.

3:56
He got a 3.92 his sophomore year and he ended out with 4.0, 4.0.

4:02
Awesome.

4:03
So let's make sure that we grab the right data type for this.

4:07
So right now, the second parameter we're leaving empty,

4:11
let's say np., and then, what do you think that was?

4:14
Let's see, let's use this link one more time.

4:16
We'll click this, I already have it open here in the tab so,

4:20
this data type, let's say we, is that float64, we definitely don't need that.

4:25
We should be totally fine with just this float 16, so let's use float16.

4:31
So we will build our student GPA with a float16.

4:42
And then let's that a look at it too.

4:43
We'll say, students_gpas.

4:46
So, this is really just an interval.

4:49
It's a list of lists.

4:51
So each element of that list is another list, okay.

4:54
List of lists, multidimensional list.

4:56
So let's go ahead and take a look at it.

4:59
Awesome and it looks exactly like we want.

5:03
It's an array of float16 and it has all of the values there.

5:08
Now, it's possible that we would have run in to a bit of a precision error but

5:13
we didn't.

5:13
We haven't run that here but just in case because we did set a data type.

5:17
It might change precision.

5:19
Check the teacher's notes for more information on that.

5:22
So we now have a two dimensional array, right?

5:26
We have rows and we also have columns, right?

5:31
We have these columns here.

5:34
You can always look at the property on an array to see how many are there and

5:40
we have .ndim for number of dimensions.

5:43
You see that it has two.

5:47
If we wanted to see how many elements are in each dimension,

5:49
we can actually view that.

5:50
That's exposed using what is known as shape.

5:53
So you can say students_gpas.shape.

5:58
This is saying, three by four, and there are three rows and four columns.

6:07
Each element in this two pull represents the length of each axis.

6:13
So that's another term that you'll see used to describe dimensions.

6:16
The plural for axis is axes, not like what Paul Bunion had a bunch of, he had axis.

6:23
This axes, AXES.

6:27
So in our case, the first axis is students and the second axis is years.

6:35
Axes are zerobased, so axis zero is 3, and axis one is 4.

6:43
Now, one thing that might bite you if you aren't careful,

6:46
is that if you use the size property, so if we say students_gpas.size,

6:52
it's gonna tell you all of them.

6:55
Not just the first dimension.

6:57
If you wanna see just the first dimension, you can still use len,

7:00
and that will just show you how many students there are.

7:04
Three.

7:05
There's also a property that we can peek at called item size.

7:08
So again, students_gpas.itemize, and

7:13
that shows you how many bytes are being used for each item.

7:19
Which means now we can calculate how much space we're

7:24
taking up if we did students_gpas.itemsize times student_gpas.size.

7:32
We're taking up 24 bytes.

7:36
Which lines up with exactly what you would see if you were to use the whos command.

7:41
So let's say whos, and then if you follow a type ndarray,

7:44
it will show us all the ndarrays.

7:46
So here's the 4 by 4, this is the singular one that we had and

7:49
look that was 32 bytes and here is 3 by 4 and it's 24 bytes, it's already smaller.

7:56
Data types savings for the win, right.

7:58
This whos is a Jupiter Notebook special command, but you could also grab this

8:03
information and any information of a numpy object using a function named info,

8:08
np.info, and we can pass in students_gpas.

8:13
And if we take a look, we could see, there's the shape, 3 by 4,

8:15
there's our item size, and here's the type, and some more information.

8:21
All right, let's access some things.

8:23
So if I wanted to get access to the third student's record, I could just do this.

8:28
I could say, students_gpas [2].

8:36
And that is the third student.

8:40
That's Quasey's grades.

8:41
And see how that returns me an array.

8:43
So of course if I wanted to, I could just chain off of it, right,

8:47
because it is an array.

8:49
Let's say I wanted to get the last year here, so I could just get this last year,

8:53
this last four here.

8:54
So I could say, students_gpas,

9:00
[2], so that will get us the student there, and

9:04
then I just get [3], and it should return back 4, awesome.

9:09
This is just one way of accessing this value in the twodimensional space, and

9:14
remember, this 2D, or

9:15
twodimensional example is synonymous with the term matrix.

9:22
Whoah.

9:25
Sorry for making mutlidimensionality seem a little less futuristic

9:28
than you probably anticipated.

9:29
It still sounds cool, though, right?

9:31
So make sure to drop it when someone asks what you're learning.

9:33
Just coding in a couple of dimensions.

9:36
I do hope it's pretty straightforward, and we'll get lots of practice working in in

9:39
dimensions through the rest of this course.

9:42
Now that you've got a handle on the main data type and

9:44
most of its terminology, let's go ahead and wrap up this first stage.

9:47
We'll go deeper in the remainder of this course, and

9:50
I'll show off some great shortcuts,

9:51
as well as how to use these powerful multidimensional arrays to your advantage.

9:55
For now though, why don't you make sure you reflect a bit

9:58
on multidimensional arrays and add some thought to your notebook.

10:01
And please remember to take a break and let all your learning sink in.

10:05
You deserve it.

10:06
See you in a bit.
You need to sign up for Treehouse in order to download course files.
Sign up