NetworkX

Previous topic

networkx.convert.from_edgelist

Next topic

networkx.convert.to_numpy_recarray

networkx.convert.to_numpy_matrix

networkx.convert.to_numpy_matrix(G, nodelist=None, dtype=None, order=None, multigraph_weight=<built-in function sum>)

Return the graph adjacency matrix as a NumPy matrix.

Parameters :

G : graph

The NetworkX graph used to construct the NumPy matrix.

nodelist : list, optional

The rows and columns are ordered according to the nodes in nodelist. If nodelist is None, then the ordering is produced by G.nodes().

dtype : NumPy data type, optional

A valid single NumPy data type used to initialize the array. This must be a simple type such as int or numpy.float64 and not a compound data type (see to_numpy_recarray) If None, then the NumPy default is used.

order : {‘C’, ‘F’}, optional

Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory. If None, then the NumPy default is used.

multigraph_weight : {sum, min, max}, optional

An operator that determines how weights in multigraphs are handled. The default is to sum the weights of the multiple edges.

Returns :

M : NumPy matrix

Graph adjacency matrix.

Notes

The matrix entries are assigned with ‘weight’ edge attribute. When an edge does not have the ‘weight’ attribute, the value of the entry is 1. For multiple edges, the values of the entries are the sums of the edge attributes for each edge.

When nodelist does not contain every node in G, the matrix is built from the subgraph of G that is induced by the nodes in nodelist.

Examples

>>> G = nx.MultiDiGraph()
>>> G.add_edge(0,1,weight=2)
>>> G.add_edge(1,0)
>>> G.add_edge(2,2,weight=3)
>>> G.add_edge(2,2)
>>> nx.to_numpy_matrix(G, nodelist=[0,1,2])
matrix([[ 0.,  2.,  0.],
        [ 1.,  0.,  0.],
        [ 0.,  0.,  4.]])