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, edge_attr="weight", *, nodelist=None
)
| 1119 | |
| 1120 | @nx._dispatchable(graphs=None, returns_graph=True) |
| 1121 | def from_numpy_array( |
| 1122 | A, parallel_edges=False, create_using=None, edge_attr="weight", *, nodelist=None |
| 1123 | ): |
| 1124 | """Returns a graph from a 2D NumPy array. |
| 1125 | |
| 1126 | The 2D NumPy array is interpreted as an adjacency matrix for the graph. |
| 1127 | |
| 1128 | Parameters |
| 1129 | ---------- |
| 1130 | A : a 2D numpy.ndarray |
| 1131 | An adjacency matrix representation of a graph |
| 1132 | |
| 1133 | parallel_edges : Boolean |
| 1134 | If this is True, `create_using` is a multigraph, and `A` is an |
| 1135 | integer array, then entry *(i, j)* in the array is interpreted as the |
| 1136 | number of parallel edges joining vertices *i* and *j* in the graph. |
| 1137 | If it is False, then the entries in the array are interpreted as |
| 1138 | the weight of a single edge joining the vertices. |
| 1139 | |
| 1140 | create_using : NetworkX graph constructor, optional (default=nx.Graph) |
| 1141 | Graph type to create. If graph instance, then cleared before populated. |
| 1142 | |
| 1143 | edge_attr : String, optional (default="weight") |
| 1144 | The attribute to which the array values are assigned on each edge. If |
| 1145 | it is None, edge attributes will not be assigned. |
| 1146 | |
| 1147 | nodelist : sequence of nodes, optional |
| 1148 | A sequence of objects to use as the nodes in the graph. If provided, the |
| 1149 | list of nodes must be the same length as the dimensions of `A`. The |
| 1150 | default is `None`, in which case the nodes are drawn from ``range(n)``. |
| 1151 | |
| 1152 | Notes |
| 1153 | ----- |
| 1154 | For directed graphs, explicitly mention create_using=nx.DiGraph, |
| 1155 | and entry i,j of A corresponds to an edge from i to j. |
| 1156 | |
| 1157 | If `create_using` is :class:`networkx.MultiGraph` or |
| 1158 | :class:`networkx.MultiDiGraph`, `parallel_edges` is True, and the |
| 1159 | entries of `A` are of type :class:`int`, then this function returns a |
| 1160 | multigraph (of the same type as `create_using`) with parallel edges. |
| 1161 | |
| 1162 | If `create_using` indicates an undirected multigraph, then only the edges |
| 1163 | indicated by the upper triangle of the array `A` will be added to the |
| 1164 | graph. |
| 1165 | |
| 1166 | If `edge_attr` is Falsy (False or None), edge attributes will not be |
| 1167 | assigned, and the array data will be treated like a binary mask of |
| 1168 | edge presence or absence. Otherwise, the attributes will be assigned |
| 1169 | as follows: |
| 1170 | |
| 1171 | If the NumPy array has a single data type for each array entry it |
| 1172 | will be converted to an appropriate Python data type. |
| 1173 | |
| 1174 | If the NumPy array has a user-specified compound data type the names |
| 1175 | of the data fields will be used as attribute keys in the resulting |
| 1176 | NetworkX graph. |
| 1177 | |
| 1178 | See Also |
no test coverage detected
searching dependent graphs…