Save a trained model for later use – Session 7

To save a trained model for later use, you need to manually save it using Python’s serialization tools, such as joblib or pickle. These libraries allow you to save the model to disk and load it back later without retraining it.

Here’s how you can save and load your model using joblib (preferred for larger models) or pickle (used for general object serialization):

import joblib

# After training the model
model.fit(X_train, y_train)

# Save the trained model to a file
joblib.dump(model, 'naive_bayes_model.pkl')

print("Model saved successfully!")

2. Loading the Saved Model:

# Load the saved model from the file
loaded_model = joblib.load('naive_bayes_model.pkl')

# Now you can use the loaded model for predictions
y_pred = loaded_model.predict(X_test)

print("Predictions:", y_pred)
import pickle

# After training the model
model.fit(X_train, y_train)

# Save the trained model to a file using pickle
with open('naive_bayes_model.pkl', 'wb') as model_file:
    pickle.dump(model, model_file)

print("Model saved successfully!")

2. Loading the Saved Model:

# Load the saved model from the file
with open('naive_bayes_model.pkl', 'rb') as model_file:
    loaded_model = pickle.load(model_file)

# Use the loaded model for predictions
y_pred = loaded_model.predict(X_test)

print("Predictions:", y_pred)
  1. joblib:

    • Save: joblib.dump(model, 'filename.pkl') saves the model to a file.

    • Load: joblib.load('filename.pkl') loads the model from a file.

    • Use Case: It is particularly efficient when dealing with large numpy arrays, which makes it well-suited for machine learning models.

  2. pickle:

    • Save: pickle.dump(model, file) saves the model to a file.

    • Load: pickle.load(file) loads the model from a file.

    • Use Case: pickle is a general-purpose serialization library and can be used for saving any Python objects, including models.

You save a model:

  • After training it, so you don’t need to retrain it every time you need to make predictions.

  • To deploy it in production systems where it will be used to make predictions in real time.

  • When you want to share the model with others, or use it at a later point for additional analysis or testing.

In the examples we’ve discussed so far, the model is not being saved. However, by using joblib or pickle, you can easily save your trained model to a file and load it whenever needed. This way, you don’t have to retrain the model every time you want to make predictions.