Saguaro
Saguaro

Reputation: 233

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

Upvotes: 7

Views: 6897

Answers (3)

J.Schneider
J.Schneider

Reputation: 434

I found this blog post helpful, directly using the statefile produced by MALLET which is also produced using Gensim's Mallet wrapper.

Upvotes: 0

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

Upvotes: 3

kelvt
kelvt

Reputation: 1038

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

Upvotes: 17

Related Questions