to_numpy_array#
- to_numpy_array(G, nodelist=None, dtype=None, order=None, multigraph_weight=<built-in function sum>, weight='weight', nonedge=0.0)[source]#
Returns the graph adjacency matrix as a NumPy array.
- Parameters:
- Ggraph
The NetworkX graph used to construct the NumPy array.
- nodelistlist, optional
The rows and columns are ordered according to the nodes in
nodelist
. Ifnodelist
isNone
, then the ordering is produced byG.nodes()
.- dtypeNumPy data type, optional
A NumPy data type used to initialize the array. If None, then the NumPy default is used. The dtype can be structured if
weight=None
, in which case the dtype field names are used to look up edge attributes. The result is a structured array where each named field in the dtype corresponds to the adjacency for that edge attribute. See examples for details.- 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_weightcallable, optional
An function that determines how weights in multigraphs are handled. The function should accept a sequence of weights and return a single value. The default is to sum the weights of the multiple edges.
- weightstring or None optional (default = ‘weight’)
The edge attribute that holds the numerical value used for the edge weight. If an edge does not have that attribute, then the value 1 is used instead.
weight
must beNone
if a structured dtype is used.- nonedgearray_like (default = 0.0)
The value used to represent non-edges in the adjacency matrix. The array values corresponding to nonedges are typically set to zero. However, this could be undesirable if there are array values corresponding to actual edges that also have the value zero. If so, one might prefer nonedges to have some other value, such as
nan
.
- Returns:
- ANumPy ndarray
Graph adjacency matrix
- Raises:
- NetworkXError
If
dtype
is a structured dtype andG
is a multigraph- ValueError
If
dtype
is a structured dtype andweight
is notNone
See also
Notes
For directed graphs, entry
i, j
corresponds to an edge fromi
toj
.Entries in the adjacency matrix are given by the
weight
edge attribute. When an edge does not have a weight attribute, the value of the entry is set to the number 1. For multiple (parallel) edges, the values of the entries are determined by themultigraph_weight
parameter. The default is to sum the weight attributes for each of the parallel edges.When
nodelist
does not contain every node inG
, the adjacency matrix is built from the subgraph ofG
that is induced by the nodes innodelist
.The convention used for self-loop edges in graphs is to assign the diagonal array entry value to the weight attribute of the edge (or the number 1 if the edge has no weight attribute). If the alternate convention of doubling the edge weight is desired the resulting NumPy array can be modified as follows:
>>> import numpy as np >>> G = nx.Graph([(1, 1)]) >>> A = nx.to_numpy_array(G) >>> A array([[1.]]) >>> A[np.diag_indices_from(A)] *= 2 >>> A array([[2.]])
Examples
>>> G = nx.MultiDiGraph() >>> G.add_edge(0, 1, weight=2) 0 >>> G.add_edge(1, 0) 0 >>> G.add_edge(2, 2, weight=3) 0 >>> G.add_edge(2, 2) 1 >>> nx.to_numpy_array(G, nodelist=[0, 1, 2]) array([[0., 2., 0.], [1., 0., 0.], [0., 0., 4.]])
When
nodelist
argument is used, nodes ofG
which do not appear in thenodelist
and their edges are not included in the adjacency matrix. Here is an example:>>> G = nx.Graph() >>> G.add_edge(3, 1) >>> G.add_edge(2, 0) >>> G.add_edge(2, 1) >>> G.add_edge(3, 0) >>> nx.to_numpy_array(G, nodelist=[1, 2, 3]) array([[0., 1., 1.], [1., 0., 0.], [1., 0., 0.]])
This function can also be used to create adjacency matrices for multiple edge attributes with structured dtypes:
>>> G = nx.Graph() >>> G.add_edge(0, 1, weight=10) >>> G.add_edge(1, 2, cost=5) >>> G.add_edge(2, 3, weight=3, cost=-4.0) >>> dtype = np.dtype([("weight", int), ("cost", float)]) >>> A = nx.to_numpy_array(G, dtype=dtype, weight=None) >>> A["weight"] array([[ 0, 10, 0, 0], [10, 0, 1, 0], [ 0, 1, 0, 3], [ 0, 0, 3, 0]]) >>> A["cost"] array([[ 0., 1., 0., 0.], [ 1., 0., 5., 0.], [ 0., 5., 0., -4.], [ 0., 0., -4., 0.]])
As stated above, the argument “nonedge” is useful especially when there are actually edges with weight 0 in the graph. Setting a nonedge value different than 0, makes it much clearer to differentiate such 0-weighted edges and actual nonedge values.
>>> G = nx.Graph() >>> G.add_edge(3, 1, weight=2) >>> G.add_edge(2, 0, weight=0) >>> G.add_edge(2, 1, weight=0) >>> G.add_edge(3, 0, weight=1) >>> nx.to_numpy_array(G, nonedge=-1.) array([[-1., 2., -1., 1.], [ 2., -1., 0., -1.], [-1., 0., -1., 0.], [ 1., -1., 0., -1.]])