Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyLDAvis with Mallet LDA implementation : LdaMallet object has no attribute 'inference'

is it possible to plot a pyLDAvis with a Mallet implementation of LDA ? I have no troubles with LDA_Model but when I use Mallet I get :

'LdaMallet' object has no attribute 'inference'

My code :

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim.prepare(mallet_model, corpus, id2word)
vis
like image 526
Saguaro Avatar asked May 15 '18 00:05

Saguaro


2 Answers

Run this line to convert the class of your mallet model into a LdaModel before pyLDAvis

[Edit]: edited code to use the inbuilt function in gensim instead. I just tried it but am unable to get meaningful results with the pyLDAvis on a converted mallet model; the topics seem to contain random terms.. Anybody encountered this before?

import gensim    
model = gensim.models.wrappers.ldamallet.malletmodel2ldamodel(mallet_model)

Got this from the link below, do explore it, lines 565 - 590

https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/wrappers/ldamallet.py#L359

like image 122
kelvt Avatar answered Oct 01 '22 06:10

kelvt


I hope I have helped.

from gensim.models.ldamodel import LdaModel
def convertldaGenToldaMallet(mallet_model):
    model_gensim = LdaModel(
        id2word=mallet_model.id2word, num_topics=mallet_model.num_topics,
        alpha=mallet_model.alpha, eta=0,
    )
    model_gensim.state.sstats[...] = mallet_model.wordtopics
    model_gensim.sync_state()
    return model_gensim
like image 43
Kid Yonatan Valeriano Valdez Avatar answered Oct 01 '22 06:10

Kid Yonatan Valeriano Valdez