Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to see about a new library which is called as Seaborn which will be used
for data visualization.
Now again Seaborn.
Similarly, like how we discussed with respect to matplotlib, I feel seaborn is much more better than
matplotlib and it has amazing features to create more data visualization graphs.
Uh, so in this video completely the discussion will will be with the help of Seaborn and we'll be seeing
multiple examples.
So first of all, let's go and see some brief introduction about Seaborn.
So Seaborn is a Python visualization library based on matplotlib.
So it is built on top of matplotlib that provides a higher level interface for drawing attractive and
informative statistical graphics.
Seaborn helps in creating complex visualization with just a few lines of code.
In this lesson, we'll cover the basics of seaborn, including creating various types of plots and customizing
them.
Now, before we go ahead and start, first of all, again, this is a library which will not be by default
available, so we have to probably go ahead and do the pip install.
So I'll just go ahead and do pip install Seaborn.
Now let's execute this code.
This is one way how you can do the installation.
The other way.
Is that what you can do in requirements.txt you have to make sure that you have the seaborn library
also, so that you can just right pip install minus r requirements.txt and automatically that particular
installation will also happen.
So here you can see our requirement already satisfied.
All the installation is done and successfully installed Seaborn 0.1 3.2, which is the recent version
right now with respect to Seaborn I guess, um, now we are going to go ahead and import the seaborn,
right?
So for importing Seaborn it is very much simple.
So we'll go ahead and right for import Seaborn as SNS okay.
So SNS is an alias.
And inside this particular seaborn there will be a lot of methods which we will be using.
Okay.
So let's go ahead and import this.
The first thing that we are going to basically go ahead with its basic plottings okay.
So here I'm going to basically write basic plotting With Seaborn.
Okay, let me do one thing since the diagrams will be quite bigger.
So I'll just hide my face so that you will also be able to see the diagram very much clearly.
So basic plotting.
And here what I'm actually going to do.
Let's go ahead.
And uh in seaborn there is a function which is called as load data set.
Right.
And when we use this particular function uh, here you will be able to see that we have a lot of inbuilt
data sets that are available.
Uh, like if you probably go into this particular link, you'll be able to see a lot of, uh, different
different data set dot CSV file.
So all you need to do is that go ahead and write your data set name.
Let's say that I want to use one of the data set which is called as tips okay.
So this will specifically be my tips data set okay I will go ahead and execute it.
And let's go ahead and print tips okay.
So here you will be able to see that I will be able to load it.
And here uh in this Tips data set you have uh some of the features like total underscore, bill tip,
sex smoker day time and size.
Right.
So we will try to consider this particular data Because this data has categorical data, it has numerical
data.
So all the different types of data is basically present over here.
Then let's go ahead and first of all create a scatter plot by using this particular data set.
So for creating a scatter plot like how we used to do in matplotlib like plt dot scatter plot, we can
also do it over here.
So before we'll go ahead and import matplotlib matplotlib dot pyplot as plt.
Okay.
And after this what I'm actually going to do I'll just go ahead and write SNS dot scatter okay.
And we are going to use the scatter plot here.
My x coordinate will be.
Let's say that I want to plot a total bill versus tip.
Okay.
So I can just go ahead and write in my x coordinate total bill okay.
And in my y coordinate I will go ahead and write my tip.
Okay.
And then finally the entire data is nothing but my tips.
So data is my third parameter that we specifically use in SNS dot scatter plot.
And I will just go ahead and give the title to this entire plot.
So let's say it will be something like scatterplot of total bill versus tip.
Okay.
So this will be there.
And finally I will just go ahead and write PLT dot show.
Let me go ahead and execute it.
So here you can see could not interpret value tips for y.
Okay.
Uh let's see.
Total underscore bill is there.
And tip it is right.
Not it is not tips.
So I'll just go ahead and execute it.
Now here you can see the entire scatter plot for this particular data.
And the x axis you have total underscore bill.
In the y axis you have tip.
And this is your entire scatter plot.
And this way you can basically see, uh, how the data is distributed.
And what is the relationship between total underscore bill and tip.
Okay.
Now this is my first plot.
Uh, coming to the next plot that we can actually use is something called as line plot.
Okay.
Now in line plot, I will just go ahead and write SLS dot line plot.
And here I will go ahead and use my x coordinate.
That is size.
Let's say this time I go ahead and use size.
My y coordinate will be total underscore bill.
So based on the people that who's probably coming in that particular restaurant.
Right.
And this this entire data set, if you probably go ahead and see this data set is of a restaurant,
um, here size basically means how many people are coming to have the food.
Is it a dinner time on which date is probably coming?
This was the total bill and how much tip it was probably given from this people.
Right.
So all those informations are specifically available over there.
Right.
So here I'm going to use size and total underscore bill.
And let's say my data is nothing but my entire Tips data set okay.
Our Tips data set okay.
And then I will just go ahead and write plot dot title.
And let's say this will be the line plot of total bill by size.
Okay.
So this is one information.
And if I go ahead and display plot dot show you'll be able to see that, hey, this is how my entire
line plot looks like.
And this is based on the distribution based on the size.
Right.
This is one two different different coordinates.
And we have basically, uh, combined it over here.
Okay.
Amazing thing about, uh, matplotlib and seaborn is that you can also work and create your categorical
plots.
No, categorical plots usually uses categorical variables like some of the example in this particular
data set.
Like sex is a categorical variable.
Smoker is a categorical variable.
De is a categorical variable.
Time is a categorical variable.
So all these are specifically categorical variables.
So I will just go ahead and create my categorical variable plot.
And for this we will be using SNS uh and in say in categorical plots.
Also first of all, the plot that we are going to see is nothing but bar plot.
Okay.
And similarly there are different different plots.
So I will go ahead and write SNS dot bar plot.
And inside this the first parameter is again my x coordinate.
Let's say based on day I want to see all my total bills, right?
So here I will go ahead and write.
My y coordinate is equal to total underscore bill.
And finally you have data is equal to tips.
Okay then let's go ahead and write plot dot title.
And here we're just going to write plot plot of total bill by day okay.
And finally we'll go ahead and write plot dot show.
Now we should be able to see the bar plot.
So here you can see on Thursday this was the total bill right.
Average total bill.
In short, you know, on Friday this was the total bill.
Like how many number of total bills Saturday.
This was it Sunday.
This was it.
Right.
So this is with respect to the total bills.
And we have seen with respect to this day over here.
Right.
So this is how you can basically go ahead and create a bar plot.
Now coming to the next plot that we are going to discuss is nothing but box plot.
Uh, box plot is also an amazing way of probably seeing outliers and all.
We'll just go ahead and discuss this.
So if I go ahead and write SNS dot box plot.
And let's say I will go ahead and probably use my x coordinate.
So x coordinate will be, let's say d.
The y coordinate that I'm actually going to use is nothing but total underscore bill.
And finally, my data is nothing but my tips if I'm using this and if I'm plotting it.
Here you can see this is what it is.
Uh, it is.
Right.
So this is the box plot.
This is nothing.
But this value is zero percentile, 25 percentile, 50th percentile, 75 percentile.
And this is 100 percentile.
And all these values that you will be able to see is basically outliers.
This is also outliers.
This is also outlier and each and every value.
So this is for Saturday.
This is my minimum bill that is zeroth percentile then 2550 75 and 100 percentile.
Right.
And these are some of the outliers that is available with respect to this particular box plot.
Similarly uh, along with the box plot, let's say I want to see the distribution of this particular
data.
I can go ahead and use my violin plot.
Okay.
So let's go ahead and show you violin violent plot for using a violent plot.
It will be SNS dot violent plot.
Here I will again go ahead and use my x, d and y is equal to total underscore bill.
And finally you will be able to see data is equal to tips okay.
If I go ahead and plot it and this is how you know my violent plot will look like.
So here you can see the distribution.
The mean is somewhere between 15 right.
15 over here.
Over here.
The mean is somewhere near to ten.
Right.
So you'll be able to see this kind of distribution, which is nothing, but it is a violent plot, right?
Violent plot just gives you an idea, like how the distribution of the data is with respect to the total
bill.
Debuys.
Okay.
Uh, so this was about violent plot.
Now let's go ahead and try to also go ahead and create histograms.
So with the help of Seaborn, also you can go ahead and create the histogram.
So for that I will be using SNS dot hist plot.
Okay.
Um here let's say I will go ahead and use my Tips of total underscore bill.
Okay.
And then let's say I want to go with uh how many bins.
So the second parameter is basically bins.
So I will go ahead and use ten bins and also say hey kernel density estimator should be true.
So that I get the shape of the histogram.
I'll just not be creating shape.
I'll also show you how it will look like okay.
And here if I go ahead and execute it here, you can see if I just make it KDE as false.
You'll be able to see I'll not be able to get that curve right.
This curve, if I get it, it is basically smoothening the histogram that is actually shows you that
how your data is distributed.
So let me just make it true.
And this is how your data is basically distributed.
And total underscore bill is basically there.
And with respect to the count also you can see the entire distribution.
Okay.
Uh, this was one of the example uh, let's say that I just want to focus on this kernel density estimator
and show this particular path itself.
I can go ahead and construct my KDE plot.
Okay.
And again, guys, at the end of the day, all these things will be super beneficial when you are doing
visualization, when you are probably understanding things like how your data is right.
All those things you will be able to understand through this.
So I will go ahead and write CD plot and with respect to this, let's go ahead and write tips.
And this will be nothing but total underscore bill comma.
Let's say that I want to also make sure that I have some shade in this.
So this is what shade does right.
So same thing right.
This is the same curve over here.
Also you'll be able to see and here you are able to see these things right.
Uh shade is a now deprecated in favor of fill.
Right.
So I can go ahead and write fill is equal to true okay.
Done.
So now I will not be able to get a warning.
Perfect.
So I hope you are able to understand one more, uh, amazing plots that is most commonly used in Seaborn
is something called as pair plot.
Now, what exactly is this pair plot?
If I want to see the relationship between all the variable to different different variables.
Right.
Let's say here I have 4 to 5 features.
So if I probably go ahead and show you, let's say if I go ahead and write Sns.pairplot and if I just
go ahead and write tips okay.
So this will basically be my data set right.
Here you'll be able to see the relationship between each and every other feature like size to total.
Bill is this relation size to tip is this relation size to size is.
In this relation tip to total bill.
You can see this is the relation.
Tip to tip will also be coming as a histogram.
All right.
And here you can see tip to size is basically in this relation.
Here you can see total bill to tip is basically having this relation.
So it is amazing to see that how easily you are able to see the relationship between each and every
feature to its other cell like other feature, what is the relation over there?
And this will be very handy.
And remember for this categorical variable is not taken into consideration, so you will not be able
to find out days and all.
So for this you specifically need to have a numerical variable.
Okay then let's go ahead and do one more thing.
Something related to correlation with the help of heat map, right?
Heat map is one amazing functionality in Seaborn.
So here, let me just show you that with the help of heat map.
So first of all, I will go ahead and write my correlation.
I'll go ahead and write tips.co rr correlation okay.
Then I will go ahead and write SNS dot heat map.
And let's first of all use this correlation.
See if I go ahead and just execute this correlation.
You'll be able to see that this will be my output okay.
Um tips dot correlation could not convert a string to float.
No.
Okay.
Let's see why that problem is basically there.
Because if uh, again uh, if I'm using a categorical variable, uh, that is the reason it is showing
us this.
Let me do one thing.
Let me just go ahead and use all the, uh, numerical features.
So first of the numerical feature that I am going to use over here, let's go and see this.
So this is my tips data set okay I'll use total bill.
So this will be my first feature.
Then I'll create like this.
Okay.
List.
Then the next one is basically with respect to tip.
So I'll go ahead and write tip.
And my third feature that I'm going to basically write is size.
Now if I go ahead and see the correlation you'll be able to see this particular correlation.
It is nothing, but it is the relationship between one feature to the other feature.
You can see if total underscore bill is related to tip.
That basically means if it is, if total bill is increasing by one unit, that basically means tip will
also be increasing by 0.67 units okay.
Similarly tip to tip.
Tip to total bill.
All this particular relationship is there.
Now heatmap will basically give you some colorful visualization for this particular correlation.
So let's see that how to probably do this.
So I'll just go ahead and write SNS dot heatmap.
And here I'm going to basically use the correlation and say hey give the annotation as true.
So that whatever information that you really want to display, you can go ahead and display and we can
use some color map.
There are different different color maps.
Again you can probably go ahead and see the seaborn documentation.
But let's say that I'm going to just use some cool warm color okay.
Uh, this is one of the feature that you can actually use.
And according to you you can go ahead with this.
Right.
So here you can see based on the correlation, wherever there is red it is one.
So here is what correlation is right between 0.5 to 1.
If you go towards this particular correlation this color that basically means one variable is really
really cold related to the other variable.
Let's take in the size.
Size to total bill is 0.6.
Tip to total bill is 0.68 right.
So it all has a positive correlation there.
So this was some of the good amazing examples that you have actually seen.
Okay.
Now let's do some customization of a plot okay.
And uh the best way to probably do the customization is obviously using matplotlib along with seaborn.
So what I will do, first of all, I will go ahead and import pandas as PD.
Okay.
We'll take some data set.
Sales underscore DF, which we are referring to.
I'll go ahead and write pd underscore read underscore csv.
PD dot read underscore csv.
And this will basically be my sales underscore data dot csv.
And then I will go ahead and write sales underscore df dot head.
Okay.
So it will give me the top five records.
Okay.
Now what I'm actually going to do I'll I'll basically go ahead and plot total sales by product.
Okay.
Let's say so first of all I'll go ahead and create my product figure plot dot figure.
Here I'm going to specifically use fixed size.
And let's say the size that I'm actually going to give is ten cross six in terms of inches I guess.
And here you'll be able to see I'll be able to use a bar plot as SNS dot bar plot and my X coordinate.
Let's see which will be my x coordinate.
X coordinate let's say is with respect to product category.
Okay.
So I will go ahead and probably use this product category and my y coordinate y coordinate will be nothing,
but it will be revenue.
If you see over here, it will be total revenue.
Okay.
And then I can actually go ahead and give my data, which will be nothing but sales underscore DF.
And then I'll also be using an estimator.
See.
Now this is very much important because in matplotlib we had to probably apply aggregate functions.
Right.
But here you don't have to do that.
There is a feature which is called as estimator.
And inside this you can put any aggregation function like sum um mean whatever things you really want
to use.
Okay.
So if I go ahead and do this and along with this, let's go ahead and put some title x label and y label.
So if I go ahead and execute it here, you'll be able to see that how beautifully we are able to get
it.
With respect to electronics this is the revenue home appliances.
This is the revenue clothing.
This is the revenue books beauty products sports.
And obviously you can see there is a craze with respect to electronics.
And this is what is the outcome that you can probably get from this entire data.
Right.
And this data is quite huge.
And quickly, just by importing and doing all these things, we are able to do so many amazing things
over here, right?
So, uh, in conclusion, uh, again, you can try with region, you can try with different different
things as you want.
Right.
So I can probably go also go ahead and just copy this entire thing.
Let's say here I'm going to probably use region okay.
Now based on region, all I have to probably do is that copy this feature and paste it over here.
That's it.
Total sales by region right.
So here x label will be region if I go ahead and execute it.
See North America has the highest sale then you have European region.
So second is Asia and third is European region.
You can probably see this.
So I hope you were able to understand this particular video.
I hope you like it.
I hope you have learned a lot of things.
This was it from my side.
I will see you all in the next video.
Thank.