[[+content_image]]
D
D
deeppsycoder2021-04-17 13:34:16
numpy
deeppsycoder, 2021-04-17 13:34:16

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

Please tell me a more elegant solution, preferably in one line.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question