NetworkX 学习笔记
3.2 基本网络类型
3.2.1 非定向网络(含自循环)
Graph(data=None, **arrr)
- data(input graph) : 可以是边的列表,或者一个 NetworkX 对象。
- attr(keyword arguments, optional (default = no attributes)) : 属性键值对。
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#!/user/bin/env python
# coding:utf8
import networkx as NetworkX
G = NetworkX.Graph()
# 节点
# 添加单个节点
G.add_node(1)
# 从列表中添加节点
G.add_nodes_from([2, 3])
G.add_nodes_from(range(100, 110))
# 从另一个 Graph 对象中添加节点
H = NetworkX.Graph()
H.add_path([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
G.add_nodes_from(H)
# 边
# 添加单条边
G.add_edge(1, 2)
# 添加一个边的列表
G.add_edges_from([1, 2], [1, 3])
# 从另一个 Graph 中添加边
G.add_edges_from(H.edges())
# 属性
G = NetworkX.Graph(day = "Friday")
G.graph # graph 返回属性字典
# 节点属性添加
G.add_node(1, time = '5pm')
G.add_nodes_from([3], time = '2pm') #对 [] 中所有点添加相同点属性值
# 节点多个属性
G.node[1]['room'] = 714
del G.node[1]['room'] # 移除属性
# 显示所有节点及对应属性
G.nodes(data = True)
# 对边添加属性
G.add_edge(1, 2, weight=4.7)
G.add_edges_from([(3, 4), (4, 5)], color='red')
G.add_edges_from([(1, 2, {'color':'blue'}), (2, 3, {'weight':8})])
G.edges(data = True) # 显示所有边及其属性
G.edges(data = 'weight') # 显示所有边的 weight 属性
3.2.2 方法
Graph.__init__(data=None, **attr)
初始化一个网路
# 生成无向网络
G = NetworkX.Graph()
# 生成网络并附属性命名
G = NetworkX.Graph(name = 'my graph')
Graph.add_node(n, attr_dict=None, **attr)
- n : 一个节点(可以是任何 Python 对象,包括另一个 graph)
- attr_dit : 字典或者节点属性
- attr
G = NetworkX.Graph()
G.add_node(1)
G.add_node('Hello')
# 创建一个 graph
K3 = NetworkX.Graph([(0, 1), (1, 2), (2, 0)])
# 将 Graph 作为一个节点加入 G 中
G.add_node(K3)
G.number_of_nodes() # 节点:1,hello,K3
Graph.add_nodes_from(nodes, **attr)
3.3.1中已经有所介绍
nodes 对象均为迭代对象
G = NetworkX.Graph()
G.add_nodes_from('Hello') # 从字符串中添加节点, H e l l o , 并非作为一个对象
G.add_nodes_from(K3) # 将 K3 中的所有 node 加入 G
K3 = NetworkX.Graph([(0, 1), (1, 2), (2, 0)])
G.add_node(K3) # 将 K3 作为一个 node 加入 G
s = sorted(G.nodes(), key = str) # 将 G 中 node 作为 str 存入 s
G.add_nodes_from([3, 4], weight = 0.4) # 给[]`中的所有对象添加相同的属性
# 同时添加多个 node 及属性
G.add_nodes_from([(1, dict(size=11)), (2, {'color':'blue'})])
H = NetworkX.Graph()
H.add_nodes_from(G.nodes(data = True)) #将 G 中 node 及其属性添加入 H
Graph.remove_node(n)
顾名思义,移除单个 node
Graph.remove_nodes_from(nodes)
- nodes : 一个可迭代对象存放 nodes。如果所含某个 node 不在 Graph 中将直接忽略
Graph.add_edge(u, v, attr_dict=None, **attr)
- v(u, ) : 节点
- attr_dict :
- attr :
Graph.add_edges_from(ebunch, attr_dict=None, **attr)
- ebunch :
- attr_dict :
- attr :
G = NetworkX.Graph()
G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples
e = zip(range(0, 3), range(1, 4)) # e = [(0, 1), (1, 2), (2, 3)]
G.add_edges_from(e)
G.add_edges_from([(1, 2), (2, 3)], weight=3)
G.add_edges_from([(3, 4), (1, 4)], label='WN2898')
Graph.add_weighted_edges_from(ebunch, weight='weight', **attr)
- ebunch : (u, v, w)
- weight :
- attr :
Graph.remove_edge(u, v)
G = NetworkX.Graph()
G.add_path([0, 1, 2 ,3])
G.remove_edge(0, 1)
e = (1, 2)
G.remove(*e)
e = (2, 3, {'weight':7}) #
G.add_path([3, 4, 5])
G.add_edge(1, 2)
G.remove_edge(*e[:2]) # 选择匹配 e 中前两位
Graph.remove_edges_from
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
ebunch = [(1, 2), (2, 3)]
G.remove_edges_from(ebunch)
Graph.add_star(nodes, **attr)
中心
- nodes : nodes 中的第一个 node 将会与其后所有 node 连接
- attr :
G = NetworkX.Graph()
G.add_star([0, 1, 2 ,3])
G.add_star([10, 11, 12], weight=2)
Graph.add_paht(nodes, **attr)
- ndoes : 路径按照 nodes 中的 node 顺序连接
- attr :
G = NetworkX.Graph()
G.add_path([0, 1, 2 ,3])
G.add_path([10, 11, 12], weight=2)
Graph.add_cycle(nodes, **attr)
- nodes : nodes 中 node 按照顺序形成一个循环
- attr :
G = NetworkX.Graph()
G.add_cycle([0, 1, 2 ,3])
G.add_cycle([10, 11, 12], weight=2)
Graph.clear()
移除所有 node & edge
并且移除图形名称、属性等所有数据
Graph.nodes(data=False)
- True : 返回[(node, {node data}), ....]
- False : a list of nodes
Graph.nodes_iter(data=False)
同上
Graph.__iter__()
这两个类似于迭代器,但具体的使用不太清楚,欢迎讨论
Graph.edges(nbunch=None, data=False, default=None)
- nbunch : 可选,存放 nodes 迭代
- data : edges 属性返回 (u, v, ddict[data]),若 False 返回 (u, v)
- default :
G = NetworkX.Graph()
G.add_path([0, 1, 2])
G.add_edge(2, 3, weight=5)
print G.edges()
G.edges(data = True)
list(G.edges_iter(data='weight', default = 1))
G.edges([0, 3]) # 搜索所有 0, 3 开始的边
G.edges(0) # 搜索所有 0 开始的边
Graph.edges_iter(nbunch=None, data=False, default=None)
- nbunch :
- data :
- default :
G = NetworkX.Graph()
G.add_path([0, 1, 2])
G.add_edge(2, 3, weight=5)
print [e for e in G.edges_iter()]
print list(G.edges_iter(data=True)) # 将 edge 及其属性数据返回为一个列表
list(G.edges_iter(data='weight', default=1)) # 没有 weight 赋值的 edge 统一默认赋值为 1
list(G.edges_iter([0, 3])) # 搜索输出所有 0、3 开头的边
list(G.edges_iter(0)) # 参上
Graph.get_edge_data(u, v, default=None)
返回 edge(u,v) 的属性字典,属于查看性质
- v(u, ) :
- default :
Graph.neighbors(n)
返回与参数节点相连的节点列表
- n(node) :
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
G.add_edges_from([(0, 4), (0, 9), (1, 3)])
print G.neighbors(0)
print G.neighbors(1)
Graph.neighbors_iter(n)
返回节点 n 的临近节点的迭代
Graph.__getitem__(n)
将 n 的临近节点及其属性作为字典返回
返回对象为双层字典 {1: {}, 2: {]},内层字典为何用?
Graph.adjacency_list()
返回表示网络的 adjacency list
按照节点顺序输出
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
print G.adjacency_list()
# 输出 [[1], [0, 2], [1, 3], [2]]
# 分别对应 0 1 2 3 节点所连接的节点
Graph.adjacency_iter()
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
G.add_edges_from([(0, 4), (0, 5), (1, 9), (4, 5)])
print G.adjacency_list()
# [[1, 4, 5], [0, 9, 2], [1, 3], [2], [0, 5], [0, 4], [1]]
print [(n, nbrdict) for n,nbrdict in G.adjacency_iter()]
[(0, {1: {}, 4: {}, 5: {}}), (1, {0: {}, 9: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}}), (4, {0: {}, 5: {}}), (5, {0: {}, 4: {}}), (9, {1: {}})]
Graph.has_node(n)
如果 n 在 G 内,返回 True
Graph.__contains_(n)
同上,用法更简单node in G
Graph.has_edge(u, v)
是否含有 edge
Graph.order()
返回 G 中 node 个数
Graph.number_of_ndoes()
同上
Graph.__len__()
同上
Graph.degree(nbunch=None, weight=None)
返回节点度
- nbunch : 存放 nodes
- weight : 各边的权重
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
print G.degree([0, 1]) # 输出节点及度
print list(G.degree([0, 1]).values()) # 仅输出度
Graph.degree_iter(nbunch=None, weight=None)
Graph.size(weight=None)
返回 edge 数量
G = NetworkX.Graph()
G.add_path([0, 1, 2, 3])
print G.size() # Output : 3
G.add_edge('a', 'b', weight=2)
G.add_edge('b', 'c', weight=4)
print G.size() # Output : 5
print G.size(weight='weight')
# Output : 9. 3+6(a-b权重2, b-c权重4)
Graph.number_of_edges()
返回 edge 个数
Graph.nodes_with_selfloops()
返回含有自循环的节点列表(单个节点的自循环)
Graph.selfloop_edges(data=False, default=None)
返回循环边组成的列表
Graph.number_of_selfloops()
返回自循环边的数量
Graph.copy()
复制一个图形
G = nx.Graph()
G.add_path([0, 1, 2, 3])
H = G.copy()
Graph.to_undirected()
将原图形转化为非定向网络
Graph.to_directed()
将原图转化为定向网络
Graph.subgraph()
返回包含选定节点的子网络
- nbunch :
G = nx.Graph()
G.add_path([0, 1, 2, 3])
H = G.subgraph([0, 1, 2]) # 0,1,2 节点及之间的边
print H.edges()
网友评论