Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to NumPy!
You have completed Introduction to NumPy!
Preview
NumPy integrates with a whole slew of external packages and tools. Let's take a look at the plotting library, matplotlib
Learn More
My Notes for Routines in Action
## Common Routines
* Common [mathematical](https://docs.scipy.org/doc/numpy-1.14.0/reference/routines.math.html) [routines](https://docs.scipy.org/doc/numpy-1.14.0/reference/routines.html) are exposed so the formula can be abstracted away.
* [`mean`](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.mean.html#numpy.mean) is a [statistics](https://docs.scipy.org/doc/numpy-1.14.0/reference/routines.statistics.html) routine used to calculate the average.
* Reduction functions take a dimension and collapse it into a single value.
* These functions define an axis parameter, and you should remember that the function works across the dimension.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Just about every Python data library that
you come across is going to accept and
0:00
produce NumPy arrays.
0:04
Let's take a look at a common use case,
plotting our data graphically.
0:06
Let's go and get Matplotlib installed.
0:10
So here are my reflection notes, but
before we review them, let's multitask.
0:13
We need to install Matplotlib, so
let's flip over to our terminal.
0:17
And we will stop the server with Ctrl
+ C and do I wanna shut this down?
0:23
I'm gonna click yes to shut it down.
0:27
If we go back at the prompt and
we're gonna type conda install matplotlib.
0:30
And as that starts happening,
we are just gonna go and
0:36
flip over back to our notebook,
I'm gonna say OK, it's still up.
0:39
So it says connecting the kernel,
that's fine cause the server is down.
0:43
So let's take a look at this.
0:47
So some common mathematical
routines are exposed so
0:48
the formula could be abstracted away.
0:51
That was awesome.
0:54
So we saw mean was like that.
0:55
STD or standard deviation was like that.
0:56
A note here of reduction functions
take a dimension and it will collapse.
0:59
It will reduce across
that into a single value.
1:03
And that axis parameter is one
that's a little bit tricky.
1:05
Good thing we made note of
that with some examples.
1:08
Awesome, and
it works across the dimension.
1:11
So that's the common routines.
1:13
And I did another one,
1:15
another little section here
about the universal functions.
1:16
So here we go.
1:20
Universal functions expose a function
to reduce an array to a single value.
1:21
And there's also a function
called accumulate, which is nice,
1:25
cuz you can see things as it accumulates.
1:28
Let's see how we're doing with our server.
1:31
Shoot.
1:36
Yes, we want that to install.
1:37
I was trying to be a little too fast.
1:39
All right, cool.
1:40
Here, we're good.
1:41
So, let's go ahead and let's start that
notebook back up, jupyter notebook.
1:42
And we should be able to just do it
over here, if we come back here and
1:49
we say Kernel > Restart & Run All.
1:54
Here we go, back to it.
1:58
If you haven't used matplotlib lib before,
it's a wonderful, versatile, and
2:02
quite popular graphing library.
2:07
There is more in the teacher's notes
if you would like to learn more.
2:09
So I'm gonna come up here into my import
and I am going to add matplotlib.
2:11
So I will say import matplotlib.pyplot.
2:17
And keeping things lazy it's
called PLT typically as plt.
2:23
Awesome, so what can we do with that?
2:30
Well, what do you say we create a graph
that shows the student's GPA's matrix?
2:34
So let's get down to where that was at,
here we go.
2:40
Let's get rid of a couple of these.
2:43
[SOUND] And I'm going to come here.
2:45
We should probably convey the way that
each student performed over the year.
2:48
Now, choosing which type of graph
to get your point across the best
2:55
is an art form in and of itself.
2:59
Now, one effective way that
I've seen this expressed
3:01
is by using what is known as a box plot.
3:05
So, what a box plot does, is that it shows
the range, the maximum and the minimum and
3:08
the various quartiles and
the mean, all in one graph.
3:12
Well, the the best way to explore
anything is to just to explore it.
3:16
Let's do it.
3:19
Off of the py plot module, there is
a function named box plot, plt.boxplot().
3:21
And it takes an array, so
let's give it our array, so
3:29
let's say students_gpas, and
then I'm gonna go ahead and do plt.plot().
3:32
That will actually draw it here for
us in our notebook.
3:38
Check this out.
3:41
[SOUND] students_gpas is not defined.
3:48
I wonder if that's because I ran the line.
3:53
Let's try that one more time.
3:56
Restart & Run All.
3:57
It looked like it worked.
4:03
Sorry about that.
4:07
Drops us down at the bottom too, so
we've got to get back up to where we were.
4:10
There we go, there's our graph.
4:15
Look at that.
4:17
It looks like we're doing the right math,
but it's along the years, right.
4:18
So this is 1, 2, 3, 4 if you remember
what our, or students GPA's look like.
4:22
Fourth was all 4, and these was 4,
3.2, 3.96, let's just make sure, yeah,
4:28
that's what's happening here.
4:32
So, it's along the years
instead of along the students.
4:34
This is also a perfectly fine
way of looking at things, but
4:38
it's not what we're after.
4:41
So this bottom whisker here is the minimum
and the top whisker is the maximum.
4:43
Here's the interquartile and
this is the median here.
4:48
It's a bummer that the box plot
is graphing along the columns.
4:52
Wasn't there a way to switch
the axis of the array?
4:57
Like you can go from a 4 to 3 to a 3 to 4?
5:01
I'm pretty sure it was on that array
manipulation doc that we linked to in our
5:04
notes.
5:09
If you don't remember how to do this,
why don't you go and find that?
5:10
Give me a pause and see if you can't
find it in the documentation and
5:14
get this graph showing what we want.
5:17
Go ahead, pause me.
5:19
Did you find it?
5:22
It's transposed.
5:23
So .T right?
5:24
On all arrays it will actually transpose
and it will switch the order of arrays.
5:27
So lets run that one more time.
5:30
Beautiful, here we go.
5:32
So we got the three students, this is me.
5:33
I was all over the place.
5:36
Quesy doing good, like we said.
5:37
And here's Vlada doing well as well.
5:40
Got so much better from the start.
5:43
So what do you think we could do with
our 100 days of code study minutes?
5:45
I know one of the questions
that I get often is
5:50
how much time do you
actually spend doing it?
5:52
Because sometimes it's hard to
stop once you get on a roll.
5:55
[LAUGH] I'm definitely one to overdo it.
5:57
So let's go down to our study a minute,
so I'm gonna scroll down here.
6:00
Right about here.
6:05
One way to tackle this is
to group your data, right?
6:07
Now there's a type of
graph known as histogram
6:10
that will group your data into bins or
buckets and then display it.
6:13
It's pretty perfect for
what we need actually.
6:18
So remember that we dropped
the values that were under 60,
6:21
so we really just want values
that are greater than 0.
6:24
So the name of the function is hist,
short for
6:28
histogram, not like what a cat does
when you try to be nice to it.
6:33
So we're gonna put it here,
all of the minutes
6:37
that are greater than 0.
6:44
Actually, why don't you do that?
6:46
So I'm gonna leave a little to do here for
6:49
you, so we're gonna say pass in
6:55
a one dimensional array of all
7:00
the minutes that are greater than zero.
7:05
So you got that, right.
7:11
You can do that.
7:12
Go ahead.
Enter this hist function,
7:13
pass in a one dimensional array of all
the minutes that are greater than zero.
7:15
Go ahead pause me.
7:17
You got this.
7:19
How'd you do?
7:21
Did you get it?
7:21
So we can index our study_minutes
array with a Boolean array, right?
7:23
And we're gonna use broadcasting here,
7:30
cuz we're gonna broadcast this 0
using this comparison operator and
7:33
that will give us a one dimensional array
of all the values, and so we can say plot.
7:37
Bam, look at that.
7:45
There's some buckets that got
defined by our data, so 25ish,
7:48
maybe 26, 27 times,
we were in the 1st range of 60 or so.
7:52
And you could tweak this
actually a little bit more, but
7:58
I think that this gets
the point across pretty well.
8:00
There's times where I was down
here in the three-hour range.
8:02
But mostly, it was up here.
8:06
[LAUGH] And then there was even one
where we kind of crazy down here.
8:07
I hope that you are starting to see and
8:10
feel comfortable navigating around
the wonderful world of NumPy.
8:13
You'll see it everywhere.
8:17
Like everywhere.
8:19
And being able to use it and
8:20
understand the terminology surround it
will greatly help your data journey.
8:21
Matplotlib is just one of hundreds
of data related libraries
8:26
that work great with NumPy.
8:30
I am excited to see what you do with it.
8:32
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up