I'm trying to get perplexity and log likelihood of a Spark LDA model (with Spark 2.1). The code below does not work (methods logLikelihood
and logPerplexity
not found) although I can save the model.
from pyspark.mllib.clustering import LDA
from pyspark.mllib.linalg import Vectors
# construct corpus
# run LDA
ldaModel = LDA.train(corpus, k=10, maxIterations=10)
logll = ldaModel.logLikelihood(corpus)
perplexity = ldaModel.logPerplexity(corpus)
Notice that such methods do not come up with dir(LDA)
.
What would be a working example?
That's because you are working with the old, RDD-based API (MLlib), i.e.
whose
LDA
class indeed does not includefit
,logLikelihood
, orlogPerplexity
methods.In order to work with these methods, you should switch to the new, dataframe-based API (ML):