Answer the question
In order to leave comments, you need to log in
[[+content_image]]
How to convert a sparse matrix in the form of a list of lists of tuples to a dense matrix?
There is an LDA model from the gensim.models.ldamodel module. Its get_document_topics(corpus) method returns an object that can be converted to a list of lists of tuples: [[(0, 0.09714252),
(1, 0.04680671), ..., where the length of the list is the document axis and the first number of the tuple is the topic axis , and the second is the probability of the subject of the document.
I would like to convert this list into a dense numpy array. Are there ready-made solutions?
So far I'm doing this (I don't like it when there are a lot of loops in the code):
document_topics = pd.concat(
[pd.Series(dict(doc)) for doc in lda.get_document_topics(corpus)],
axis=1
).T.fillna(0).values
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question