Hello everyone and welcome to the Caine years Neighbors Project solution's lecture this lecture.
We're going to be going over the solutions for the Kinnear's Neighbors Project.
Let's go ahead and go to the project notebook to get started.
OK.
Here I am at the project notebook going to go ahead and type with the header and get a little more room
here.
Let me go ahead and import the usual libraries.
That's going to be Pansy's.
PD non-payers and P.
And then our visualization libraries which are going to be my lib pipe plot as Piazzi see Mourne as
asinus.
And then since I'm using an Egyptian or a notebook I want to go out and say map Cutlip in line.
Now let's go ahead and get the data we want to go ahead and read the KNM project data see as we file
into a data frame.
So I just say DFT equals PD read underscore C S V neck and begin typing out the file name click tab
and it will autocomplete for me.
I'll check the head of the data frame which is DF head.
And there we have it again.
How this data has quite a range.
So we're going to need to do some standardization.
Once we do some exploratory data analysis.
Now since this data is essentially artificial and you don't know the actual columns instead of doing
some exploratory data analysis of these columns you can just create a large pair plot with Seaborn and
then you can indicate the Heugh by the target class column in order to do that.
It's actually just one line.
You say SNS stopped pair plot DF say Heugh equal to the string target class and then you can set your
palette equal to cool warm if you want the color scheme to match up to mine.
But you can also just use the default color scheme.
Now keep in mind when you run this since there's going to be so many columns compared to each other
it will take a while.
And there it is.
Now we see our pair plot and what you can do now is maybe choose one of these squares that looks interesting
to you maybe one that has a lot of separation and you can go ahead and explore that one in more detail
.
For instance we kind of just browse around here.
It looks like there's pretty good separation based off of this column right here which is the T R A
T column so maybe you want to do a joint plot with TR 80 vs. maybe MGG or whatever.
But since we don't actually know what these column names represent we'll go ahead and just leave our
exploratory data analysis there.
Go ahead and move on to standardizing the variables we want to import standard scalar from sikat learn
Kensei from S-K learn dot and it's under the pre-processing family will say import standard scaler and
then we want to create a standard scalar object called scalar.
So I'll just say scalar is equal to standard scalar instantiated and then going to fit the scalar to
the features.
I'm going to say scalar thought fits the F drop targets class.
Access is equal to 1.
Because remember I just want to pass in the features not the entire data frame.
Want to go ahead and run that and then I want to use the transform method to transform those features
to the scaled version of those features and I'm going to go ahead and save that as scale.
Underscore features as equal to scalar thought transform and then passing the same thing it was that
earlier.
Just the features.
Target Class and then say x is equal to 1.
Great.
And then we convert the skilled features to a data frame and check the head of the data frame to make
sure the scaling worked.
Let's go ahead and do that.
I'm going to say DF underscore fee is equal to PD data frame pasand the scaled features as my data and
then the columns just like we did in the lecture.
Is going to be equal to DL columns everything but the very last column which is the target column and
then we can go ahead and request the feet's.
The head of that data frame scRunge just see the first three rows and there you have it.
Now you can see if it can compare these values.
They've all been standardized and scaled to each other versus if you scroll back up here.
These values were quite a bit larger.
Very different scales from each other.
Now that everything's been standardized we can go ahead and continue on.
If a train test split will go ahead and say from S-K learn the cross validation we want to import train
test split and then we're going to go ahead and say train to split.
And as always my trick goes shift tab.
Scroll down to see the example and go ahead and save yourself some typing by just copying and pasting
this will copy and paste that here and when we want to do is set test size to 30 it's up to you a test
size you want to do.
And also at random state equal to 101 although it doesn't really matter.
And we can create our x values remember x values is going to be our actual features and that's DFA underscore
feet's.
Or we can also just passing the array itself created skillz features works either way.
And now that we have that we can go ahead and say why is equal to the target column which was DPF target
class.
We run these two lines and make sure you run both of them.
These two cells that we have our testing data in our training data.
Now what we want to do is import Cayne neighbors classifier from sikat learn member that's going to
be from S-K learn neighbors in poor neighbors classifier and we're going to start off by creating a
can and model instance of and neighbors equal to one which means we're going to go ahead and say KNN
is equal to key neighbors classifier and underscore neighbors is equal to one.
And we're going to fit this can and model to the training data meaning we're going to say can fit to
X train and y train and we get that out.
And now it's time for some predictions and evaluations we'll go ahead and use a predict method to predict
values using arcane and model and X tests say pred is equal to Canan predict X. underscore a test and
we're going to go in and create a confusion matrix and classification report will say from S-K learn
that metrics in poor classification report and confusion matrix we can go ahead and print the confusion
matrix first by passing in Y test versus the predictions were shackled Pred.
And since I used a difference random stay I got a little bit of a different value.
You can go ahead and use whatever random state.
So don't worry if your results don't match exactly.
So my confusion matrix that just has to do with the way you split up the data here.
So if you just use the default random state which is the default random state using the solution book
you may have gotten a different answer for the confusion matrix but don't worry that's just due to whatever
number you pass in here or if you pass in a default value.
Continuing on let's go ahead and prints the classification report just using tab to help myself complete
some of the stuff I'll say will say Whitehurst and Fred run that and it looks like we're around 72 percent
precision recall and accuracy.
Let's go ahead and move along and see if we can use the Elbel method to pick a better k value.
We're going to create a for loop that trains various K and models with different k values then keep
track of the error rate for each of these models of the list and you can refer to the lecture if you're
confused on the step.
Basically we can do exactly what we did in the lecture which is create the error rate as an empty list
and then say for I in range once a 40 Kanon is Cain ABERS classifier where the number of neighbors is
equal to.
I will go ahead and fit that model to our training data.
Both Y and X train.
And then we go ahead and grab the prediction Canan underscore predicts loops that predict Excuse me
some method X underscore a test and then finally we'll say error rates append and we will append the
error rate which is the average of where the predictions do not match.
Why test.
We can go ahead and run that then it may take some time depending on how fast your computer is and we'll
go out and create the plot right here to pick a better value we'll say we'll see that figure.
Let's go and insert fig size equal to 10 by 6 fealty t that plot will do range 1 to 40 on the x axes
will do our error rates on the y axes and we'll go ahead and just add and some more arguments to make
it look a little nicer like we have here.
And those arguments are going to be things such as specifying the line color blue line style.
You can put a dash line there or you can just put a string the word dashed at the marker equal to 0
.
And I'm going to set the marker face color to red and I'm going to make the marker size a little bigger
10.
And then let's go ahead and put some labels here will say the title of this is the string error rate
versus K and just so we can read this will say X label is K and the y label is my error rates run this
.
Make sure the thing worked out OK.
Great.
And it looks like we get a plot that looks something like this.
And it also looks like the K value as it increases actually kept dropping even further and further.
So if we wanted to explore this a little more we can do is choose an even larger k value.
Let's go and explore that a little bit.
Show me what you could do if you get something like this in your real life scenario.
You can go ahead and increase this is something like let's say 60.
Run that and then change right here to 60 and run that again.
And now we can see that it begins to actually flatten now after around we can go ahead and say maybe
the late 20s so probably around 25 or even less than that 18 ish.
It looks like you're not really increasing that much accuracy for that much more value.
So let's go ahead and redo this.
Well go ahead just choose Keiko's thirties.
And that seems to be one of the lowest points versus the actual k value and we'll go ahead and retrain
the model of cable's 30.
Again it's totally up to you what you decide what you want to do.
The downside of using a higher K value is it's going to take longer to actually fit and train your model
.
But the upside is hopefully you get a reduced air at a certain point and then at a certain point increase
in that cave you're not going to get any more information as we could see.
Basically anything below 30 just kind of hovers around Point 1 7 error rate.
So going to say Kanon is equal to a neighbors classifier specify the number of neighbors to 30 fit that
model to X train y train create my predictions of that model predict that X tests.
And then I'm going to go ahead and print my confusion matrix and classification report which I can actually
just grab from a peer previously sung to the same thing here print my confusion matrix and scrolling
up again grab the classification report
and in between them I'm going to go ahead and just print a new line so we get a little bit of separation
here backslash.
And for a new line going and run this in it looks like I was able to do significantly better if a higher
K value I was increasing my accuracy and precision recall by about 10 percent 11 percent actually which
is pretty good for choosing a UK value.
All right I hope you enjoyed the project.
Let me go and do a quick run through of everything we just did before we end this lecture.
We had to read it in the data after we did our imports.
We did a little bit of exploratory data analysis not that much since this was basically anonymised or
classified data.
So you can actually tell that the columns stand for.
So it's not that interesting to explore then an important step was to standardize the variables using
sikat learns standard scaler and the transform into the scaled features and use that through your train
test split with sikat learns cross-validation and then you can use Kinnear's neighbors to actually build
your classifier.
We start off the locate value fit it check out our predictions with a confusion matrix and classification
report.
Then we use a for loop to choose a k value by doing the elbow method to choose a good k value choose
a reasonable value.
Retrain your model and check the improved results.
All right.
Hope you enjoyed the project.
Feel free to post any questions to the Q&A forums.
Thanks everyone and I'll see at the next lecture