I have created a Sparse matrix using the Scipy dok_matrix method as follows:
sparse_dtm = dok_matrix((num_documents, vocabulary_size), dtype=np.float32)
for doc_index, document in enumerate(data_list):
document_counter = Counter(document)
for word in set(document):
sparse_dtm[doc_index, word_index[word]] = document_counter[word]
Where data_list is a list of lists with tokenized texts.
After having created sparse_dtm, I would like to retrieve all values for the first row.
From the documentation I know that I can use the .getrow() method to get all elements from row i.
However, so far I am unable to retrieve the keys/values stored in the csr_matrix:
sparse_dtm.getrow(0).keys()
AttributeError: keys not found
sparse_dtm.getrow(0)[0]
<1x90140 sparse matrix of type '<class 'numpy.float32'>'
with 576 stored elements in Compressed Sparse Row format>
sparse_dtm does contain the right information though:
print(sparse_dtm.getrow(0))
Output: (0, 21018) 6.0
(0, 76741) 3.0
(0, 14008) 1.0
(0, 54143) 2.0
(0, 11866) 1.0
...
How can I access elements from row i and retrieve its keys and values?