What is most efficient way of setting row to zeros for a sparce scipy matrix?

Posted by Alex Reinking on Stack Overflow See other posts from Stack Overflow or by Alex Reinking
Published on 2013-11-05T08:48:38Z Indexed on 2013/11/06 3:54 UTC
Read the original article Hit count: 98

Filed under:
|
|
|
|

I'm trying to convert the following MATLAB code to Python and am having trouble finding a solution that works in any reasonable amount of time.

M = diag(sum(a)) - a;
where = vertcat(in, out);
M(where,:) = 0;
M(where,where) = 1;

Here, a is a sparse matrix and where is a vector (as are in/out). The solution I have using Python is:

M = scipy.sparse.diags([degs], [0]) - A
where = numpy.hstack((inVs, outVs)).astype(int)
M = scipy.sparse.lil_matrix(M)
M[where, :] = 0  # This is the slowest line
M[where, where] = 1
M = scipy.sparse.csc_matrix(M)

But since A is 334863x334863, this takes like three minutes. If anyone has any suggestions on how to make this faster, please contribute them! For comparison, MATLAB does this same step imperceptibly fast.

Thanks!

© Stack Overflow or respective owner

Related posts about python

Related posts about matlab