简体   繁体   English

将Scipy中的csr矩阵列的每个元素相乘

[英]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 (eg 5) My approach seems not to work. 我如何将csr矩阵的特定列乘以固定值(例如5),我的方法似乎不起作用。 First i'm creating a update_vector of the same size as my matrix column filled with my default value. 首先,我要创建一个update_vector,其大小与填充我的默认值的矩阵列相同。 Then i utilize the multiply-method of the Scipy csr matrix: 然后我利用Scipy csr矩阵的乘法方法:

_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 谢谢

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. 尽管很难在不查看源代码的情况下知道这种情况是否确实发生或触发某种形式的复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM