加权有向图的转移矩阵

时间:2018-09-23 00:40:40

标签: python networkx graph-theory markov

我想为文件中提供的以下图形构建过渡矩阵,如下所示:

1   2   0.9
1   3   0.1
2   1   0.8
2   2   0.1
2   3   0.1
2   4   0.1
3   2   0.5
3   3   0.3
3   4   0.2
4   1   0.1
4   4   0.9

例如,4 4 0.9表示权重为0.9的从节点4到节点4的弧。我的第一个问题是转换矩阵应该是什么?

我应用以下代码:

def _build_matrices(self, original_ppi):
        """ Build column-normalized adjacency matrix for each graph.

        NOTE: these are column-normalized adjacency matrices (not nx
              graphs), used to compute each p-vector
        """
        original_graph = self._build_og(original_ppi)
        self.OG = original_graph
        og_not_normalized = nx.to_numpy_matrix(original_graph)
        self.og_matrix = self._normalize_cols(og_not_normalized)
        #self.og_matrix = og_not_normalized
        print og_not_normalized

def _build_og(self, original_ppi):
        """ Build the original graph, without any nodes removed. """

        try:
            graph_fp = open(original_ppi, 'r')
        except IOError:
            sys.exit("Could not open file: {}".format(original_ppi))

        G = nx.Graph()
        edge_list = []

        # parse network input
        for line in graph_fp.readlines():
            split_line = line.rstrip().split('\t')
            # assume input graph is a simple edgelist with weights
            edge_list.append((split_line[0], split_line[1],  float(split_line[2])))

        G.add_weighted_edges_from(edge_list)
        graph_fp.close()
        return G

当我打印出矩阵print og_not_normalized时,我得到以下信息:

[[0.  0.1 0.8 0.1]
 [0.1 0.3 0.5 0.2]
 [0.8 0.5 0.1 0.1]
 [0.1 0.2 0.1 0.9]]

列和行的顺序为2 4 3 1。如何使它们按1 2 3 4的顺序排列?

0 个答案:

没有答案