J-H
J-H

Reputation: 1869

Multiply each element of a csr matrix column in Scipy

How can i multiply a particular column of a csr matrix with a fixed value (e.g. 5) My approach seems not to work. First i'm creating a update_vector of the same size as my matrix column filled with my default value. Then i utilize the multiply-method of the Scipy csr matrix:

_column = _matrix.getcol(_index)
update_vector = numpy.tile(5, (_column.shape[0], 1))
_matrix[:, _index].multiply(update_vector)

The code runs without exception but the matrix stays unchanged. Do i have to create a copy first or is there another possibility to solve the problem?

Thanks

Upvotes: 1

Views: 441

Answers (1)

Jaime
Jaime

Reputation: 67507

The low level way of doing this operation in-place is something like:

_matrix.data[_matrix.indices == _index] *= 5

I would be surprised if you could not simply do:

_matrix[:, _index] *= 5

although it is hard to know if this really happens in place or triggers some form of copy without looking at the source code.

Upvotes: 2

Related Questions