‫All right.
‫So we're going to start off on the desktop like we always do.
‫We're going to come into terminal and I'll zoom in here a little bit.
‫Well, first thing is to get our environment activated, so we'll do a contact end list to remind ourselves
‫of where our environments are.
‫I've got three here.
‫The one we've been working with is in the sample project folder.
‫So I'm going to go conda activate and then copy this.
‫I could copy and paste it, but I'm just going to type it out because that's what we're in the habit
‫of doing.
‫So we use this slash Daniel slash desktop.
‫Slash ML course.
‫Well, typos galore today.
‫Slash sample project.
‫Your file path here might be different unless your name is general because otherwise great name.
‫And then here, once we've got our environment activated, we can see that this has changed from base
‫to this string here.
‫Wonderful.
‫That means our active environment is activated.
‫So then we'll go here.
‫Jupyter Notebook so we can get our notebook server loaded up.
‫Wonderful.
‫This is going to open our browser.
‫I haven't changed directory, so that's why I'm at the top folder here.
‫But that's alright.
‫I can change in a desktop ML course.
‫Sample project is where we've been working.
‫We've got our introduction to map port lib, num pi and pandas notebooks.
‫Wonderful.
‫But I'm going to create a new one because.
‫We're going through introduction to socket loan.
‫Wonderful.
‫We'll put a beautiful heading up the top here.
‫Introduction to socket loan brackets sky loan to remind ourselves we go.
‫This notebook demonstrates some of the most useful functions of the beautiful which it is beautiful
‫socket learn library wonderful.
‫We'll hit escape an m to change that into markdown we'll run it and maybe we'll put in here what we're
‫going to cover.
‫Just so just so we get a little bit of a highlight.
‫What?
‫We're going to cover.
‫So we're going to start off with zero because we're going to index Python style zero will be an end
‫to end socket learn workflow.
‫Wonderful.
‫And then we're going to deep dive into each step of this workflow.
‫So we'll look at getting the data ready.
‫Then we'll choose the right estimator.
‫Now estimator is another word in psychic learn.
‫You'll see in a second for machine learning model.
‫So when you hear the word model or machine learning algorithm or estimator, that's what scale own uses
‫for it.
‫They use the term estimate up for machine learning, model output slash algorithm for our problems.
‫Three will fit the model slash algorithm slash estimate and use it to make predictions on our data.
‫Wonderful.
‫And then four will.
‫Look at evaluating a model and then five, we're going to improve the model and then six will save and
‫load a trained model.
‫And then seven, why don't we put it all together?
‫Maybe putting it all together.
‫Sounds a bit better there.
‫Wonderful.
‫All right, let's dive into it.
‫Let's start with zero.
‫We'll create another heading.
‫So zero, an end to end.
‫Now follow along, if you can, because that's the best.
‫But I'm going to go a little fast, to be honest, because I'm kind of just talking through this as
‫I go.
‫So if you can't keep pace, that is absolutely fine.
‫You can potentially slow the video down or just revisit it a few times until you see what's going on.
‫But don't worry, you'll have access to all this code here.
‫Sorry.
‫An end to end socket learn workflow.
‫And we got the steps right here.
‫What we have to do.
‫So let's do it.
‫Step one, getting the data ready.
‫Let's see what that looks like.
‫We had a little bit of experience in the past few sections on using a heart disease data set.
‫So let's see if we can do that and maybe build a machine learning model on that heart disease data set.
‫All right.
‫So we'll do one, get the data ready.
‫Import pandas as paid.
‫Now remember this.
‫The end to end psychic loan workflow.
‫We're going to kind of breeze through these steps, so we'll deep dive into each of them as we go.
‫So we'll go.
‫Heart disease equals PD dot read us v now I believe I've moved everything into a data folder, so usually
‫we could just put the CSV name here.
‫But I've changed it up and now all of our CSV folders are within this data folder just to kind of clean
‫and keep our sample project folder nice and neat looking.
‫So that's why I've got the file path data here.
‫Heart Disease Dot CSA.
‫And then we'll go.
‫Heart disease.
‫We'll view it just to make sure.
‫Wonderful.
‫There's all of our rows there.
‫And so what we're going to do with this is use these columns all across here age, sex, CPE, whatever
‫these are, to try and predict the target, which is one or zero heart disease or not.
‫So the first things first is we need an X and you'll see this a lot in second line X is kind of the
‫features matrix, which is essentially these columns and then Y will be this column.
‫So let's see how we do that.
‫So we create X, maybe we'll put here create X, which is called features matrix.
‫Oftentimes it has other names, could be data, it could be feature variables, hot disease dot.
‫We want to every column except the target column.
‫So we need to go axis equals one beautiful.
‫And then we're going to create y, which is label, label matrix or labels.
‫Let's just call it labels beautiful y equals.
‫We want heart disease now we only want the target column.
‫The target.
‫Wonderful.
‫So we've created X and Y, so basically X and Y.
‫X is just the easier.
‫And why is this column here?
‫Wonderful.
‫Now, what's next in Psychic Learn when we go up here to choose the right estimate a slash algorithm
‫for our problems?
‫Well, our problem is classification because we want to classify whether someone has heart disease or
‫not.
‫So let's see that in action.
‫So two is choose the right model and hyper parameters.
‫Hyper parameters are like dials on a model that you can tune to make it better or worse.
‫So let's use a random forest.
‫So ask alone if you're wondering what is done you're talking about with random forests.
‫Well, we're going to see this in a little more depth in a future video.
‫But just so you're aware, just imagine random forests from cyclone ensemble import, random forest
‫classifier.
‫This is just a classification machine learning model that is the most you need to know for now.
‫So it's capable of learning patterns and data and then classifying whether a sample a.k.a a row is one
‫thing or another thing.
‫And so we'll instantiate that class using CLF, which is short for classifier in second line.
‫Other times you'll see it called model, but we're going to use CLF because we'll stick with the documentation.
‫So self equals random for us classifier.
‫Beautiful.
‫And we're going to we'll keep the default parameters, hyper parameters, keep the default.
‫Hybrid parameters.
‫So see if you can see what parameters your model is using using CLF target params.
‫So we'll see this.
‫Maybe take a little while to import.
‫In the meantime, we'll create the heading for this one.
‫So three is fit the model.
‫To the data.
‫I'm wondering why that's taking so long.
‫Maybe it'll finish while we're typing.
‫So, first of all, we need to train our model on training set and test it on a test set.
‫So we need to create that.
‫What is this?
‫Oh, my bad.
‫We need this.
‫It's not an attribute.
‫It's a method.
‫There we go.
‫So this is the hyper parameters.
‫All the little knobs on our machine learning model, random forest classifier.
‫We'll have a look at these in a future section, but we want to also fit the model to the training data.
‫Beautiful.
‫Let's do that.
‫So we want to go from scalar and model selection.
‫We need to split our data into training and test and we can do that with SRC learns train test split.
‫Train test split.
‫Wonderful.
‫And we're going to go X train.
‫X test.
‫Why train?
‫Why test equals train.
‫Test split x y test size.
‫We want test size to be 0.2 or whatever size you'd like to put in.
‫Essentially what this is going to do is split our data from X and Y into x train.
‫So training data, y, train, x test and Y test.
‫So testing data.
‫So if you recall from way back in section one, we usually fit the model to training data and then evaluate
‫it, see what it's learned on test data.
‫So data it's never seen before.
‫And what's this test size?
‫Well, that means that 80% of the data will be used for training because it's 0.2 because test size
‫is going to be 20%.
‫So if we had 1000 rows, 800 of them would be used for training and 200 of them would be used for testing.
‫But we're getting distracted here.
‫This is a whirlwind tour of scale.
‫Skyline We want to go CLF now.
‫We want to fit it.
‫We can do that with CLF fit x train.
‫This is basically saying a classification model, random forest, find the patterns in the training
‫data.
‫We'll go shift and enter.
‫Beautiful.
‫This is going to give us a little warning here because there's something that's happening.
‫So let's read a future warning.
‫The default value of an estimate is which is a parameter.
‫We go up here an estimate as warning that makes sense that it's giving us a warning.
‫We'll change from ten in version 0.20 to 100 in 0.22.
‫Hmm.
‫Well, let's see if we can just update that now.
‫So an estimate is we're altering our hyper parameters here equals 100.
‫Now, we shouldn't get that warning.
‫Beautiful.
‫We don't.
‫We can get rid of that.
‫Output by just putting that little semicolon.
‫Excellent.
‫Now, our model is fit to the data.
‫So what can we do?
‫Well, we can make a prediction, so let's do that.
‫Make a prediction.
‫This is still step three, by the way.
‫Y label equals c left predict and we have to pass an umpire.
‫Ray So let's do that.
‫Umpire Ray.
‫0234.
‫Let's see what happens.
‫Are we get an error?
‫NumPy is not defined.
‫We should have done this right at the top.
‫Let's do that.
‫Import numpy as np beautiful.
‫Now this should work.
‫No, it doesn't work.
‫Why doesn't it work?
‫Ah, the input.
‫So the shape isn't correct.
‫Now the reason this is, is because when you're trying to model on something that looks like x train,
‫which x train happens here.
‫We can only make predictions on arrays that look like this because that is what our model has learned.
‫So does this np array 0 to 3 for look anything like this?
‫No, not at all.
‫But what does look like?
‫That is x test.
‫Beautiful.
‫So what we can do is we can make some predictions.
‫So why Preds is a conventional name for for making predictions on test data equals CLF our model predict
‫x test.
‫Now what does this look like?
‫Why Preds?
‫We've got an array of zeros and ones and now what does this look like?
‫We've got y test.
‫We also have an array of zeros and ones.
‫Hmm.
‫I wonder what we could do with this.
‫Well, we can go step four, evaluate the model.
‫This is where we evaluate how good the predictions are or how well the machine learning model we've
‫just trained, our random forest classifier has done learning on the training data.
‫So why train?
‫Let's do on the training data.
‫Training data and test data.
‫This is what we're doing here.
‫So clefts score.
‫Wow.
‫It's actually done very well.
‫So this is going to return if we press shift tab the mean accuracy on the given test data and labels.
‫So we've passed it the training data.
‫So the model has done 100%, 1.0.
‫That's the maximum you can get with score on our training data.
‫Let's see how it's done on the test data.
‫Now, if it gets 100% on the test data, we might think that something's wrong.
‫Beautiful.
‫Excellent.
‫So what has happened here?
‫Well, the model has found patterns in the training data so well that it's got 100% Y because it got
‫trained on the features.
‫So way back up here it got trained on on these as well as the label.
‫So it had a chance to correct itself if it got something wrong, but it performs at 75% accuracy, which
‫means it gets three out of four predictions correct on the test data because it's never seen that data,
‫nor has it ever seen the labels.
‫All right.
‫So there are some more metrics that we can use rather than just accuracy.
‫So let's see that from Skyline Dot metrics.
‫Import Classification report because we have a classification problem.
‫We want a confusion matrix and one on accuracy score just to see another way of getting the same thing
‫that we have here.
‫So we'll start off, we'll go print you classification report.
‫And we want to compare.
‫What does this take?
‫We can't press shift tab because we have an imported it yet I believe it takes why tests so test labels
‫so true labels versus our predictions so what this is going to return is.
‫Spoiler.
‫Let's just see it.
‫So what this shows us is some classification metrics that compare the test labels to the predictions,
‫the prediction labels that we made up here with our model.
‫So using the predict function.
‫And then if we do the same again, we can do our confusion matrix.
‫So we get confusion matrix and we want y test first y prints.
‫Wonderful.
‫And then we can do accuracy score and we want why test why Preds.
‫Beautiful.
‫These are just some more steps that we can do to evaluate our model.
‫So next step would be we're not really happy with with how the accuracy score is doing there.
‫So we want let's go to step five, which is come back up here right to the top.
‫Improve a model.
‫Okay, let's do it.
‫Let's see how we can improve a model.
‫We might go try different amount of an estimate us, which is one of the hyper parameters a.k.a dials
‫on our machine learning model that we can churn to try and improve it.
‫So let's see this in action.
‫MP will start off with a random seed.
‫Well, our results are replicable and random seed and we want for I and range 1010 100 step can be ten.
‫We go here and we want print.
‫We'll give it a little communication trying model with I estimate us.
‫Wonderful.
‫Oh.
‫We've hit shift into too quickly.
‫It's like I'm trigger happy.
‫And again.
‫My goodness.
‫Okay, now then we want to go see elf equals.
‫We'll instantiate another model random forest.
‫Can you press tab classify beautiful we can thank you Auto complete now estimate is equals I wonderful
‫so this is going to loop through and try ten and then 20 and then all the way up to 100 estimates because
‫we're trying to figure out if we can improve our model by adjusting one of the hyper parameters, because
‫that's step five, improve the model.
‫So we'll go print if model accuracy on test set, we'll go model score X test because we want to evaluate
‫our model on the test data.
‫So it's going to learn on the training data and then evaluate itself on the test data.
‫And then we'll put a little bit of a percentage sign here, maybe limit it to two decimal places.
‫Does that make sense?
‫Let me read back this model accuracy on test set model score.
‫Yet we're just adding a little bit of communication here and then maybe we'll print a space here so
‫we can kind of see it.
‫Let's say this in action.
‫Trying a model with ten estimate is why didn't this work name model is not defined?
‫Oops.
‫See, I told you model can be used we need clef.
‫So clef, remember is short for classifier and let's do it.
‫How come it's not working again?
‫Random forest classifier is not fitted yet.
‫Call with fit.
‫Yes, we've instantiated, but we haven't.
‫How are we trying to score a model we've never, never fitted on our day yet.
‫We have to do this here.
‫See, this is great.
‫Live coding full of errors.
‫Wonderful.
‫Now, if we would look back through this, you would say that the best model with the best accuracy
‫if we go back is 20 estimate as well.
‫There we go.
‫We've gone from what was our original one, 75% to 83% by adjusting one of the high parameters of our
‫model to 20 instead of the default, which is ten currently, but it's going to be updated to 100.
‫And we're going really fast through here.
‫But what's a final step?
‫We go back up here.
‫Right to the top.
‫Save a model and load it.
‫Okay, let's do it.
‫So we want to go six, save a model and load it.
‫Now we can save a model with Python's Pickle Library.
‫So we're going to import pickle and then to save them on all go pickle or dump and we'll pass it our
‫model AKC for classifier and we'll pass it also we'll give it open with the file name that we want.
‫If I can remember this random forest model, one fancy name haha pickle.
‫And then we want to write binary with WB.
‫Wonderful.
‫So this is going to save our dump.
‫See, I'm not even following my own motto here.
‫If in doubt, run the code.
‫Now, this should have saved a model to file.
‫Let's go up and have a look.
‫If we refresh this.
‫Beautiful rainforest model there.
‫Now, what happens if we try to import that?
‫So we want to go.
‫Loaded model equals pickle dot load and we want to go open.
‫Pass it the file name random.
‫Can we do that?
‫Yes, we can.
‫Tab auto complete.
‫And now this time we need AB following out of that read binaries and then we'll go loaded model.
‫Dot score, x test.
‫Y test.
‫See what happens.
‫73.
‫Now, that should line up 73.7 with the last model that we tried up here.
‫And it does wonderful.
‫Now we've just zoomed through.
‫That took about 20 minutes or so to do an entire second line workflow with a lot of talking.
‫So we started off with one getting the data ready.
‫We saw our data frame of heart disease and we split it into X and Y, which is often what you'll do
‫in supervised learning problems in cyclone.
‫We're going to use the features here to predict the target.
‫Wonderful.
‫And then we chose the right model.
‫We kind of skimmed over a bit of how you'd actually do that.
‫But from experience, I know random forest classifier is pretty good at doing classification problems,
‫so we'll see how to do that.
‫And then we decided to use the default type of parameters.
‫Which are those little knobs?
‫You can turn on a model.
‫Now we saw how to fit the model to the training data, so a.k.a model finding patterns in the training
‫data.
‫And first of all, to even get our data from a big set into training and test, we use this little helper
‫function from cyclone.
‫Thank you, cyclone.
‫And then we made a prediction, or at least we tried to, and then we realized, hold on, our model
‫can't make predictions on things that aren't the same shape because remember, cyclone is built on numpy
‫arrays.
‫So if we pass it an array, this bad boy here that isn't the same shape as the data that it's trained
‫on now, it's going to throw us an error.
‫So we fix that error by asking our model to predict on X test, which in turn it predicted a label.
‫Y spreads for each of our samples, and then we compared our predictions to the Y test through evaluation.
‫Now our model got 100% on the training data, but only got 75% accuracy on the test data, which actually
‫still pretty good.
‫A coin toss for zero one would be 50%, so our models are halfway to being the perfect model.
‫And then we go here, we try a few more classification metrics, so we got a little bit more of an insight
‫rather than just accuracy.
‫We did a confusion matrix and use the accuracy score function.
‫And then step five, we tried a bunch of different hyper parameters a.k.a different estimators.
‫We found out that maybe 20 estimators is the best for our model because they've got the highest accuracy.
‫And then finally we saved a model and reloaded it, and it got the same score as our most recently trained
‫model, which was 90 estimated, 73.77.
‫And now this is a whole lot to take in.
‫I completely understand that we've gone through it relatively or actually quite fast, but what we're
‫going to do is break down each of these steps throughout the next few videos.
‫So if you want to go back through, try and put all the code that we've used into one cell if you can,
‫and see if you can get a model trained, can you get it saved?
‫Can you get re-import, have a little practise, but otherwise I'll see you in the next video and we're
‫going to check out how to get our data ready for using it with machine learning models.