- 导入包networkxpip install networkx
-
#创建一个没有边和结点的空图 import networkx as nx G = nx.Graph() #添加一个结点 G.add_node(1) #添加多个结点,可以从list,也可以从tuple中添加 G.add_nodes_from([2,3]) #添加结点属性,通过元组实现 G.add_nodes_from([ (4, {"color": "red"}), (5, {"color": "green"})]) #添加边 G.add_edge(1,2) G.add_edge(*(2,3)) #添加多条边,可以用元组包含list,也可以用list包含元组 G.add_edges_from(([3,4],[1,5],[2,4])) G.add_edges_from([(1,3), (2,5)]) #添加一个用"node"命名的结点 G.add_node("node") #添加结点"node"与其他数字结点的联系,这个结点的名字不会表现出来 G.add_edges_from([("node",1), ("node", 3), ("node", 5)]) #查看图的结点个数和边个数 print(G.number_of_nodes(), G.number_of_edges()) #图的四个属性 print(list(G.nodes)) #[1, 2, 3, 4, 5, 'node'] print(list(G.edges)) #[(1, 2), (1, 5), (1, 3), (1, 'node'), (2, 3), (2, 4), (2, 5), (3, 4), (3, 'node'), (5, 'node')] print(list(G.adj["node"])) #[1, 3, 5],结点"node"的邻接结点 print(G.degree["node"]) #3 ,结点"node"的度
```python pandas 与 图 cora_graph = nx.from_pandas_edgelist(citations.sample(n=1500))#可以直接将pandas中数据边导入图中
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)