I have a sparse matrix of size (n x m):
sparse_dtm = dok_matrix((num_documents, vocabulary_size), dtype=np.float32)
for doc_index, document in enumerate(data):
document_counter = Counter(document)
for word in set(document):
sparse_dtm[doc_index, word_to_index[word]] = document_counter[word]
Where:
- num_documents = n
- vocabulary_size = m
- data = list of tokenized lists
Also, I have a list with length n
:
sums = sparse_dtm.sum(1).tolist()
Now, I want to do an element-wise division in which each cell of row_i
in sparse_dtm
is divided by sums[i]
.
A naive approach, using the traditition Python element-wise division:
sparse_dtm / sums
Leads into the following error:
TypeError: unsupported operand type(s) for /: 'csr_matrix' and 'list'
How can I perform this element-wise division?