def louvain(G): communities = community_louvain.best_partition(G) for node, label in communities.iteritems(): G.node[node]['lbl'] = label partition = {} for node in G.nodes: lbl = communities[node] if partition.has_key(lbl): partition[lbl] += [node] else: partition[lbl] = [node] return partition.values() return partition
import networkx as nx from community_louvain import best_partition g = nx.Graph() g.add_nodes_from(range(0, 17)) edgestr = ''' [(0, 0, 47), (0, 1, 2), (0, 5, 1), (0, 7, 1), (0, 13, 1), (0, 16, 1), (1, 1, 31), (1, 11, 1), (1, 14, 2), (2, 2, 53), (2, 11, 1), (2, 10, 1), (2, 15, 1), (3, 3, 32), (4, 16, 12), (4, 8, 1), (4, 10, 2), (4, 11, 1), (4, 4, 29), (5, 5, 29), (5, 8, 1), (5, 10, 12), (5, 13, 2), (5, 14, 1), (6, 6, 15), (7, 7, 77), (7, 9, 1), (7, 12, 1), (7, 13, 1), (7, 14, 1), (8, 8, 41), (8, 14, 1), (8, 13, 1), (9, 9, 22), (9, 12, 17), (9, 14, 1), (10, 10, 9), (11, 11, 52), (11, 14, 2), (12, 12, 38), (12, 13, 1), (12, 15, 1), (13, 13, 73), (13, 16, 1), (14, 14, 49), (15, 15, 10), (16, 16, 17)] ''' weightededgelist = eval(edgestr) unweightededgelist = [(e[0], e[1]) for e in weightededgelist] g.add_weighted_edges_from(weightededgelist) #g.add_edges_from(unweightededgelist) print(list(g.edges(data=True))) communities = best_partition(g) print(communities)
def louvain_partition(graph): partition = community_louvain.best_partition(graph) return partition
import community_louvain import networkx as nx import matplotlib.pyplot as plt import pandas as pd actor_edge = pd.read_csv('2.csv') #导入演员网络边的csv文件,导入后为Dataframe格式 print(actor_edge.head(3)) #显示表的前3行 weight_edge = [] for _, row in actor_edge.iterrows(): #把边及边的权重加入列表,数据格式为(节点,节点,权重) weight_edge.append((row['A'], row['B'], row['rankaveragebox'])) G = nx.Graph() #初始化无向图 G.add_weighted_edges_from(weight_edge) #把带权重边的信息加入无向图中 #first compute the best partition partition = community_louvain.best_partition(G) print("0") #drawing size = float(len(set(partition.values()))) print("0") pos = nx.spring_layout(G) count = 0. for com in set(partition.values()): count = count + 1. list_nodes = [ nodes for nodes in partition.keys() if partition[nodes] == com ] nx.draw_networkx_nodes(G, pos, list_nodes,
def create_graph(self): n = 28 G = nx.grid_2d_graph(n, n) graph_list = [] self.fill_edges(G, n) mapping = dict() for node in G.nodes(): mapping[node] = len(mapping) G = nx.relabel_nodes(G, mapping) for x in range(10000): H = nx.Graph() image, graph_label = mnist_trainset[x] node_attributes,position = self.process_image(image, n) #print(node_attributes) #print(G.number_of_edges()) #self.compute_weights(G,n,node_attributes) #print(G.edges.data()) #print(G.number_of_edges()) partition = best_partition(G) print(partition) no_clusters = max(partition.values()) + 1 print(no_clusters) new_attributes = self.compute_atrributes(H,no_clusters,partition,node_attributes,position) #print(new_attributes) #print(x) #print(H.number_of_nodes()) #print(H.number_of_edges()) nodes = list(G.nodes()) #print(nodes) edge_matrix = np.zeros(shape=(no_clusters,no_clusters)) #count_edges = 0 for i in range(len(nodes)): for j in range(i+1, len(nodes)): if G.has_edge(nodes[i], nodes[j]): if(partition[i]!=partition[j]): H.add_edge(partition[i],partition[j]) edge_matrix[partition[i],partition[j]] = edge_matrix[partition[i],partition[j]] + 1 #count_edges = count_edges + 1 #print(count_edges) edge_attributes = np.zeros(H.number_of_edges()) counter = 0 for i in range(no_clusters): for j in range(no_clusters): if(edge_matrix[i,j] != 0 or edge_matrix[j,i]!=0): edge_attributes[counter] = edge_matrix[i,j] + edge_matrix[j,i] edge_matrix[i,j] = 0 edge_matrix[j,i] = 0 counter = counter + 1 edges = np.array(H.edges()).transpose() graph = Data( x=torch.tensor(new_attributes), edge_index=torch.tensor(edges), edge_attr=torch.tensor(edge_attributes), y=graph_label) graph_list.append(graph) for x in range(1000): H = nx.Graph() image, graph_label = mnist_testset[x] node_attributes,position = self.process_image(image, n) self.compute_weights(G,n,node_attributes) partition = best_partition(G) no_clusters = max(partition.values()) + 1 new_attributes = self.compute_atrributes(H,no_clusters,partition,node_attributes,position) #print(new_attributes) nodes = list(G.nodes()) edge_matrix = np.zeros(shape=(no_clusters,no_clusters)) #count_edges = 0 for i in range(len(nodes)): for j in range(i+1, len(nodes)): if G.has_edge(nodes[i], nodes[j]): if(partition[i]!=partition[j]): H.add_edge(partition[i],partition[j]) edge_matrix[partition[i],partition[j]] = edge_matrix[partition[i],partition[j]] + 1 edge_attributes = np.zeros(H.number_of_edges()) counter = 0 for i in range(no_clusters): for j in range(no_clusters): if(edge_matrix[i,j] != 0 or edge_matrix[j,i]!=0): edge_attributes[counter] = edge_matrix[i,j] + edge_matrix[j,i] edge_matrix[i,j] = 0 edge_matrix[j,i] = 0 counter = counter + 1 edges = np.array(H.edges()).transpose() graph = Data( x=torch.tensor(new_attributes), edge_index=torch.tensor(edges), edge_attr=torch.tensor(edge_attributes), y=graph_label) graph_list.append(graph) return graph_list