scipy:将稀疏向量添加到稀疏矩阵的特定行 [英] scipy: Adding a sparse vector to a specific row of a sparse matrix

查看:49
本文介绍了scipy:将稀疏向量添加到稀疏矩阵的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,将CSR向量添加到CSR矩阵的特定行的最佳方法是什么?我在这里找到了一种解决方法,但想知道是否有更好/更有效的方法做这个.将不胜感激任何帮助.

In python, what is the best way to add a CSR vector to a specific row of a CSR matrix? I found one workaround here, but wondering if there is a better/more efficient way to do this. Would appreciate any help.

给定一个 NxM CSR 矩阵 A 和一个 1xM CSR 矩阵 B,以及一个行索引 i,目标是添加 Bi 的第 A 行有效.

Given an NxM CSR matrix A and a 1xM CSR matrix B, and a row index i, the goal is to add B to the i-th row of A efficiently.

推荐答案

明显的索引添加确实有效.它给出了一个效率警告,但这并不意味着它是最慢的方法,只是你不应该重复这样做.它建议使用 lil 格式,但转换为该格式并返回可能比执行添加到 csr 矩阵需要更多时间.

The obvious indexed addition does work. It gives a efficiency warning, but that doesn't mean it is the slowest way, just that you shouldn't count of doing this repeatedly. It suggests working with the lil format, but conversion to that and back probably takes more time than performing the addition to the csr matrix.

In [1049]: B.A
Out[1049]: 
array([[0, 9, 0, 0, 1, 0],
       [2, 0, 5, 0, 0, 9],
       [0, 2, 0, 0, 0, 0],
       [2, 0, 0, 0, 0, 0],
       [0, 9, 5, 3, 0, 7],
       [1, 0, 0, 8, 9, 0]], dtype=int32)
In [1051]: B[1,:] += np.array([1,0,1,0,0,0])
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
In [1052]: B
Out[1052]: 
<6x6 sparse matrix of type '<class 'numpy.int32'>'
    with 17 stored elements in Compressed Sparse Row format>
In [1053]: B.A
Out[1053]: 
array([[0, 9, 0, 0, 1, 0],
       [3, 0, 6, 0, 0, 9],
       [0, 2, 0, 0, 0, 0],
       [2, 0, 0, 0, 0, 0],
       [0, 9, 5, 3, 0, 7],
       [1, 0, 0, 8, 9, 0]])

正如您链接的问题所示,可以直接对稀疏矩阵的属性采取行动.他的代码显示了为什么会出现效率警告——在一般情况下,它必须重建矩阵属性.

As your linked question shows, it is possible to act directly on the attributes of the sparse matrix. His code shows why there's an efficiency warning - in the general case it has to rebuild the matrix attributes.

lil 对于行替换更有效,因为它只需要更改矩阵 .data.rows 属性中的子列表.一行的变化不会改变其他任何一行的属性.

lil is more efficient for row replacement because it just has to change a sublist in the matrix .data and .rows attributes. A change in one row doesn't change the attributes of any of the others.

也就是说,如果您添加的行与原始行具有相同的稀疏性,则可以更改 data 属性的特定元素而无需重新修改 .indices.indptr.绘制链接代码

That said, IF your addition has the same sparsity as the original row, it is possible change specific elements of the data attribute without reworking .indices or .indptr. Drawing on the linked code

A.data[:idx_start_row : idx_end_row]

是将要更改的 A.data 切片.您当然需要向量"中的相应切片.

is the slice of A.data that will be changed. You need of course the corresponding slice from the 'vector'.

In [1049] B

In [1085]: B.indptr
Out[1085]: array([ 0,  2,  5,  6,  7, 11, 14], dtype=int32)
In [1086]: B.data
Out[1086]: array([9, 1, 2, 5, 9, 2, 2, 9, 5, 3, 7, 1, 8, 9], dtype=int32)
In [1087]: B.indptr[[1,2]]  # row 1
Out[1087]: array([2, 5], dtype=int32)
In [1088]: B.data[2:5]
Out[1088]: array([2, 5, 9], dtype=int32)
In [1089]: B.indices[2:5]   # row 1 column indices
Out[1089]: array([0, 2, 5], dtype=int32)
In [1090]: B.data[2:5] += np.array([1,2,3])
In [1091]: B.A
Out[1091]: 
array([[ 0,  9,  0,  0,  1,  0],
       [ 3,  0,  7,  0,  0, 12],
       [ 0,  2,  0,  0,  0,  0],
       [ 2,  0,  0,  0,  0,  0],
       [ 0,  9,  5,  3,  0,  7],
       [ 1,  0,  0,  8,  9,  0]], dtype=int32)

注意更改的值 [3,7,12] 采用 lil 格式:

Notice where the changed values, [3,7,12], are in the lil format:

In [1092]: B.tolil().data
Out[1092]: array([[9, 1], [3, 7, 12], [2], [2], [9, 5, 3, 7], [1, 8, 9]], dtype=object)

这篇关于scipy:将稀疏向量添加到稀疏矩阵的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆