I’ve got a SciPy sparse matrix A, let’s say in CSR format, and a vector v of matching length.
What’s the best way of row-scaling A with v, i.e., performing
diag(v) * A?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easy way is to let scipy handle the gory details, and simply do:
EDIT If (and only if) your matrix is stored in CSC format, you can do the operation in place as follows:
I’ve done some timings, at it wildly depends on the sparsity of the matrix and its size, feel free to play with the following code:
Summary seems to be, if it is CSR, or really big, go with the simple first method. If it is a smallish, very sparse matrix, then the in place method will be faster, although all times are small then.