All right.
I'm very excited it's time to start modelling it's time to start using machine learning to figure out
whether or not we can classify whether someone has heart disease or not based on their health parameter.
So in previous video we said that we're going to try out three machine learning models so we'll put
them out here.
We're going to try three different machine learning models one logistic regression to K nearest
neighbors classifier and three random forest classifier.
And if you're wondering how we came about these three because we went through this diagram the psychic
line machine learning map we've we followed this through based on our data.
We came across here we actually a good question you might be asking is hey I don't see logistic regression
we kind of covered that before where it said logistic regression that doesn't really make sense it's
got regression here.
So we're not trying to predict no more trying to predict a category why are we using it for classification
and I was pretty stumped too when I found out that we could use logistic regression for classification
you might be thinking even further back.
How did you even find logistic regression if it's not in here.
Well I was a bit confused at this too I was wondering why is it logistic regression listed here.
But after searching for go S.K. learn logistic regression and you might be wondering what if I didn't
even know to search for SBA loan logistic regression.
Well the way I stumbled upon this is I just went machine learning models used for classification problems.
I search something like that and then someone suggested logistic regression.
And so I decided to search logistic regression and I found it in here.
So I get learn it had an implementation I could of course build it from scratch but I'm more of a practitioner
right I want to apply models that someone else's built in the past so and then I read through and maybe
it wasn't in this logistic regression maybe it was in the user guide that's I think where it was so
logistic regression Here we go.
Despite its name is a linear model for classification rather than regression.
So this line here kind of clarifies the confusion in the name in logistic regression.
And of course you could read through all this.
You go to some map here of how it's actually implemented.
But the reason why we're trying it is because we're pretending that we've tried linear SBC we actually
have in a previous socket line video I'm going to say that it's not working.
So now we're trying we've gone through here we're not working in text data we're going to try k near
as neighbors classifier and ensemble classifier.
So that's that's how we've deduced these three different models and why logistic regression.
Well I've decided to throw it in here because it's not listed there and we want to kind of check it
out because if it's not on the the standard list of algorithms to try we might try and figure it out
anyway.
And again if this process seems like it's not really structured at all it's like your way.
We did Daniel find that machine learning model.
What do you even think of.
Again it's just from searching something like this.
Once we've defined that our problem is classification if you're trying to figure out which machine learning
model that you want to use it's something as simple as this and you could search for it and explore.
And if we go back to our keynote here that's what we're doing.
We're up to experiments.
We're trying different machine learning models.
That's enough talking actually.
One more thing and that's what I want to try and encourage you to do right.
Is that because there's so many different ways to do things and machine learning.
A lot of it is about just searching for the answer or not even the answer just asking questions and
trying to implement them.
That's what we're gonna do now.
So because we want to build three different models and because we want to evaluate them and because
we want to do a couple of experiments to see which one is the best we want to train them on the training
data and test them on the test data evaluate them what we might do is set up a little dictionary with
our models in it and then we'll create a function to fit and score our models so rather than rewrite
all the same code for feeding and evaluating our models set settle up in a function.
Yeah that's a that's a good idea.
Daniel let's do that all right.
So put models in a dictionary.
We're going to get models equals logistic regression and we're gonna go here and this is logistic regression
and then we want to go okay and then we're going to instantiate the class here.
So it's gonna be k neighbors we can probably place tab order complete let's be real and then we're gonna
go here random forest and this is going to be our trusty random forest classifier beautiful.
And then let's go create a function.
We want to fit and score the model so we want to train it need a function to train our models in the
training data and then evaluate them on the test data to fit and score models.
Death fit and score something simple it'll need to take out a dictionary of models and then we'll take
X train x test y train y test and we could put a little doctoring here just to make sure our code is
is legible.
If someone else was to use this function what would it do.
So fits and evaluates given machine learning models and then we'll tell it what parameters we're using
so models a dig of different so I could learn Shayne lining models.
What's X trained.
We know what X Chinese training data no libels x test is testing data No Labels and then why train is
training labels and then why test is test labels beautiful.
And now what should we do we might set a random seed set random seed even if it's within the function
to make sure that our results are reproducible and then we're going to go here.
We'll make a list to keep model scores or actually this is really better.
Make a dictionary.
That's what we want because we're working with dictionaries.
We want to go model scores equals an empty dictionary because we're gonna fill it up in a second we'll
talk through this function as we as we do it.
So loop through models.
This is where we're going to go for name model so name is the key model is the value of our dictionary.
So for key value in models to item so we're accessing the items of a dictionary with DOT items then
we're gonna go fit the model to the data.
That's where we go model dot fit X train y train.
So if fitting our model there.
So for each model it's going to fit it to the training data and then we want to evaluate the model and
append its score to model scores.
So this is how we're going to evaluate each model in one hit.
We're going to save the name of it to our dictionary.
Here is the key and then it's score as a value in model scores.
So model scores name.
So we're creating a key in the model scores.
Empty dictionary.
And then we're going to go equals model dot score.
So this is our model here.
So for example if it was logistic regression we've just fit it.
So logistic regression not fit X train y train.
And now we're appending logistic regression dots score x test y test to model scores under the name
logistic regression.
And if we've gone through that and you're thinking well that's a fair few steps in it it's only about
two or three but that's right.
When I first worked with these kind of functions I was eye color on used to just doing this line by
line but by being efficient in this project.
So we're gonna return model scores which will return a dictionary so we'll hit shift and enter.
Mm hmm.
And I wonder if this will work.
So basically what it's going to do is we're gonna take our dictionary of models it's gonna set up a
random seed it'll set up an empty dictionary and it's gonna loop through our models dictionary.
So for name model So for logistic regression logistic regression and model store items logistic regression
we'll pretend it's logistic regression not fit.
So selling the logistic regression model to find the patterns in the training data and then it's going
to create a key in our empty dictionary model scores with the score of how well our logistic regression
model performs on the test data a.k.a. using the patterns that it's found in the training data and then
it's going to return our model scores dictionary and then we're gonna see how each of our model performs
in the test dataset and our three models here we should have three different scores.
So I'm to leave it on a cliffhanger there and we're gonna evaluate our three models in the next video
so we'll see their.