Hello everyone and welcome to the logistic regression project exercise solutions lecture and just lecture
.
We're going to be going over the solutions for the logistic regression project.
Let's go ahead and jump to the notebook to get started.
All right.
Here I am at the notebook.
I'm going to go ahead and get started off the tasks.
First thing you wanted to do was to import a few libraries.
You'll think you need or just import them as you go along.
I'm going to go ahead and import the ones that we usually import from Pine Pandurs and then also do
the visualization of libraries that we use such as matplotlib.
Pipelet as Piazzi import seaborne as S.A. and then map plot live in line so I can see the visualisations
in my Jupiter notebook.
Then we wanted to read the advertising that see as the file as an underscore data so you can do that
with PD read CXVIII
and then we want to check the head of add data and we can do that just with the head method.
Now we get these columns back.
So here we have the columns of consumer time on the site in minutes the customer age in years average
income of geographical area of consumer average minutes a day the consumer is on the Internet.
The headline of the advertisement that's the ad topic line the city of the consumer.
Whether or not the consumer was male the country of consumer and time which consumer clicked on an ad
if we go out and scroll to the right a bit that time stamp and then clicked on AD 0 or 1 indicating
clicked on AD.
OK next we want to use info and describe an AD data so you can go ahead and just say info and it returns
back to 1000 rows and columns and then if you want a Cisco description you can just go ahead and say
the scribe and you can see some statistical information about the numeric columns.
Let's go ahead and continue on with some exploratory data analysis with some plots.
We want to go ahead and create a histogram of the age not to do this.
You can just call directly off of the age column dot plot dot hissed or even just dot hissed and let's
go ahead and set bin's equal to 30 and check it out.
All right.
So there's a gram of the age looks like it's pretty normally distributed kind of around maybe 30 35
ish a little hard to tell.
All right the next plot we wanted to do was a joint plot showing area income versus age.
Let's go ahead and explore that.
You can just call Asinus that joint plots on the X-axis we want.
Age so I'll say age that's capital A.
And on a wire we're going to go ahead and say Area space income and then we'll start our data source
equal to add data and then we get this joint plot that appears like this.
And we can kind of see the relationship between the area income versus this person's age.
A little scattered but it does look like there's a trend that the only start earning stuff once you
start getting into your 20s which makes sense.
And then as you get older you hear it looks like your income maybe starts to increase but as you get
even older than that towards retirement either you start getting no income or source that drop.
So we can go ahead and spoil that a little more.
We can go ahead and do check out a joint plot showing Katie distributions of daily time spent on site
versus age and that's going to look like this is going to show you how it can do that and say Asinus
joint plot.
We want to pass an age as X and on why we want to go ahead and say daily time is spent on site which
is the name of the column data is add data.
And then let's go ahead and just check this out.
Notice if they don't specify kind we just get a scatterplot again.
So I want to go ahead and say kind is equal to Katie.
And here I get this nice Katie diagram and you can go ahead and specify colors if you want.
So you could add color equals red to see distribution in red.
All right.
Now let's go ahead and create a joint plot of daily time spent on site versus daily Internet usage.
So you can say Asinus joint plot x is equal to Daily Times Spens on site in y is going to be equal to
daily Internet usage where a data is equal to add data.
Let's go ahead and just explore that.
What does that look like.
Notice I'm just keeping the colors of default blue.
Looks like we can definitely see two clusters here as you go higher in daily Internet usage and higher
in daily time spent on the site.
You tend to belong to this cluster here and it looks like there's another cluster of this.
Well it looks like to be almost a circular pattern in this paired diagram.
And what we can do to explore is a little further it's just do this joint plot for every single numeric
column and we can go ahead do that with an s n s per plot we can say add data.
Now we also want to say Heugh defined by clicked on the add.
So I'm going to go and say Hugh is equal to click on Add.
Go ahead and run that and remember per plot's may take a while depending on how big your data set is
.
All right.
Here it is if the default colors and you can zoom in on this or set the notebook or style to whatever
you want if you want to increase size we can actually see here quite clearly certain relationships between
things such as age or income or daily time spent on site and whether or not they click on the ad and
you can go ahead and explore these on your own if you're interested in them.
Let's go ahead and move on to the logistic regression.
All I want to do is split the data into a train set and a testing set using trained to split in order
to do that we just say from S-K learn that cross-validation in poor train test split and then we can
always just say train to split do shift tab.
I always like to do scroll down to see the examples so you just copy and paste from the example and
make your life a little easier.
And then we go ahead and say X and Y test size going to be 0.3 and I'm going to set random say to 101
so you can get same results if you're following along.
Now we want to set X first and that's going to be add data.
And if we take a look at the add data again going and just make a cell here or a quick add data.
Let's just check out the very first row and see what we want to actually pass.
I definitely want to pass in all the numerical information because that's looking good things that I
can add in at this time are some string information such as the country or the city or the ad topic
line.
And I'd also have to grab stuff off the time stamp column in order for that to be irrelevant.
So we'll go ahead and just drop that as well.
So let's go ahead and just grab the columns that we're interested in for add data as features that's
going to be the Daily Times Spens on sites.
Next column we want is the age.
Also take the area income
and the daily Internet usage
and whether or not that person is male
and that's it.
Those are the columns we're going to be using.
And then for a while that's just going to be our target column which is an underscore data that they
actually click on the ad it's going to run that picture it works and looks like are good.
So that means we can train fit logistically Russian model on the training set.
So I can go ahead and say from S-K learn the linear model in poor logistically or ration we'll say log
model.
Go ahead and instantiate logistic regression.
I'm just using tab here.
And I will go ahead and fit that
to X train and y train to my training data.
I've created my model which means now I can actually predict values off of it and I can say predictions
is equal to log model predicts and passing my test data then it can create a classification report.
And I will say print's call classification report.
But that means I actually need to import it.
So I'll go ahead and do that first.
I'll say from Eskdale learned that metrics in poor classification report.
And we can forget to measure also do the confusion matrix.
So I can say orients classification report.
Passen white test.
And then the predictions and you can do the same thing if you wanted to prints confusion matrix y test
predictions.
And this gives you a bunch of information.
And in this case it looks like we're actually doing pretty well we're getting around 90 percent on the
precision recall and 92 percent around the accuracy.
Looks like we actually have a few mislabelled points which is not bad given the size of our dataset
.
Now you can also clearly see here that for some certain features the data in blue versus green here
is actually quite separate.
So that's a good indicator in certain things.
OK.
I hope you enjoy the project.
It should have been pretty straightforward However certain instructions were left kind of vague on purpose
so you would actually have to reference the lecture certain things such as the step on splitting data
was just left as a single line to split the data even though it had multiple steps included with it
such as choosing the x and y values.
But as you get more and more used to working with sikat learn and machine learning of Python those things
will become more and more natural to you.
All right.
Hope you enjoyed the project.
Thanks everyone and I'll see you at the next lecture.
Feel free to post any questions you have to the Q&A forms