Skip to content
Advertisement

Python matrix multiplication: sparse multiply dense

Given the code snippet:

JavaScript

where A is a CSR scipy sparse matrix, M and T are two numpy arrays.

Question: During the matrix operations, does numpy treat A as a dense matrix, or M and T as two sparse matrices?

I suspect that the latter case is true since the resulting matrix B is not in the sparse format.

I also notices that this operation is much slower if I change the format of A to dense, which sort of contradicts my guess.

Advertisement

Answer

Numpy doesn’t do sparse matrices. Scipy does the matrix multiplication (this means no multithreading, unlike numpy).

A is kept sparse but A @ M fills a dense array if M is a dense array.

JavaScript

Note that some sparse operations still give matrixes, not arrays:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement