Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python计算顶点度矩阵_Python_Cluster Analysis_Graph Theory_Adjacency Matrix - Fatal编程技术网

Python计算顶点度矩阵

Python计算顶点度矩阵,python,cluster-analysis,graph-theory,adjacency-matrix,Python,Cluster Analysis,Graph Theory,Adjacency Matrix,我目前正在努力编写代码来计算度矩阵,这样我就可以计算拉普拉斯L=D-A,其中D=度矩阵,A=邻接矩阵 这将在以后的光谱聚类算法中使用。我正在使用Python。因此,对于这个玩具的例子,我有困难做它。是否有人能提供一种有效的方法来实现这一点,或者是否有用于计算度矩阵的API?我的问题很简单,计算连通度矩阵的有效方法是什么,或者是否有python模块 例如: import numpy as np matrix = np.matrix('1, 1, 1, 1; 1, 0, 0, 0; 0, 0, 1,

我目前正在努力编写代码来计算度矩阵,这样我就可以计算拉普拉斯L=D-A,其中D=度矩阵,A=邻接矩阵

这将在以后的光谱聚类算法中使用。我正在使用Python。因此,对于这个玩具的例子,我有困难做它。是否有人能提供一种有效的方法来实现这一点,或者是否有用于计算度矩阵的API?我的问题很简单,计算连通度矩阵的有效方法是什么,或者是否有python模块

例如:

import numpy as np
matrix = np.matrix('1, 1, 1, 1; 1, 0, 0, 0; 0, 0, 1, 1')

matrix =

 1     1     1     1
 1     0     0     0
 0     1     0     1
 0     0     1     1

我如何计算得到5 3 4的度(矩阵),它表示每个节点的连通度?谢谢。

我想出来了,但我不知道这是不是最有效的方法。然而,我找到了答案

#### Example of Computing Degree Matrix
import numpy as np
matrix = np.matrix('1, 1, 1, 1;'
                   '1, 0, 0, 0;'
                   '0, 1, 0, 1;'
                   '0, 0, 1, 1')

degree = np.zeros(len(matrix)) # initialize list to hold values of degree

# calculate the sums along rows and sum along columns
colsum = matrix.sum(axis=0)
rowsum = matrix.sum(axis=1)

# loop through matrix and add up all degree connections
for j in range(0, len(matrix)):
    degree[j] = colsum[0,j] + rowsum[j,0]

# get the diagonal entries to correct the for loop oversumming
A = matrix.diagonal()
d = A.flat
diagMat = list(d)

# print the degree of connectivity matrix 
print np.diag(degree - diagMat)

[[ 5.  0.  0.  0.]
 [ 0.  3.  0.  0.]
 [ 0.  0.  4.  0.]
 [ 0.  0.  0.  4.]]

你是在计算入学位还是出学位

我认为更有效的代码应该是: 度大小=np大小(矩阵,0)


要回答这个问题,如何从邻接矩阵中获取度矩阵:

它可能不会比其他一些答案快,但至少要简单一点,并且是用i-PyTorch编写的(应该像其他答案一样容易翻译成numpy)

非常直接的代码,一些解释:

  • torch.diagonal获取一维数组中的对角线元素
  • torch.diagflat获取一个数组并创建一个二维对角矩阵

    • 有一个专门的图形包
      networkx

      import networkx as nx
      import numpy as np
      
      m = np.matrix('1, 1, 1, 1;'
                    '1, 0, 0, 0;'
                    '0, 1, 0, 1;'
                    '0, 0, 1, 1')
      G = nx.from_numpy_matrix(m)
      nx.laplacian_matrix(G).toarray()
      
      结果:

      array([[ 3, -1, -1, -1],
             [-1,  2, -1,  0],
             [-1, -1,  3, -1],
             [-1,  0, -1,  2]], dtype=int64)
      

      对于处理无向图的用户,可以使用以下方法创建节点的对角度矩阵:

      def diagonal_degree_matrix(adj):
          diag = np.zeros([adj.shape[0], adj.shape[0]]) # basically dimensions of your graph
          rows, cols = adj.nonzero()
          for row, col in zip(rows, cols):
              diag[row, row] += 1
          return diag
      

      其中,adj
      csr\u矩阵类型的邻接矩阵,np是
      numpy
      库。

      您可以使用
      numpy的
      sum
      diag
      用数组代替矩阵

      import numpy as np
      matrix = np.array([[1, 1, 1, 1],[1, 0, 0, 0],[ 0 ,1 ,0 ,1 ],[0, 0, 1, 1]])    
      degree = np.diag(np.sum(matrix, axis=1))
      

      呃,你想在计算机上用数值方法对图论对象进行编码,你甚至可以声明一个矩阵。是关于邻接矩阵的另一个问题/答案(还有其他类似的问题和答案,你应该先通读一遍,而不是问另一个版本),是互联网上关于如何做你所问的事情的众多文章之一,这是理论。简单地问一下如何有效地计算度矩阵。剩下的我知道怎么做。
      def diagonal_degree_matrix(adj):
          diag = np.zeros([adj.shape[0], adj.shape[0]]) # basically dimensions of your graph
          rows, cols = adj.nonzero()
          for row, col in zip(rows, cols):
              diag[row, row] += 1
          return diag
      
      import numpy as np
      matrix = np.array([[1, 1, 1, 1],[1, 0, 0, 0],[ 0 ,1 ,0 ,1 ],[0, 0, 1, 1]])    
      degree = np.diag(np.sum(matrix, axis=1))