NetworkX

Previous topic

to_numpy_recarray

Next topic

to_scipy_sparse_matrix

from_numpy_matrix

from_numpy_matrix(A, create_using=None)[source]

Return a graph from numpy matrix.

The numpy matrix is interpreted as an adjacency matrix for the graph.

Parameters :

A : numpy matrix

An adjacency matrix representation of a graph

create_using : NetworkX graph

Use specified graph for result. The default is Graph()

Notes

If the numpy matrix has a single data type for each matrix entry it will be converted to an appropriate Python data type.

If the numpy matrix has a user-specified compound data type the names of the data fields will be used as attribute keys in the resulting NetworkX graph.

Examples

Simple integer weights on edges:

>>> import numpy
>>> A=numpy.matrix([[1,1],[2,1]])
>>> G=nx.from_numpy_matrix(A)

User defined compound data type on edges:

>>> import numpy
>>> dt=[('weight',float),('cost',int)]
>>> A=numpy.matrix([[(1.0,2)]],dtype=dt)                      
>>> G=nx.from_numpy_matrix(A)
>>> G.edges(data=True)
[(0, 0, {'cost': 2, 'weight': 1.0})]