Seaborn's Web site looks like this. Seaborn is a Python module that's actually based on matplotlib 1
but it has some nice extra features. You can think of seaborn like matplotlib on steroids. 2
If matplotlib was Lance Armstrong then Seabourn is still Lance Armstrong. 3
Lance Armstrong on the Tour de France. 4
So let's import the Seaborn module at the top of the notebook as sns. I'm going to write 5
"import seaborn as sns" and hit Shift+Enter. In the cell below the histogram we're gonna make use of one 6
of seaborne functions. 7
We're gonna make use of the distplot function, so "sns.distplot(data[ 8
'PRICE']) and then Enter, "plt.show()". Seaborn's distplot function 9
only needs a single argument, namely the data it's meant to plot and we're gonna plot the target values and 10
we're gonna see how this compares with 11
matplotlib's histogram function. So let me hit Shift+Enter 12
and here we can see that straight out of the box we get a very beautiful graph. We get both a histogram 13
and an estimate of the probability density function. 14
The probability density function is the darker squiggly line that's superimposed on top of the histogram. 15
This is the line that estimates the distribution of the data and I think this is a neat little addition 16
that Seaborn provides as an add on to the histogram. 17
Now of course we can make the seaborn plot look a lot more like matplotlib. 18
So for starters I could size it differently. 19
So I'm going to take this line of code here. 20
Copy it. 21
Go down here. 22
Paste it, press Shift+Enter can see that's a bit larger but easier to see. But we can also modify the 23
number of arguments 24
this distplot here takes. So it also has the bins argument that we can give it and I can give it 50. 25
So now this ties out with what we see up here from matplotlib, and then we can also show and hide the two 26
bits of the graph here. 27
The PDF or probability density function and the histogram. 28
So if I come up here to the arguments and I put "hist = False", with a capital F, and hit Shift+Enter 29
it hides the histogram and we're just left with the probability density function which is the estimate 30
for the distribution. 31
And of course we can also do it the other way around. 32
So if I write "kde = False" and then change the hist argument to True, then we're hiding the probability 33
density function and just showing the histogram. And in terms of styling you can also grab a different 34
color here. 35
So if I want to go with a yellow palette for example pick this hex code, go back here, 36
and then add the color argument as a keyword and paste the hex code in, hit Shift+Enter and then we'll see 37
it like so. Now in retrospect I don't think this was actually a wise choice. 38
This is very, very low contrast. 39
I might have to go for something like this which is a bit more Amber and substitute that for the yellow. 40
And this makes the histogram a lot more clear. 41
All right so I think this is a really good time for a challenge. 42
See if you can investigate and visualize two other features of the dataset and I also have a question 43
for you. 44
How many rooms do you think that the average property in Boston has? 45
What do you think? 46
What's the total number of rooms for a property in Boston? 47
So including bedrooms, living rooms, bathrooms etc. 48
Have you made your guess? 49
Okay, so for this challenge we're gonna be checking the answer. 50
We're gonna have a look at the RM feature. 51
Can you write the Python code to plot this on a histogram and visualize the number of rooms per dwelling 52
and then figure out what the average number of rooms is for a property in Boston Massachusetts? 53
I think this would be fairly straightforward. 54
Uh you can use either matplotlib or seaborn to generate your histogram but 55
maybe have a think about what you would expect beforehand. 56
Ready? 57
Here's the solution. 58
So what I'm gonna do is I'm going to take this cell here and I'm going to copy the cell, come down here 59
and "Edit", paste the cell and then what I'm going to do is I'm going to change the code here, so I'm gonna 60
substitute RM for price. 61
I'm going to delete the bins. 62
I think this is no longer what we want. 63
I'm also gonna pick a different color so maybe I'm gonna go for teal. It was dark teal here, just to differentiate 64
the chart a little bit and then I'm going to change the x label here, because we're no longer looking 65
at the price we're looking at 66
then number of rooms. In fact this is the average number of rooms. And I'm going to hit Shift+Enter 67
and the chart that I get back looks like this. We can see that the highest bar is around the number 6. 68
In other words, most of the properties in Boston have around 6 rooms. 69
I don't know about you but that actually seems pretty big actually, because if you think about it, 70
bedroom, living room, kitchen, bathroom, 71
that adds up to 4. If you add another bedroom, 72
then you have 5, but the average is 6. 73
So I don't know if they have a linen closet that they're counting or they have two bathrooms or three bedrooms. 74
Now of course we don't know anything about the size of the rooms, but still I'm going to venture a guess 75
that six rooms on average doesn't seem to be too shabby. 76
I certainly wouldn't mind living somewhat with 6 rooms. 77
That's palatial by London standards. 78
Now we just eyeballed this graph to figure out the average number of rooms. 79
What if we wanted to know an exact number for the average? Well pandas 80
also has you covered for that. 81
So what we can do is come down here and access our our RM data series, so "data[' 82
RM']", put a dot after it, and then call the mean() function. So "data['RM'].mean()". 83
So here we're calling the mean method on our series object and hitting Shift Enter, 84
we see that the true average is 6.28.