Domanda

The dot product of a row and column in a matrix is a 1x1 csr_matrix. How can I efficiently convert that to a scalar? Right now I use sum. M below is a square matrix:

dot_product_result = csr_matrix.sum(M.getrow(3).dot(M.getcol(5)))

Here's the lil_matrix documentation.

UPDATE When I try the solution below, I get this error:

    raise NotImplementedError('adding a nonzero scalar to a '
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported

This is the actual code I'm using:

 e = rating - Q.getrow(mid).dot(P.getcol(uid))[0]
 iteration_error += e

I initialize iteration_error to 0.0.

È stato utile?

Soluzione

M[3,:].dot(M[:,5])[0,0]

Also, calling csr_matrix.sum like that is not very good style: it's an instance method, so should be called as M[3,:].dot(M[:,5]).sum()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top