归一化矩阵行 scipy 矩阵 [英] Normalizing matrix row scipy matrix

查看:113
本文介绍了归一化矩阵行 scipy 矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对从 networkx 有向图中获得的稀疏 scipy 矩阵的每一行进行归一化.

 将 networkx 导入为 nx将 numpy 导入为 npG=nx.random_geometric_graph(10,0.3)M=nx.to_scipy_sparse_matrix(G, nodelist=G.nodes())来自 __future__ 进口部门打印(M[3])(0, 1) 1(0, 5) 1打印(M[3].multiply(1/M[3].sum()))(0, 1) 0.5(0, 5) 0.5

这没问题,我像往常一样正常化,它按预期工作.但是如果我写:

<预><代码>>>>M[3]=M[3].multiply(1/M[3].sum())>>>M[3]<1x10 类型的稀疏矩阵 '<type 'numpy.int64'>'以压缩稀疏行格式存储 10 个元素>(0, 0) 0(0, 1) 0(0, 2) 0(0, 3) 0(0, 4) 0(0, 5) 0(0, 6) 0(0, 7) 0(0, 8) 0(0, 9) 0

我只需要遍历每一行并对这个稀疏 scipy 矩阵进行归一化.你会怎么做?谢谢

解决方案

这里有一个方法(来自 networkx.pagerank_scipy).它使用 scipy 线性代数函数而不是迭代每一行.对于大型图形,这可能会更快.

在[42]中:G=nx.random_geometric_graph(5,0.5)在 [43]: M=nx.to_scipy_sparse_matrix(G, nodelist=G.nodes(), dtype=float)在 [44]: M.todense()出[44]:矩阵([[ 0., 1., 0., 1., 1.],[ 1., 0., 0., 0., 1.],[ 0., 0., 0., 1., 1.],[ 1., 0., 1., 0., 1.],[ 1., 1., 1., 1., 0.]])在 [45] 中:S = scipy.array(M.sum(axis=1)).flatten()在 [46] 中:S[S != 0] = 1.0/S[S != 0]在 [47] 中:Q = scipy.sparse.spdiags(S.T, 0, *M.shape, format='csr')在 [48] 中:(Q*M).todense()出[48]:矩阵([[ 0. , 0.33333333, 0. , 0.33333333, 0.33333333],[ 0.5 , 0. , 0. , 0. , 0.5 ],[ 0. , 0. , 0. , 0.5 , 0.5 ],[ 0.33333333, 0. , 0.33333333, 0. , 0.33333333],[ 0.25 , 0.25 , 0.25 , 0.25 , 0. ]])

I wish to normalize each row of a sparse scipy matrix, obtained from a networkx directed graph.

 import networkx as nx
 import numpy as np

G=nx.random_geometric_graph(10,0.3)
M=nx.to_scipy_sparse_matrix(G, nodelist=G.nodes())

from __future__ import division

 print(M[3])
  (0, 1)        1
  (0, 5)        1

print(M[3].multiply(1/M[3].sum()))                                                                                                                                                                                                                                         
  (0, 1)        0.5
  (0, 5)        0.5

this is ok, I normalize as usual and it's working as desired. But if I write:

>>> M[3]=M[3].multiply(1/M[3].sum())
>>> M[3]
<1x10 sparse matrix of type '<type 'numpy.int64'>'
        with 10 stored elements in Compressed Sparse Row format>
  (0, 0)        0
  (0, 1)        0
  (0, 2)        0
  (0, 3)        0
  (0, 4)        0
  (0, 5)        0
  (0, 6)        0
  (0, 7)        0
  (0, 8)        0
  (0, 9)        0

I just need to iterate over each row and normalize over this sparse scipy matrix. How would you do this? Thanks

解决方案

Here is a way to do it (from networkx.pagerank_scipy). It uses scipy linear algebra functions instead of iterating over each row. That will probably be faster for large graphs.

In [42]: G=nx.random_geometric_graph(5,0.5)

In [43]: M=nx.to_scipy_sparse_matrix(G, nodelist=G.nodes(), dtype=float)

In [44]: M.todense()
Out[44]: 
matrix([[ 0.,  1.,  0.,  1.,  1.],
        [ 1.,  0.,  0.,  0.,  1.],
        [ 0.,  0.,  0.,  1.,  1.],
        [ 1.,  0.,  1.,  0.,  1.],
        [ 1.,  1.,  1.,  1.,  0.]])

In [45]: S = scipy.array(M.sum(axis=1)).flatten()

In [46]: S[S != 0] = 1.0 / S[S != 0]

In [47]: Q = scipy.sparse.spdiags(S.T, 0, *M.shape, format='csr')

In [48]: (Q*M).todense()
Out[48]: 
matrix([[ 0.        ,  0.33333333,  0.        ,  0.33333333,  0.33333333],
        [ 0.5       ,  0.        ,  0.        ,  0.        ,  0.5       ],
        [ 0.        ,  0.        ,  0.        ,  0.5       ,  0.5       ],
        [ 0.33333333,  0.        ,  0.33333333,  0.        ,  0.33333333],
        [ 0.25      ,  0.25      ,  0.25      ,  0.25      ,  0.        ]])

这篇关于归一化矩阵行 scipy 矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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