2
votes

What is the readable and efficient way to compute the dot product between two columns or rows of a sparse matrix using scipy? Let's say that we want to take the dot product of two vectors x and y, two columns of sparse matrix A, then I'm currently doing:

    x = A.getcol(i)
    y = A.getcol(j)
    dot = (x.transpose() * y)[0,0]

A is stored in csc form for efficiency. Is there a more readable way to get the dot product without sacrificing efficiency?

Note: Using Python 2.7.2 with scipy 0.11.0

1

1 Answers

3
votes

Sparse matrices have a dot method, so you can also go with

dot = x.T.dot(y)[0, 0]

But I personally find your code at least as good as the above.