Saving Machine Learning Model in File Format and again Loading the Model from the File.
There are basically two easy ways of saving the trained model, that you have trained in Jupyter Notebook, PyCharm or Google Colab using Machine Learning.
- pickle
- sklearn joblib (efficient if there is more numpy arrays used in the model)
let’s say our trained model is:
md = linear_model.LinearRegression()
md.fit('car_capacity', 'price')
md.predict(1000)
Now, we will use the methods to save our model named ‘md’ ,
1. pickle
Saving The Model into The File :
a. import the pickle library first using import pickle.
b. ‘md_pickle’ is the file name that we are creating using the commands ‘wb’ meaning ‘wb’ = Write in Binary ,
The file is saving in Binary format
c. ‘f’ refers to the file.
d. ‘pickle.dump’ means dumping (Storing) the entire trained model → ( md ) in the file → ( f ).
import pickle
with open('md_pickle', 'wb') as f :
pickle.dump(md,f)
Loading The Model form The File :
a. ‘md_pickle’ the file that was saved previously is opened in Binary Read Mode using ‘rb’ as a file → ‘f’.
b. pickle.load(f) loads the whole ‘f’ → file into a variable named ‘mp’ for further calculations.
c. Now, we can put some value to see the same prediction as above using ‘mp.predict()’ function.
with open('md_pickle', 'rb') as f :
mp = pickle.load(f)
mp.predict(1000)
2. sklearn Joblib
sklearn joblib is also used to save model but its more efficient to store model if there are numpy arrays used in training the model.
Saving The Model into The File :
a. Same as pickle we import joblib library from sklearn.externals.
b. we the dump(Store) the model → ( md ) by giving it a name ‘md_joblib’ in using ‘joblib.dump( )’ .
from joblib import dump, load.
joblib.dump(md, 'md_joblib',)
Loading The Model from The File :
a. Now, using function joblib.load () we load the previously saved file named as ‘md_joblib’ and that holds the model.
b. Then we load this model into variable named mj.
c. Now, we can put some value to see the same prediction as above using ‘mj.predict( )’ .
mj = joblib.load('md_joblib')
mj.predict(1000)
Both methods are commonly used in practice, with joblib
being more efficient for models involving NumPy arrays.
Always ensure that the necessary libraries (like pickle
, joblib
, or other relevant libraries) are installed in your Python environment before using them.