Oh right.
All right.
So we're going to start off on the desktop.
Likely always do.
We're going to come into terminal and I'll zoom in here a little bit.
First thing is to get an environment activated so into a contact and 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 photo so I'm going to go
conduct activate and then copy this I could copy and paste it but I'm just gonna type it out because
that's what we're in the habit of doing.
So it uses slash Daniel slash desktop slash M.O. cause or typos galore today slash sample project your
file path here might be different unless your name's Daniel because otherwise great name.
And then here once you've got our environment activated we can see that this is changed from base to
this string here.
Wonderful.
That means our contact by environment is activated.
So then we'll go here.
Jupiter notebook.
So we can get our notebook server load it up wonderful this is gonna open our browser.
I haven't changed directory so that's why I'm at the top folder here.
But that's right.
I can change in a desktop email course sample project is where we've been working we've got our introduction
to map port lib num pi and pen is notebooks.
Wonderful but I'm going to create a new one because we're going through introduction to so I could learn.
Wonderful.
We'll put a beautiful heading up the top here.
Introduction to socket line brackets SDK learn to remind ourselves go.
This notebook demonstrates some of the most useful functions of the beautiful which it is beautiful.
So I could learn library wonderful will hit escape.
And aim to change that into markdown we'll run it and make 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 gonna start off with zero because we're going to index Python style zero will be an end to
end.
So I can't 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 will choose the right estimate.
Now estimate it is another word in so I can't learn you'll see in a second for machine learning models
so when you hear the word model or at machine learning algorithm or estimate.
That's what SBA loan uses for it.
They use the term estimate out 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 and 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 well let's dive into it.
Let's start with zero will create another hitting so zero and end to end.
Now follow along if you can because that's the best but I'll 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 could 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.
And end to end socket learn workflow and we've got the steps right here what we have today is 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.
So let's see if we can do that and maybe build a machine learning model on that heart disease data set.
All right we'll do one get the data ready import pandas as payday.
Now remember this the end to end like it.
So 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 get heart disease Eagles PD Jo Reed see us V now I believe I've moved everything into a data
folder so usually we could just put the CSP name here but I've changed it up and now all of our CSC
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 DOD CSA and then we'll go to heart disease.
We'll view it just to make sure wonderful there's all of our roads there.
So what we're going to do with this is use these columns all across here age sex C.P. whatever these
are to try and predict the target which is 1 0 0 0 heart disease or not.
So the first things first is we need an X and you'll see this a lot in cyclone X is kind of the beaches
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 may we'll put here create X which is called features matrix oftentimes has other names
could be data could be feature variables heart disease Dart.
We want to every column except the target column.
So anything access equals one.
Beautiful.
And then we're going to create Y which is label label matrix all labels.
Let's just call it labels.
Beautiful Y equals.
We want to heart disease.
Now we only want the target column to target.
Wonderful.
So we've created X and Y so basically X and Y X is just easier.
And why is this column here.
Wonderful.
Then what's next.
In socket 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 to is choose the right model and have a parameters hybrid parameters like dials on a model that you
can tune to make it better or worse.
So let's use a random forest SDK line if you're wondering what is Daniel talking about where the random
forest.
Well we're going to see this in a little more debt than a future video.
But it's so far away.
Just imagine random forest from Cyclone dot ensemble.
Import random forest classifier.
This is just a classification machine learning model.
That is a motion need to know for now.
So it's capable of learning patterns in 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 CSF which is short for classifier and socket line.
Other times you'll see a called model but we're gonna use CnF because we'll stick with the documentation
to see it angles random for us classifier beautiful.
And we're going to keep the default parameters hybrid parameters.
The default
hybrid parameters so CnF you can see what parameters your models using using CnF don't get parameter.
So we'll see this maybe take a little while to import in the meantime we'll create the heading for this
one so train is fit the model to the data I'm wondering why that's taking so long.
Maybe it will finish while we're typing.
So first of all we need to train our model on training set and tested 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 that we get.
So this is a hybrid parameters with 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 S.K. lined up model selection.
We need to split our data into training and test and we can do that with SBA loans train test bullet
train test split.
Wonderful.
And we're going to go X train x test y train y test equals train let's test the splint x y test size.
We want test size to be zero point two 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 1 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 we'll see this test size.
Well that means that 80 percent of the data will be used for training because it's zero point two because
test size is going to be 20 percent.
So if we had a thousand rows eight hundred 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 S.K. line.
We want to go CSF.
Now we want to fit it.
We can do that with CnF dot Fed Ex train this is basically saying hey classification model random forest
find the patterns in the training data will go up shift and enter and 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 and estimate is warning.
That makes sense that it's giving us a warning will change from 10 inversion zero point to zero to 100
in zero point to two.
Mm 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
why label equals C left.
Don't predict and we have to pass an empire right.
So let's do that no empire Ray 0 2 3 4 let's see what happens.
Are we in an era.
None Pi is not defined we should have done this right at the top.
Let's do that.
Import num pi as MP.
Beautiful.
Now this should work.
No it doesn't work.
Why doesn't it work are the input so the shape is incorrect.
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 NPM rise 0 2 3 4.
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 spreads is a conventional name for making predictions on test data equals sale.
If our model don't predict x test now what does this look like.
Why spreads.
We've got an array of zeros and ones and now what does this look like.
We want Y test.
We also have an array of zeros and ones.
Hmm hmm I wonder what we could do with this.
Well go Step Four evaluate the model.
This is where we evaluate how good the predictions or how well the machine learning model we've just
trained our random for us classifier has done learning on the training data.
So why train let's do on the training data training data and test data is what we're doing here.
So see a left dot school where 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 percent one point zero.
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 percent 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 percent.
Why y because it got trained on the features.
So way back up here.
It got trained on on these as well as the labels so it had a chance to correct itself if it got something
wrong but it performs at 75 percent 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 say that from S.K. line dot metrics import classification report because we have a classification
problem.
We want to 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 a grant classification report and we want to compare what does this
take.
We can't press shift time because we haven't imported it yet.
I believe it takes y tests to test labels are true labels versus our predictions.
So what this is going to return is spoiler it lets you see a.
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 predictive 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 that's why Fred's wonderful.
And then we can do accuracy school and we want Y test y periods beautiful.
These are just some more steps that we can do to evaluate our model.
And 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 model.
OK.
Let's do it.
Let's see how we can improve a model.
We might go try different amount of an estimate is 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 We'll start off with a random seed.
Well the results are replicable and be random seed.
And I want for I in range 10 10 100.
Step can be 10 here and we won't print.
We'll give a little communication trying model with I estimate is wonderful.
Oh you've hit shift enter too quickly.
It's like I'm trigger happy and again.
My goodness.
OK now then we want to go see a laugh.
Eagles will instantiate another model random forest press tab classify beautiful.
We can thank you auto complete now estimate is eagles I wonderful so this is going to loop through and
try 10 and then 20 and then all out to 100 estimates because we're trying to figure out if we can improve
our model by adjusting one of the Hopper parameters because that's step 5 improved model so we'll go
print if model accuracy on tests and we'll go model dot score x test because we want to evaluate our
model on the test data we're just gonna 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 school.
Yet we're just adding a little bit of communication here and then maybe we'll print a a space here so
we can kind of see.
Let's say this action.
Try and model with 10 estimate as wide in this work.
No model is not defined.
Whoops see I told you model can be used we need CnF CCL f 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 would you want to score a model we've never never fitted on our day yet.
They say this is great love 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 estimates.
Well there we go.
We've gone from what was our original one seventy five percent to 83 percent by adjusting one of the
high parameters of our model to 20 instead of the default which is 10 currently.
But it's gonna be updated to 100 and we're going really faster here.
What's a final stamp.
We go back up here right to the top save a model and load it.
Okay.
Most do it.
So we want to go six save a model and load it.
Now we can save the model with Python's pickle y rate so we're going to import pickle and then to save
the model will go pig or dump and we'll pass it our model a.k.a. ACL F4 classifier will pass and also
give it open with the file name that we want.
If I can remember these random forest Model 1 fancy name haha pickle and then we want to write binary
with WB wonderful.
So this is going to save lot.
See I'm not even following my own model 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 random forest model there.
Now what happens if we tried to import that.
So we want to go loaded model Eagle's pickle dot load and we want to go open pass the file name random.
Can we do that.
Yes we can.
Tab auto complete.
And now this time we need be followed after that read binaries and then we'll go loaded model dot School
X test y test see what happens 73.
Now that should line up seventy three point seven with the last model that we tried up here and it does
wonderful.
Now we've just zoomed through that and took about 20 minutes or so to do an entire socket learn workflow
with a lot of talking.
So we started off with one getting the data ready.
We saw our data frame of heart disease.
Then we split it into x and y which is often what you'll do in supervised learning problems in psychic
line.
We're going to use that 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 hyper parameters which are those little knobs you can turn on
a model.
And we saw how to fit the model to the training data so a.k.a. a model finding patterns in the training
data.
And first of all to even get our data from a big set into training a test we use this little helper
function from so I can't learn.
Thank you socket line.
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 psychic loan is built on Empire raise.
So if we pass that 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 in 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 then we compared our predictions to the Y test through evaluation.
Our model got 100 percent on the training data but only got 75 percent accuracy on the test data which
actually is still pretty good.
A coin toss for 0 0 1 would be 50 percent so our models are halfway to being the perfect model.
And then we go here we tried 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 5.
We tried a bunch of different hyper parameters a.k.a. different estimate as we found out that maybe
20 estimate is the best for our model because it got the highest accuracy.
And then finally we saved a model and reloaded and it got the same score as our most recently trained
model which was ninety estimated seventy three point seventy seven.
And now this is a whole lot to take in.
I completely understand that we've gone through it relatively well 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
see if you can get a model train can you get it saved.
Can you again reimpose it have a little practice but otherwise I'll see in the next video and we're
going to check out how to get our data ready for using it with machine learning models.