Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save LDA model - LatentDirichletAllocation in python

I have trained a LDA model using below command, need to understand how to save it.

lda_model = LatentDirichletAllocation(n_components=25, random_state=100)

I have tried the below method, but it is saying

AttributeError: 'LatentDirichletAllocation' object has no attribute 'save'

lda_model.save("xyz.model")

It took 16 hours to train the model. Re-creating it will be very time consuming. Any advise will be really appreciated!

like image 767
Dr.Chuck Avatar asked May 31 '26 22:05

Dr.Chuck


1 Answers

Models are serializable in scikit-learn, thus you can save it with:

import pickle

pickle.dump(lda_model, 'lda_model.pk')
# then reload it with
lda_model = pickle.load('lda_model.pk')

Note that, according to the doc, you may want to prefer joblib when model contains large estimators

import joblib

joblib.dump(lda_model, 'lda_model.jl')
# then reload it with
lda_model = joblib.load('lda_model.jl')

As mentioned by Michael Silverstein, it is documented here.

like image 119
dtrckd Avatar answered Jun 02 '26 21:06

dtrckd