Save classifier to disk in scikit-learn in Python
In this article, we will cover saving a Save classifier to disk in scikit-learn using Python.
We always train our models whether they are classifiers, regressors, etc. with the scikit learn library which require a considerable time to train. So we can save our trained models and then retrieve them when required. This saves us a lot of time. Serialization is the process of saving data, whereas Deserialization is the process of restoring it, we will learn to save the classifier models in two ways:
Method 1: Using Pickle
Pickle is a library provided by Python and is the standard way of saving and retrieving files from storage. It first serializes the object model and then saves it to the disk. Later we retrieve it using deserializing. Pickling is a process where a Python object hierarchy is converted into a byte stream. Unpickling is the inverse of the Pickling process where a byte stream is converted into an object hierarchy.
- dumps() – This function is called to serialize an object hierarchy.
- loads() – This function is called to de-serialize a data stream.
# Saving model
pickle.dump(model, open(“model_clf_pickle”, ‘wb’))
# load retrieve
my_model_clf = pickle.load(open(“model_clf_pickle”, ‘rb’))
We have the iris dataset on which we trained the K Nearest Neighbor classifier. Then we saved the model using the pickle and later retrieved using the pickle and calculate the score of the classifier.
Method 2: Using the joblib library
Joblib is the replacement of a pickle as it is more efficient on objects that carry large NumPy arrays. This is solely created for the purpose of saving the models and retrieving them when required These functions also accept file-like objects instead of filenames.
- joblib.dump is used to serialize an object hierarchy
- joblib.load is used to deserialize a data stream
# Save model joblib.dump(model,"model_name.pkl") # Retrieve model joblib.load("model_name.pkl")
We have the iris dataset on which we trained the K Nearest Neighbor classifier. Then we saved the model using joblib and later retrieved using the joblib. Finally, we calculate the score of the classifier.
Please Login to comment...