- 图的底子
Tutorial — NetworkX 2.8.4 documentation
networkx/networkx: Network Analysis in Python (github.com)
networkx学习与使用——(2)度、邻人和搜索算法networkx获取邻人节点
首先,我们将加载网络科学中的经典图,即白手道俱乐部网络。我们将探索该图的多个图统计信息
import networkx as nx白手道俱乐部网络是一个图表,形貌了一个由白手道俱乐部的 34 名成员构成的交际网络,并纪录了在俱乐部外举行互动的成员之间的接洽
G = nx.karate_club_graph()# G is an undirected graphtype(G)## networkx.classes.graph.Graph# Visualize the graphnx.draw(G, with_labels = True)标题1: 白手道社团网络的匀称的度是多少?
def average_degree(num_edges, num_nodes): # TODO: Implement this function that takes number of edges # and number of nodes, and returns the average node degree of # the graph. Round the result to nearest integer (for example # 3.3 will be rounded to 3 and 3.7 will be rounded to 4) avg_degree = 0 ############# Your code here ############ avg_degree=round(2*num_edges/num_nodes) ######################################### return avg_degreenum_edges = G.number_of_edges()num_nodes = G.number_of_nodes()print(num_edges)print(num_nodes)avg_degree = average_degree(num_edges, num_nodes)print("Average degree of karate club network is {}".format(avg_degree))## 78## 34## Average degree of karate club network is 5标题2:白手道俱乐部网络的匀称聚类系数是多少?
def average_clustering_coefficient(G): # TODO: Implement this function that takes a nx.Graph # and returns the average clustering coefficient. Round # the result to 2 decimal places (for example 3.333 will # be rounded to 3.33 and 3.7571 will be rounded to 3.76) # avg_cluster_coef = 0 ############# Your code here ############ ## Note: ## 1: Please use the appropriate NetworkX clustering function avg_cluster_coef = nx.average_clustering(G) ######################################### return avg_cluster_coefavg_cluster_coef = average_clustering_coefficient(G)print("Average clustering coefficient of karate club network is {}".format(avg_cluster_coef))标题 3:节点 0(id 为 0 的节点)颠末一次 PageRank 迭代后的 PageRank 值是多少
PageRank算法详解 - 知乎 (zhihu.com)
G.neighbors(0)for line in G.neighbors(0): print(line)#1#3#4#5#6#7#8#10#11#12#13#17#19#21#31def one_iter_pagerank(G, beta, r0, node_id): # TODO: Implement this function that takes a nx.Graph, beta, r0 and node id. # The return value r1 is one interation PageRank value for the input node. # Please round r1 to 2 decimal places. r1 = 0 for line in G.neighbors(node_id): r1+=beta*r0/G.degree(line) r1+=round((1-beta)/G.number_of_nodes(),2) ############# Your code here ############ ## Note: ## 1: You should not use nx.pagerank ######################################### return r1beta = 0.8r0 = 1 / G.number_of_nodes()node = 0r1 = one_iter_pagerank(G, beta, r0, node)print("The PageRank value for node 0 after one iteration is {}".format(r1))标题4:白手道俱乐部网络节点5的(原始)精密性中心度是多少
度中心性(Degrree centrality)-介数中心性(Betweeness centrality)-特性向量中心性( Eigenvector centrality)-k-壳与k-核 - 言非 - 博客园 (cnblogs.com)
在社会网络分析中,常用“中心性(Centrality)"来判定网络中节点紧张性或影响力。最直接的度量是度中心性(Degrree centrality),即一个节点的度越大就意味着这个节点越紧张。
这一指标背后的假设是:紧张的节点就是拥有很多毗连的节点。你的社会关系越多,你的影响力就越强。
精密中心性(Closeness centrality)#
点度中心性仅仅使用了网络的局部特性,即节点的毗连数有多少,但一个人毗连数多,并不代表他/她处于网络的焦点位置。
精密中心性和中介中心性一样,都使用了整个网络的特性,即一个节点在整个结构中所处的位置。
精密中心性(Closeness centrality)也称靠近中心性
精密度中心性与非中心结点相比,一个中心结点应该能更快地到达网络内的其他结点。
即:假如节点到图中其他节点的最短隔断都很小,那么它的靠近中心性就很高。相比中介中心性,靠近中心性更靠近多少上的中心位置。
精密度中心性用于评价一个结点到其他全部结点的精密水平。
def closeness_centrality(G, node=5): # TODO: Implement the function that calculates closeness centrality # for a node in karate club network. G is the input karate club # network and node is the node id in the graph. Please round the # closeness centrality result to 2 decimal places. closeness = 0 closeness=nx.closeness_centrality(G=G,u=node) ### 这个函数是尺度化过的,标题要求非尺度化,以是须要转换一下,如上面的链接的谁人方程 # closeness = round(closeness /len(nx.node_connected_component(G,node))-1,2) closeness = round(closeness/(len(nx.node_connected_component(G,node))-1),2) ## Note: ## 1: You can use networkx closeness centrality function. ## 2: Notice that networkx closeness centrality returns the normalized ## closeness directly, which is different from the raw (unnormalized) ## one that we learned in the lecture. ######################################### return closenessnode = 5closeness = closeness_centrality(G, node=node)print("The karate club network has closeness centrality {}".format(closeness))2 Graph to Tensor
将图 G 转换为 PyTorch 张量,如许我们就可以在图上执行呆板学习
实战|在PyTorch框架下使用PyG和networkx对Graph举行可视化 (qq.com)
import torchprint(torch.__version__)# Generate 3 x 4 tensor with all onesones = torch.ones(3, 4)print(ones)# Generate 3 x 4 tensor with all zeroszeros = torch.zeros(3, 4)print(zeros)# Generate 3 x 4 tensor with random values on the interval [0, 1)random_tensor = torch.rand(3, 4)print(random_tensor)# Get the shape of the tensorprint(ones.shape)# Create a 3 x 4 tensor with all 32-bit floating point zeroszeros = torch.zeros(3, 4, dtype=torch.float32)print(zeros.dtype)# Change the tensor dtype to 64-bit integerzeros = zeros.type(torch.long)print(zeros.dtype)输出
tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]])tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])tensor([[0.3776, 0.6322, 0.4880, 0.0567], [0.3344, 0.2113, 0.3128, 0.6075], [0.8854, 0.1048, 0.6645, 0.3627]])torch.Size([3, 4])torch.float32torch.int64标题5: 获取白手道俱乐部网络的边沿列表,并将其转换为torch.LongTensor.pos_edge_index 张量的 torch.sum 值是多少?
def graph_to_edge_list(G): # TODO: Implement the function that returns the edge list of # an nx.Graph. The returned edge_list should be a list of tuples # where each tuple is a tuple representing an edge connected # by two nodes. edge_list = [] for ei in G.edges(): print(ei) edge_list.append(ei) print(edge_list) ############# Your code here ############ ######################################### return edge_listdef edge_list_to_tensor(edge_list): # TODO: Implement the function that transforms the edge_list to # tensor. The input edge_list is a list of tuples and the resulting # tensor should have the shape [2 x len(edge_list)]. edge_index = torch.tensor([]) edge_index=torch.tensor(edge_list).T print(edge_index) ############# Your code here ############ ######################################### return edge_indexpos_edge_list = graph_to_edge_list(G)pos_edge_index = edge_list_to_tensor(pos_edge_list)print("The pos_edge_index tensor has shape {}".format(pos_edge_index.shape))print("The pos_edge_index tensor has sum value {}".format(torch.sum(pos_edge_index)))[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32), (3, 7), (3, 12), (3, 13), (4, 6), (4, 10), (5, 6), (5, 10), (5, 16), (6, 16), (8, 30), (8, 32), (8, 33), (9, 33), (13, 33), (14, 32), (14, 33), (15, 32), (15, 33), (18, 32), (18, 33), (19, 33), (20, 32), (20, 33), (22, 32), (22, 33), (23, 25), (23, 27), (23, 29), (23, 32), (23, 33), (24, 25), (24, 27), (24, 31), (25, 31), (26, 29), (26, 33), (27, 33), (28, 31), (28, 33), (29, 32), (29, 33), (30, 32), (30, 33), (31, 32), (31, 33), (32, 33)]tensor([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 8, 8, 8, 9, 13, 14, 14, 15, 15, 18, 18, 19, 20, 20, 22, 22, 23, 23, 23, 23, 23, 24, 24, 24, 25, 26, 26, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32], [ 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31, 2, 3, 7, 13, 17, 19, 21, 30, 3, 7, 8, 9, 13, 27, 28, 32, 7, 12, 13, 6, 10, 6, 10, 16, 16, 30, 32, 33, 33, 33, 32, 33, 32, 33, 32, 33, 33, 32, 33, 32, 33, 25, 27, 29, 32, 33, 25, 27, 31, 31, 29, 33, 33, 31, 33, 32, 33, 32, 33, 32, 33, 33]])The pos_edge_index tensor has shape torch.Size([2, 78])The pos_edge_index tensor has sum value 2535标题 6:请实现以下对负边举行采样的函数。然后你会回答白手道俱乐部网络中哪些边(edge_1 到 edge_5)可以是负边?
未完待续
|