In a simple vector matrix multiplication I get different results/output formats when using a scipy.sparse matrix instead of a dense matrix. As an example I use the following dense matrix and vector:
import numpy as np
from scipy import sparse
mat = np.array([[1, 1, 0, 0, 0], [0, 2, 2, 0, 0], [0, 0, 3, 3, 0], [0, 0, 0, 4, 4]])
vec = np.arange(1, 5)
For the vector matrix product I get the following expected output:
vec.dot(mat) # array([ 1, 5, 13, 25, 16])
mat.T.dot(vec) # array([ 1, 5, 13, 25, 16])
mat.T.dot(vec.T) # array([ 1, 5, 13, 25, 16])
I accept that it does not play a role if the vector is transposed or not. But when I replace the matrix mat
by a sparse matrix mat_sparse
I obtain as a result an array of sparse 4x5 matrices containing the sparse matrix multiplied by each vector component, i.e. [1x mat_sparse, 2x mat_sparse, ...]
mat_sparse = sparse.lil_matrix(mat)
vec.dot(mat_sparse) # array([ <4x5 sparse matrix of type '<type 'numpy.int64'>' with 8 stored elements in LInked List format>, ...], dtype=object)
Using the transposed matrix trick I obtain the expected result:
mat_sparse.T.dot(vec4.T) # array([ 1, 5, 13, 25, 16])
Can someone explain why this behaviour is expected/wanted? Replacing the matrix mat
(which is actually a 2D array) by an instance of np.matrix(mat
does not change the results.