networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
to_numpy_array(G, nodelist=None, dtype=None, order=None, multigraph_weight=<built-in function sum>, weight='weight', nonedge=0.0)

Notes

For directed graphs, entry i, j corresponds to an edge from i to j .

Entries in the adjacency matrix are given by the :None:None:`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 the :None:None:`multigraph_weight` parameter. The default is to sum the weight attributes for each of the parallel edges.

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

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.]])

Parameters

G : graph

The NetworkX graph used to construct the NumPy array.

nodelist : list, optional

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

dtype : NumPy 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 :None:None:`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 adjaceny 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_weight : callable, 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.

weight : string 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. :None:None:`weight` must be None if a structured dtype is used.

nonedge : array_like (default = 0.0)

The value used to represent non-edges in the adjaceny 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 .

Raises

NetworkXError

If :None:None:`dtype` is a structured dtype and G is a multigraph

ValueError

If :None:None:`dtype` is a structured dtype and :None:None:`weight` is not :None:None:`None`

Returns

A : NumPy ndarray

Graph adjacency matrix

Returns the graph adjacency matrix as a NumPy array.

See Also

from_numpy_array

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 :None:None:`nodelist` argument is used, nodes of G which do not appear in the :None:None:`nodelist` 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.]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.linalg.modularitymatrix.modularity_matrix networkx.linalg.graphmatrix.adjacency_matrix networkx.algorithms.link_analysis.hits_alg.hits_numpy networkx.convert_matrix.from_numpy_array networkx.linalg.laplacianmatrix.laplacian_matrix networkx.convert_matrix.to_numpy_array networkx.linalg.modularitymatrix.directed_modularity_matrix

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/convert_matrix.py#1273
type: <class 'function'>
Commit: