Okay everyone I hope you're as excited as I am.
Why.
Well because we're on the homestretch.
We're up to number six save and load a trained model.
So we've gone through all these steps here.
We've seen how to improve our models with hyper parameter tuning and let's say we're at a point where
we've improved our model to the point that we're happy to either share that with one of our colleagues
or something like that or export it and save it and put it in one of our applications so that our customers
might want to use it might want to access its predictions.
Something along those lines essentially we want to take our model that we've trained.
We've got these results.
Export it to file and so that we can import it and use it somewhere else rather than it just being within
our Jupiter notebook so let's create a heading.
Six guys saving and loading trained machine learning models.
Beautiful.
And there's two ways we can do this.
So two ways to save and load chain learning models.
First I'm gonna cover is with Python's pickle module and the second one is with the job leave module.
So let's check out how we might do it with pickle so the first thing is to import pickle and then and
if you're wondering what pickle is you can go Python pickle as always if if we're not sure what something
is we can search it up so python object serialization.
So if you read this the pickle module implements binary protocols for serialization decentralizing a
python object structure well in our case if you wondering what a python object structure is in our case
a python object is our model.
So if we want to say for example save G.S. classifiers who say we're most happy with our grid search
parameters model.
So the random forest classifier we've trained with grid search.
If we wanted to export that this is our python object and that's what people can help us do.
So let's do that.
Let's say an example of how we might export our GCF which is a python object to a pickle file.
So save an existing model to file.
Yes please.
And to do so we're going to use pickle dot dump we're going to pass it the object we would like to save
in our case G.S. CnF our grid searched classifier and then you can name it something here.
So we need to use the open function so Python's open function for dealing with input and output of files
and in our case we're just going to I guess random forest model 1.
Now you can get as creative as you like with these names.
There might be some nomenclature that you decide upon with your team or something like that.
That's what you use for your models.
But now case I've just gone something nice and simple and Dot P K L stands for pickle file and we also
pass because we're writing a file we pass the parameter WB for write binary so if we hit shift and enter
beautiful this is going to save our grid search model with this file name to our current working directory.
So if we go up here if we go guess random what we've got to randoms in there.
So he s random random forest.
So this is a random random forest really random one dot scale and you can see there's some other model
files here.
So dot P K L This is one I tried just before and this is another one we we created right at the start
where we were looking at a cyclone workflow and now if you've exported a fair few of these models you
might want to put them in like a model file like we've got our data in a data file.
So this is just one way it to tidy up the directory but you can see the one we've just saved here that's
seconds ago now if we come back how would we load that in.
Well we can do it with people as well so load a saved model.
So we're going to go loaded people model Eagles pickle dot load.
Now the advantage here is that because we've saved a model to a pickle file and because it's stored
in our directory here is that someone who wanted to use our model won't have to go through all of the
training steps that we've done above.
They can just access the model and be able to make predictions by loading it in we could copy this file
name here.
But we're just going to type it in Jesus tab we used tab auto complete for that and now because we're
reading in a file we passing the parameter R B for read binary.
Wonderful.
So that's loaded in our save model.
Now the way to check it is we can make some predictions to see if this model if it actually worked if
we saved it.
And we can use it to make predictions while then we're in a good place.
We want to do loaded people model don't predict now similar to how we made predictions before with our
GCF.
We're just going to run this exact same line here but make predictions without pickled model predict
on the test dataset.
Wonderful.
And then we'll call our evaluate parades function and compare it the test labels to the pickle y parades
what have we got here found input variables with inconsistent number of samples.
Mm hmm.
I know what we've done.
We've got a data mismatch here what we'd have to do
is I believe if we just commented out this line and recreate we have to re instantiate our train and
test split that's what we have to do.
So maybe I'll just comment out these lines for the time being run this cell come back down here we load
it in so the mistake here I believe and maybe wrong hit shift enter.
There we go.
Is that our x test had been instantiated somewhere above and we had to re instantiate it so that it
was reset back to the correct shape.
And now the way we can know that this is gone through correctly is if we can pair.
So if we compare our original guess CnF predictions.
So these values here if we copy these is the original grid search classify metrics
so let's have a look.
So this is our loaded models metrics accuracy seventy eight point six nine.
Same precision zero point seven four same recall zero point eight two same and F1 score zero point seven
eight.
So this same that means that our import our saving our model and reporting it.
So loading it in has worked correctly and now again these names here you can change them to whatever
you like.
Same with the loaded variable names but this is just a quick example of how you can use pickle to quickly
save one of your models to file and then load it back in at a later date if need be and then make predictions
using the exact same loaded model that get the exact same results as the model we originally saved in
the next video we'll see how we can do the same thing except this time using job Lib.