How to save the trained machine learning models in Python

AI Maverick
3 min readMay 28, 2022

--

Building a machine learning model is just the first step, in many cases, you need to access the built models later to speed up your learning process or your data dimensions for each model are different. In this review, I introduce different approaches in Python to save your trained model and load them later in your algorithm.

Note that this review is a general guide, and you can use it for any built model from various libraries such as Sklearn, TensorFlow, etc.

In this guide, I begin with simple approaches such as dictionaries and lists, then I explain the most complicated ones.

You can find some related examples in my works on my GitHub, and if you have any questions, you can ask me by creating an issue on my GitHub or discussion.

This tutorial is divided into four parts as follows;

  1. Dictionary
  2. List
  3. pickle
  4. NumPy array

I talk about the advantages and disadvantages of each strategy and include a snippet code and an example for each part.

Dictionary

The first strategy is building a dictionary in python for different trained models. The final trained model would be an object dtype, so you can add it to a separate key in your dictionary.

models = {}
k = 5
for i in range(k):
model_ = model.fit(X, y)
models[i] = model_

Here, you will have a dictionary including different models as the dictionary key. To call the specific model, you cal call the dictionary item you are looking for.

models[1].predict(X)

Between the list and dictionary, I prefer the dictionary, because you can have a list with the order and call the model with the order you need.

List

The other simple approach is to list in Python. You can append your list for each newly trained model which is a Python object.

list_ = []
list_.append(model_)

One big issue with this approach is that it sorts the list by the order of entering the models, therefore, you can not call the specific model later or find the particular model you were looking for.

pickle

Pickle is a well-known Python module for serializing binary objects. Therefore, they could be a good method for dumping the trained models. I suggest using this method when you have the same computer when you are going to load the trained models. I suggest using this method when you have the same computer when you are going to load the trained models.

pickle

When you train your ML model, with the dump method from the pickle you can save your object in a path in your directory.

Make sure to use “wb” key to write as a binary file.

pickle.dump(model, open(model_name, "wb"))

later, you can load your model with the load method.

model = pickle.load(open(model_name, "rb"))

After dumping the models, you will have a directory including the dumped ML models. To have a clear and clean algorithm, you can define a control parameter to clean the dump models after using them.

For instance;

import os
def _clear():
for root, _, models in os.walk(path):
for model in models:
os.remove(os.path.join(root, model))

NumPy array

Last but not least is our friend NumPy array. You can take the advantage of the NumPy array to save your trained models in the Matrix form. For this purpose, you need to define an empty array first. Make sure to specify the dtype in the Matrix which is an object.

import numpy as np
n=10
models = np.empty((n, 1), dtype=object)
for i in range(n):
model_ = model()
model_.fit(X, y)
models[i, 0] = model

Here, we stored the fitted model in an array. You can change the size or make an array with more dimensions. It depends on your work.

Conclusion
In this post, we learned about four different ways to save the trained Machine Learning models, and they are general approaches, which means you can use any library to build a model and store them whit one of these approaches.
I suggest checking your Python and library versions when you want to dump your model in binary objects.

If you are motivated to learn more complex methods, you can follow me on GitHub and review some of my codes.

--

--