通过对角线对sparse.csc_matrix进行归一化 [英] Normalizing sparse.csc_matrix by its diagonals

查看:116
本文介绍了通过对角线对sparse.csc_matrix进行归一化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dtype = np.int32的scipy.sparse.csc_matrix.我想用该列中的对角线元素有效地划分矩阵的每一列(或行,对于csc_matrix而言更快).所以mnew [:,i] = m [:,i]/m [i,i].请注意,我需要将矩阵转换为np.double(因为mnew元素将位于[0,1]中),并且由于矩阵庞大且非常稀疏,所以我想知道是否可以以某种有效的方式来实现它/否for循环/从不密密麻麻.

I have a scipy.sparse.csc_matrix with dtype = np.int32. I want to efficiently divide each column (or row, whichever faster for csc_matrix) of the matrix by the diagonal element in that column. So mnew[:,i] = m[:,i]/m[i,i] . Note that I need to convert my matrix to np.double (since mnew elements will be in [0,1]) and since the matrix is massive and very sparse I wonder if I can do it in some efficient/no for loop/never going dense way.

最好

Ilya

推荐答案

制作稀疏矩阵:

In [379]: M = sparse.random(5,5,.2, format='csr')
In [380]: M
Out[380]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in Compressed Sparse Row format>
In [381]: M.diagonal()
Out[381]: array([ 0.,  0.,  0.,  0.,  0.])

对角线中的0太多-让我们添加一个非零对角线:

too many 0s in the diagonal - lets add a nonzero diagonal:

In [382]: D=sparse.dia_matrix((np.random.rand(5),0),shape=(5,5))
In [383]: D
Out[383]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements (1 diagonals) in DIAgonal format>
In [384]: M1 = M+D


In [385]: M1
Out[385]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in Compressed Sparse Row format>

In [387]: M1.A
Out[387]: 
array([[ 0.35786668,  0.81754484,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.41928992,  0.        ,  0.01371273,  0.        ],
       [ 0.        ,  0.        ,  0.4685924 ,  0.        ,  0.35724102],
       [ 0.        ,  0.        ,  0.77591294,  0.95008721,  0.16917791],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.16659141]])

现在将每列除以对角线(这是矩阵乘积")很简单

Now it's trivial to divide each column by its diagonal (this is a matrix 'product')

In [388]: M1/M1.diagonal()
Out[388]: 
matrix([[ 1.        ,  1.94983185,  0.        ,  0.        ,  0.        ],
        [ 0.        ,  1.        ,  0.        ,  0.01443313,  0.        ],
        [ 0.        ,  0.        ,  1.        ,  0.        ,  2.1444144 ],
        [ 0.        ,  0.        ,  1.65583764,  1.        ,  1.01552603],
        [ 0.        ,  0.        ,  0.        ,  0.        ,  1.        ]])

或划分行-(乘以列向量)

Or divide the rows - (multiply by a column vector)

In [391]: M1/M1.diagonal()[:,None]


哎呀,它们很稠密;让我们使对角线稀疏


oops, these are dense; let's make the diagonal sparse

In [408]: md = sparse.csr_matrix(1/M1.diagonal())  # do the inverse here
In [409]: md
Out[409]: 
<1x5 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in Compressed Sparse Row format>
In [410]: M.multiply(md)
Out[410]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in Compressed Sparse Row format>
In [411]: M.multiply(md).A
Out[411]: 
array([[ 0.        ,  1.94983185,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.01443313,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  2.1444144 ],
       [ 0.        ,  0.        ,  1.65583764,  0.        ,  1.01552603],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])

md.multiply(M)用于列版本.

稀疏矩阵的除法-相似之处在于它使用的是行总和而不是行数对角线.解决潜在的零除"问题.

Division of sparse matrix - similar except it is using the sum of the rows instead of the diagonal. Deals a bit more with the potential 'divide-by-zero' issue.

这篇关于通过对角线对sparse.csc_matrix进行归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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