美文网首页
每日总结-第八天-networkx

每日总结-第八天-networkx

作者: SamiraG | 来源:发表于2020-04-08 23:43 被阅读0次

networkx库

G = nx.Graph() # new graph
G.add_node(1)
G.add_node(1, attr = '[FUNC]')  #增加节点属性
G.add_node(2)
G.add_edge(1, 2)
G.add_edge(1, 2, attr1 = 'this_is_a_edge_attribute') #增加边属性
nx.draw(G)
plt.show()

get subgraph

G = nx.read_gml("call.gml",label="id")
def n_neighbor(node, graph, hop=2, show=False):
    ego_graph = nx.ego_graph(graph, node, radius=hop, undirected=True)
    nodes = ego_graph.nodes      
    edges = ego_graph.edges
    if show:
        import matplotlib.pyplot as plt
        # plot to check
        color = ['r' if n==node else 'b' for n in nodes]
        nx.draw(ego_graph, node_color=color); plt.show()
    return nodes, edges
n_neighbor(5,G,show=True)

TODO

  • 改bug
  • 课件

相关文章

网友评论

      本文标题:每日总结-第八天-networkx

      本文链接:https://www.haomeiwen.com/subject/cycfmhtx.html