Hello everyone and welcome to Matrix plot's lecture with Seaborn in this lecture we're going to learn
how to create matrix plots primarily heat maps using Seaborn.
Let's go ahead and jump to Jupiter notebook to get started.
OK.
Here I am at the Jupiter notebook.
I went ahead and said inport seaborne as Asinus and then set map plot live in line.
Then I loaded up two data sets.
I've loaded up the tips data frame or data set using Esson as that load underscore data set and then
passing in the string tips.
And I did a similar thing for the flight data set using S and S that low data set and then passing in
the string flights the tips data that we've seen before.
Here's the head of it.
But the flight data set we haven't.
So let's take a quick look at it.
The flight data set primarily just shows the number of passengers that flew in a given month of a given
year.
Now let's talk about heat maps which is our primary way of showing some sort of matrix plot in order
for a heat map to work properly.
Your data should already be in a matrix form.
And what I mean by matrix form is so that the index name and the column name match up so that the cell
value actually indicates something that is relevant to both of those names.
What I mean by that for instance if I take a look at the tips data frame right now I have a total bill
which is some sort of label or variable and if I look at one of the values in the cell it's 16 dollars
and 99 cents.
However the row is not an actual variable in order to get this into matrix form.
I need to have both variables on the columns and the rows.
I can do that through many methods usually through a pivot table or trying to get correlation data.
We'll go ahead and do correlation data first.
If I call it tips stop c o r r close parentheses.
Now we have a matrix form of some sort of data here.
And what you mean by that is now the column and the index variable names actually indicates a relevancy
to both the column and the index name for the cell where they match up.
And this is the matrix form.
You're going to need your data in in order to call heat map on it and then it's actually just as simple
as calling Asinus dot heat map and then your data.
Let's go in and save Tipps thought core as TC and then pass it into heat map.
And here we can see a heat map or a heat map really does is it colors in those values based on some
sort of gradient scale.
So if I take a look at my original TCE data frame it's essentially just set these numbers to a gradient
on a color scale and then fill that in.
And that way if you have a very large correlation data frame or some other matrix form data you can
easily tell what the numbers are what the values are relative to each other using a heat map.
And that's the main primary use for a matrix plot.
Other useful arguments you can pass in here to heat map besides just your data is and rotation so you
can say A and OT as arguments equals true and they will annotate the actual numerical values that belong
in each of these cells.
You don't just have to go off of color but you can also go off of the actual value here.
And then you can also specify as See map the actual color mapping you want will go over color maps and
the options later on in the seemore lecturers for instance showing an example.
Cool warm as a string that verifies a color map.
And here you can begin to see the color map.
Cool warm on the same data.
Now let's revisit the flight's data then go ahead and type in flights and we can see we have all this
year this month this passengers and I need to get this into matrix form meaning I need to somehow convert
one of these variables appear to be the call or the road names the actual index.
So I want one of these variables either Year Month or passengers.
Let's go ahead and say month to be the index.
So I'm going to say the index is equal to month and then I'm going to go out and set the columns equal
to the year and then the only thing I have left to do is specify the values and that's going to go ahead
and be the passengers column.
Go in and run this and see what this looks like now.
OK.
Now I have my data in some sort of matrix form.
You can do this with Pivot Table quite easily depending if your data lines up correctly.
Now you can see I have a bunch of numbers here that correlate well with not just the column variable
but also the index variable.
So I can see here in 1952 for instance in April there were 181 passengers.
Now what I want to do is use heat map to quickly visualize this data.
I'm going to go head and set this equal to FP for flights pivot and then just call S and S heat map
on F-P.
And here we can see the heat map for the pivoted tables of flights.
And now I can see based on a color gradient the data in a lot clear of a format.
So I can easily tell which month of which year had the most flights and it makes sense that gradually
as the years go further or forward in time you get more flights then that makes sense because as time
goes on more people are on airplanes more flights are occurring and you can also see based off the month
that popular month travel is usually during the summer months June July and August at least for us data
.
Now what you can also do is add in arguments such as sea map and let's go in and show another example
of a sea map one that's useful as magma.
And now we can also see from dark to light the actual colors here the variation in other argument you
can pass and a heat map which sometimes people like to use is like color and line which is
basically right now in this heat map.
All these cells are touching each other directly but sometimes you may want some separation.
So you can specify line withs to be a little thicker.
We'll go in and put in one there and you can auspice Feitel the line color.
We're going to go and specify it to be white.
Now if I run this I get these lines in between each cell.
And sometimes that's a little easier to read.
You can make these lines really thick if you want more separation and you can also change the color
of the line.
So for instance if I go back to maybe another seam up another one sometimes people use like we show
before us cool warm.
And this is what it would look like maybe you want something that's not white.
You can also choose like black for instance.
So really you have a lot of customization here as far as how you actually want to separate this.
Let's go to remove everything and just show the original heat map here.
Now let's talk about the second Matrix type plot in seaborne which is the cluster map and the cluster
map is actually going to use hierarchal clustering to produce a clustered version of this heat map.
Let me go to show you an example here we have the original heat map but now I'm going to call Asinus
cluster map on F.P. and notice what we have here.
We have the data on it need to zoom out to show this completely.
But now we have the data and these little hierarchal clusters and this basically tries to cluster columns
and rows together based off their similarity.
And this suddenly shows the years and the months and clusters and the months here on the y axis you
can see are no longer in order.
They go September June August July October April.
And the reason for that is because they've been clustered to most similar months being put together
and the same thing for years you can see certain years are now out of order.
It goes 1949 1950 1953.
That is basically clusters of information to try to show columns and rows that are similar to each other
so I can see now that 1959 and 1960 are very similar to each other.
And so our August and July.
And you can see the different levels of the hierarchy of the clusters based off of these kind of tree
diagrams but on the y axis and the x axis and now you can really begin to infer information from a plot
in order to make this even more clear what we can do is change the color mapping.
So see map equal to the string.
Cool warm again.
And now we can really see the clusters so you can see that there's a cluster between August and July
very clearly and something else you can do is standardize the scale.
So right now we're still on passengers from 0 to 600.
But if you wanted to normalize this you can passen arguments called standard scale and input 1 and this
would normalize this from 0 to 1 and you can really see based off a normalized scale the similarities
of the clusters based off the coloring.
So it looks like this November January and February is kind of early winter months are much closer to
low passengers and high passenger count is mainly in these summer months.
OK.
I encourage you to go ahead and explore the documentation for cluster map.
It is something you're interested in but you may want to wait until we actually get to machine learning
clustering algorithms so we can better understand what's actually going on with the mathematics behind
these methods.
BERNANKE I just think of it as an interesting method to just infer a little more information from a
normal heat than a heat map will display things in the order you put them in a cluster map will cluster
things in an order so that similar groups are close to each other.
All right thanks everyone and I'll see you at the next lecture