Summary
So, if you use CSR instead of CSC, this is single-line:
mat.data -= numpy.repeat(vec.toarray()[0], numpy.diff(mat.indptr))
Explanation
If you realize this, it’s better to do it differently, since we will subtract the same number from each row. In your example, then: subtract 1 from the first row, 2 from the second row, 3 from the third row.
I really came across this in a real application where I want to classify documents, each of which is represented as a row in a matrix, and the columns represent words. Each document has a rating, which should be multiplied by the rating of each word in this document. Using the sparse matrix row representation, I did something similar to this (I changed my code to answer your question):
mat = scipy.sparse.csc_matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
Result:
[[0 1 2]
[0 1 2]
[0 1 2]]
The visualization looks something like this:
>>> mat_row.data [1 2 3 2 3 4 3 4 5] >>> mat_row.indptr [0 3 6 9] >>> numpy.diff(mat_row.indptr) [3 3 3] >>> numpy.repeat(vec_row.toarray()[0],numpy.diff(mat_row.indptr)) [1 1 1 2 2 2 3 3 3] >>> mat_row.data -= numpy.repeat(vec_row.toarray()[0],numpy.diff(mat_row.indptr)) [0 1 2 0 1 2 0 1 2] >>> mat_row.todense() [[0 1 2] [0 1 2] [0 1 2]]