MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / from_numpy_array

Function from_numpy_array

easygraph/utils/convert_to_matrix.py:260–420  ·  view source on GitHub ↗

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, parallel_edges=False, create_using=None)

Source from the content-addressed store, hash-verified

258
259
260def from_numpy_array(A, parallel_edges=False, create_using=None):
261 """Returns a graph from a 2D NumPy array.
262
263 The 2D NumPy array is interpreted as an adjacency matrix for the graph.
264
265 Parameters
266 ----------
267 A : a 2D numpy.ndarray
268 An adjacency matrix representation of a graph
269
270 parallel_edges : Boolean
271 If this is True, `create_using` is a multigraph, and `A` is an
272 integer array, then entry *(i, j)* in the array is interpreted as the
273 number of parallel edges joining vertices *i* and *j* in the graph.
274 If it is False, then the entries in the array are interpreted as
275 the weight of a single edge joining the vertices.
276
277 create_using : EasyGraph graph constructor, optional (default=eg.Graph)
278 Graph type to create. If graph instance, then cleared before populated.
279
280 Notes
281 -----
282 For directed graphs, explicitly mention create_using=eg.DiGraph,
283 and entry i,j of A corresponds to an edge from i to j.
284
285 If `create_using` is :class:`easygraph.MultiGraph` or
286 :class:`easygraph.MultiDiGraph`, `parallel_edges` is True, and the
287 entries of `A` are of type :class:`int`, then this function returns a
288 multigraph (of the same type as `create_using`) with parallel edges.
289
290 If `create_using` indicates an undirected multigraph, then only the edges
291 indicated by the upper triangle of the array `A` will be added to the
292 graph.
293
294 If the NumPy array has a single data type for each array entry it
295 will be converted to an appropriate Python data type.
296
297 If the NumPy array has a user-specified compound data type the names
298 of the data fields will be used as attribute keys in the resulting
299 EasyGraph graph.
300
301 See Also
302 --------
303 to_numpy_array
304
305 Examples
306 --------
307 Simple integer weights on edges:
308
309 >>> import numpy as np
310 >>> A = np.array([[1, 1], [2, 1]])
311 >>> G = eg.from_numpy_array(A)
312 >>> G.edges(data=True)
313 EdgeDataView([(0, 0, {'weight': 1}), (0, 1, {'weight': 2}), (1, 1, {'weight': 1})])
314
315 If `create_using` indicates a multigraph and the array has only integer
316 entries and `parallel_edges` is False, then the entries will be treated
317 as weights for edges joining the nodes (without creating parallel edges):

Callers 1

from_pandas_adjacencyFunction · 0.85

Calls 4

add_nodes_fromMethod · 0.45
is_multigraphMethod · 0.45
is_directedMethod · 0.45
add_edges_fromMethod · 0.45

Tested by

no test coverage detected