Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

to_numpy_recarray

to_numpy_recarray(G, nodelist=None, dtype=[('weight', <type 'float'>)], order=None)[source]

Return the graph adjacency matrix as a NumPy recarray.

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 NumPy named dtype used to initialize the NumPy recarray. The data type names are assumed to be keys in the graph edge attribute dictionary.

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.

Returns :

M : NumPy recarray

The graph with specified edge data as a Numpy recarray

Notes

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.Graph()
>>> G.add_edge(1,2,weight=7.0,cost=5)
>>> A=nx.to_numpy_recarray(G,dtype=[('weight',float),('cost',int)])
>>> print(A.weight)
[[ 0.  7.]
 [ 7.  0.]]
>>> print(A.cost)
[[0 5]
 [5 0]]