Saving a Gensim TF-IDF model as a pickle file

Mohamad MahmoodMohamad Mahmood
1 min read
import pickle
from gensim import corpora, models

# Example TF-IDF model
tfidf_model = models.TfidfModel()

# Save TF-IDF model as a pickle file
with open('tfidf_model.pkl', 'wb') as f:
    pickle.dump(tfidf_model, f)

print("TF-IDF model saved.")

After running the above code, you will have a pickle file named 'tfidf_model.pkl' containing the serialized TF-IDF model. To load the model from the pickle file, you can use the pickle.load() function.

import pickle

# Load TF-IDF model from pickle file
with open('tfidf_model.pkl', 'rb') as f:
    loaded_tfidf_model = pickle.load(f)

print("TF-IDF model loaded.")
0
Subscribe to my newsletter

Read articles from Mohamad Mahmood directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamad Mahmood
Mohamad Mahmood

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He studies at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).