문제

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.

도움이 되었습니까?

해결책

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()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top