‫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 parameters.
‫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 so because we went through this diagram
‫the cyclone machine learning map where if 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 a number.
‫We're 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 isn't logistic regression listed here?
‫But after searching, if we go learn logistic regression and.
‫You might be wondering, what if I didn't even know to search for scale own logistic regression?
‫Well, the way I stumbled upon this is I just went machine learning models used for classification problems.
‫I searched 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 kept 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 has 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 being logistic regression.
‫And of course, you could read through all this, you got some math 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 SVC.
‫We actually have in a previous socket loan 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 with text data.
‫We're going to try k nearest neighbors classifier and ensemble classifiers.
‫So that's that's how we've deduced these three different models and y 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 you're where where
‫did Daniel find that machine learning model, what 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.
‫But that's enough talking.
‫Actually one more thing, and that's why 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 going to 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 fitting and evaluating our models, we'll 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 go models equals.
‫Logistic regression.
‫And we're going to go here.
‫And this is logistic.
‫Regression and then we want to go K and N, we're going to instantiate the class here.
‫So it's going to be K neighbors.
‫We can probably place tab autocomplete, let's be real and then we're going to 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 scale the model.
‫So we want it.
‫Train it.
‫I need a function to train our models in the training data and then evaluate them on the test data to
‫fit and score models.
‫So def fit and score something simple.
‫It'll need to take our dictionary of models and then we'll take x train x test, y, train, y test.
‫And we can put a little docstring 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 addict of different psychic learn machine learning models what's x train?
‫We know what X train is.
‫Training Data.
‫No Labels.
‫X Test is testing data.
‫No Labels and then y 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 probably better make a dictionary.
‫That's what we want because we're working with dictionaries here.
‫We want to go model scores equals an empty dictionary because we're going to fill it up in a second.
‫We'll talk through this function as we as we do 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 four key value in models item.
‫So we're accessing the items of a dictionary with dot items and then we're going to go fit the model
‫to the data.
‫This is where we go model fit, x train, y train.
‫So we're 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 than 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 nine.
‫So we're creating a key in the model scores empty dictionary and then we're going to go equals model
‫score.
‫So this is our model here.
‫So for example, if it was logistic regression, we've just fit it.
‫So logistic regression fit, X train, y train and now we're appending.
‫Logistic regression score X test Y test to model scores under the name logistic regression.
‫And if we're going through that and you're thinking, whoa, that's a fair few steps, it's only about
‫two or three, but that's alright.
‫When I first worked with these kind of functions I was like, Hold on, I'm used to just doing this
‫line by line, but we're being efficient in this project, so we're going to return model scores, which
‫will return a dictionary.
‫So we'll hit shift and enter.
‫Hmm.
‫And I wonder if this will work.
‫So basically what it's going to do is it's going to take our dictionary of models.
‫It's going to set up a random seed.
‫It'll set up an empty dictionary.
‫And it's going to loop through our model's dictionary.
‫So for name model.
‫So for logistic regression, logistic regression in models, dot items.
‫Logistic regression will pretend it's logistic regression dot 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 going to see how each of
‫our model performs on the test data set and our three models here, we should have three different scores.
‫So we're going to leave it on a cliffhanger there and we're going to evaluate our three models in the
‫next video.
‫So we'll see you there.