scipy.sparse.coo_matrix 如何快速找到全零列,填充 1 并标准化

2023-12-09

对于矩阵,我想找到全为零的列并用 1 填充,然后按列对矩阵进行归一化。我知道如何使用 np.arrays 做到这一点

[[0 0 0 0 0]
 [0 0 1 0 0]
 [1 0 0 1 0]
 [0 0 0 0 1]
 [1 0 0 0 0]]      
     |
     V
[[0 1 0 0 0]
 [0 1 1 0 0]
 [1 1 0 1 0]    
 [0 1 0 0 1]
 [1 1 0 0 0]]
     |
     V
[[0   0.2 0 0 0]
 [0   0.2 1 0 0]
 [0.5 0.2 0 1 0]   
 [0   0.2 0 0 1]
 [0.5 0.2 0 0 0]]

但是,当矩阵采用 scipy.sparse.coo.coo_matrix 形式时,我该如何做同样的事情,而不将其转换回 np.arrays 。我怎样才能实现同样的目标?


这会容易得多lil格式,并使用行而不是列:

In [1]: from scipy import sparse
In [2]: A=np.array([[0,0,0,0,0],[0,0,1,0,0],[1,0,0,1,0],[0,0,0,0,1],[1,0,0,0,0]])
In [3]: A
Out[3]: 
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [1, 0, 0, 1, 0],
       [0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0]])
In [4]: At=A.T                # switch to work with rows

In [5]: M=sparse.lil_matrix(At)

现在很明显哪一行全为零

In [6]: M.data
Out[6]: array([[1, 1], [], [1], [1], [1]], dtype=object)
In [7]: M.rows
Out[7]: array([[2, 4], [], [1], [2], [3]], dtype=object)

And lil格式允许我们填充该行:

In [8]: M.data[1]=[1,1,1,1,1]
In [9]: M.rows[1]=[0,1,2,3,4]
In [10]: M.A
Out[10]: 
array([[0, 0, 1, 0, 1],
       [1, 1, 1, 1, 1],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0]], dtype=int32)

我也可以用M[1,:]=np.ones(5,int)

The coo格式非常适合从以下位置创建数组data/row/col数组,但不实现索引或数学。它必须转变为csr为了那个原因。和csc对于面向列的东西。

我填写的行在 csr 格式中并不那么明显:

In [14]: Mc=M.tocsr()
In [15]: Mc.data
Out[15]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)
In [16]: Mc.indices
Out[16]: array([2, 4, 0, 1, 2, 3, 4, 1, 2, 3], dtype=int32)
In [17]: Mc.indptr
Out[17]: array([ 0,  2,  7,  8,  9, 10], dtype=int32)

另一方面,这种格式的标准化可能更容易。

In [18]: Mc.sum(axis=1)
Out[18]: 
matrix([[2],
        [5],
        [1],
        [1],
        [1]], dtype=int32)
In [19]: Mc/Mc.sum(axis=1)
Out[19]: 
matrix([[ 0. ,  0. ,  0.5,  0. ,  0.5],
        [ 0.2,  0.2,  0.2,  0.2,  0.2],
        [ 0. ,  1. ,  0. ,  0. ,  0. ],
        [ 0. ,  0. ,  1. ,  0. ,  0. ],
        [ 0. ,  0. ,  0. ,  1. ,  0. ]])

请注意,它将稀疏矩阵转换为稠密矩阵。这sum是稠密的,涉及稀疏和稠密的数学通常会产生稠密。

我必须使用更全面的计算来保留稀疏状态:

In [27]: Mc.multiply(sparse.csr_matrix(1/Mc.sum(axis=1)))
Out[27]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in Compressed Sparse Row format>

这是一种使用以下方法执行此操作的方法csc格式(在A)

In [40]: Ms=sparse.csc_matrix(A)
In [41]: Ms.sum(axis=0)
Out[41]: matrix([[2, 0, 1, 1, 1]], dtype=int32)

Use sum找到全零列。显然,如果列有负值并且总和为 0,这可能是错误的。如果这是一个问题,我可以看到使用所有内容制作矩阵的副本data值替换为 1。

In [43]: Ms[:,1]=np.ones(5,int)[:,None]
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csc_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
In [44]: Ms.A
Out[44]: 
array([[0, 1, 0, 0, 0],
       [0, 1, 1, 0, 0],
       [1, 1, 0, 1, 0],
       [0, 1, 0, 0, 1],
       [1, 1, 0, 0, 0]])

如果您反复进行此类更改,则警告会更重要。请注意,我必须调整 LHS 数组的尺寸。根据全零列的数量,此操作可以显着改变矩阵的稀疏性。

=================

我可以搜索col of coo缺失值的格式为:

In [69]: Mo=sparse.coo_matrix(A)
In [70]: Mo.col
Out[70]: array([2, 0, 3, 4, 0], dtype=int32)

In [71]: Mo.col==np.arange(Mo.shape[1])[:,None]
Out[71]: 
array([[False,  True, False, False,  True],
       [False, False, False, False, False],
       [ True, False, False, False, False],
       [False, False,  True, False, False],
       [False, False, False,  True, False]], dtype=bool)

In [72]: idx = np.nonzero(~(Mo.col==np.arange(Mo.shape[1])[:,None]).any(axis=1))[0]
In [73]: idx
Out[73]: array([1], dtype=int32)

然后我可以在此处添加一列 1idx with:

In [75]: N=Mo.shape[0]
In [76]: data = np.concatenate([Mo.data, np.ones(N,int)])
In [77]: row = np.concatenate([Mo.row, np.arange(N)])
In [78]: col = np.concatenate([Mo.col, np.ones(N,int)*idx])
In [79]: Mo1 = sparse.coo_matrix((data,(row, col)), shape=Mo.shape)
In [80]: Mo1.A
Out[80]: 
array([[0, 1, 0, 0, 0],
       [0, 1, 1, 0, 0],
       [1, 1, 0, 1, 0],
       [0, 1, 0, 0, 1],
       [1, 1, 0, 0, 0]])

正如所写,它仅适用于一列,但它可以推广到几列。我还创建了一个新矩阵而不是更新Mo。但这就地似乎也有效:

Mo.data,Mo.col,Mo.row = data,col,row

正常化仍需csr转换,虽然我认为sparse可以为你隐藏它。

In [87]: Mo1/Mo1.sum(axis=0)
Out[87]: 
matrix([[ 0. ,  0.2,  0. ,  0. ,  0. ],
        [ 0. ,  0.2,  1. ,  0. ,  0. ],
        [ 0.5,  0.2,  0. ,  1. ,  0. ],
        [ 0. ,  0.2,  0. ,  0. ,  1. ],
        [ 0.5,  0.2,  0. ,  0. ,  0. ]])

即使当我承担维持稀疏性质的额外工作时,我仍然得到csr matrix:

In [89]: Mo1.multiply(sparse.coo_matrix(1/Mo1.sum(axis=0)))
Out[89]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in Compressed Sparse Row format>

See

查找 pandas 稀疏矩阵中的全零列

了解查找 0 列的更多方法。事实证明Mo.col==np.arange(Mo.shape[1])[:,None]大的时候太慢Mo。测试使用np.in1d好多了。

1 - np.in1d(np.arange(Mo.shape[1]),Mo.col)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

scipy.sparse.coo_matrix 如何快速找到全零列,填充 1 并标准化 的相关文章

随机推荐