Skip to main content
`Ctrl`+`K`

# from_numpy_array

from_numpy_array(A, parallel_edges=False, create_using=None, edge_attr='weight', *, nodelist=None)

  Returns a graph from a 2D NumPy array.

  The 2D NumPy array is interpreted as an adjacency matrix for the graph.

  Parameters:
    A a 2D numpy.ndarray

      An adjacency matrix representation of a graph

    parallel_edges Boolean

      If this is True, `create_using` is a multigraph, and `A` is an integer array, then entry (i, j) in the array is interpreted as the number of parallel edges joining vertices i and j in the graph. If it is False, then the entries in the array are interpreted as the weight of a single edge joining the vertices.

    create_using NetworkX graph constructor, optional (default=nx.Graph)

      Graph type to create. If graph instance, then cleared before populated.

    edge_attr String, optional (default=”weight”)

      The attribute to which the array values are assigned on each edge. If it is None, edge attributes will not be assigned.

    nodelist sequence of nodes, optional

      A sequence of objects to use as the nodes in the graph. If provided, the list of nodes must be the same length as the dimensions of `A`. The default is `None`, in which case the nodes are drawn from `range(n)`.

  See also

  `to_numpy_array`

  Notes

  For directed graphs, explicitly mention create_using=nx.DiGraph, and entry i,j of A corresponds to an edge from i to j.

  If `create_using` is `networkx.MultiGraph` or `networkx.MultiDiGraph`, `parallel_edges` is True, and the entries of `A` are of type `int`, then this function returns a multigraph (of the same type as `create_using`) with parallel edges.

  If `create_using` indicates an undirected multigraph, then only the edges indicated by the upper triangle of the array `A` will be added to the graph.

  If `edge_attr` is Falsy (False or None), edge attributes will not be assigned, and the array data will be treated like a binary mask of edge presence or absence. Otherwise, the attributes will be assigned as follows:

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

  If the NumPy array 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 as np
      >>> A = np.array([[1, 1], [2, 1]])
      >>> G = nx.from_numpy_array(A)
      >>> G.edges(data=True)
      EdgeDataView([(0, 0, {'weight': 1}), (0, 1, {'weight': 2}), (1, 1, {'weight': 1})])

  If `create_using` indicates a multigraph and the array has only integer entries and `parallel_edges` is False, then the entries will be treated as weights for edges joining the nodes (without creating parallel edges):

      >>> A = np.array([[1, 1], [1, 2]])
      >>> G = nx.from_numpy_array(A, create_using=nx.MultiGraph)
      >>> G[1][1]
      AtlasView({0: {'weight': 2}})

  If `create_using` indicates a multigraph and the array has only integer entries and `parallel_edges` is True, then the entries will be treated as the number of parallel edges joining those two vertices:

      >>> A = np.array([[1, 1], [1, 2]])
      >>> temp = nx.MultiGraph()
      >>> G = nx.from_numpy_array(A, parallel_edges=True, create_using=temp)
      >>> G[1][1]
      AtlasView({0: {'weight': 1}, 1: {'weight': 1}})

  User defined compound data type on edges:

      >>> dt = [("weight", float), ("cost", int)]
      >>> A = np.array([[(1.0, 2)]], dtype=dt)
      >>> G = nx.from_numpy_array(A)
      >>> G.edges()
      EdgeView([(0, 0)])
      >>> G[0][0]["cost"]
      2
      >>> G[0][0]["weight"]
      1.0