‫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 Jupyter Notebook.
‫So let's create a heading six.
‫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 one we're going to cover is with Python's Pickle Module.
‫And the second one is with the Job Lib 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 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 serializing and decentralizing
‫a Python object structure.
‫Well, in our case, if you're wondering what a Python object structure is, in our case, a python
‫object is our model.
‫So if we want to say, for example, say cheese classifier, so 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 pickle can help us do.
‫So let's do that.
‫Let's say an example of how we might export our self, 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 dump.
‫We're going to pass it the object we would like to save in our case.
‫CLF 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 go as random forest model one.
‫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 in our case, I've just gone something nice and simple and Dot Pkl stands for Pickle File and we
‫also pass because we're writing a file, we pass the parameter WB for right binary so if we hit shift
‫and enter.
‫Beautiful.
‫This is going to save.
‫Owl grid search model with this file name to our current working directory.
‫So if we go up here, if we go, geez, random.
‫Well we've got two randoms in there so geez random random forest.
‫So this is a random random forest, really random one k pkl.
‫And you can see there's some other model files here.
‫So Dot PCO, this is one I tried just before and this is another one we created right at the start where
‫we were looking at a psychic 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 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 Pickle as well.
‫So load a saved model.
‫So we're going to go loaded pickle model equals pickle 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 GE's tab.
‫We use tab autocomplete for that and now because we're reading in a file, we pass in the parameter
‫RB for read binary.
‫Wonderful.
‫So that's loaded in our save model.
‫Now the way to check it is we can make some predictions.
‫So to see if this model, if it actually worked, if we saved it and we can use it to make predictions,
‫well then we're in a good place.
‫We want to do loaded pickle model predict now similar to how we made predictions before with our self,
‫we're just going to run this exact same line here but make predictions with our.
‫Pickled model predict on the test data set.
‫Wonderful.
‫And then we'll call our evaluate Fred's function and compare it the test labels to the pickle y Preds.
‫What have we got here?
‫Found input variables with inconsistent number of samples.
‫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 comment 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 and run this cell.
‫Come back down here.
‫We load it in so the mistake here, I believe and maybe wrong.
‫Hit shift into 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 has gone through correctly is if we compare.
‫So if we compare our original GPRS clef predictions, so these values here, if we copy these, these
‫are the original grid search classifier metrics.
‫So let's have a look.
‫So this is our loaded model's metrics.
‫Accuracy, 78.69.
‫Same precision 0.74, same recall 0.82 same and F one score 0.78.
‫So the same.
‫That means that our import, our saving, our model and re importing 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.