def analyze(g, culturemat, culture_change_all, norm=2): # for analysis of sayama sim g_undir = nx.DiGraph.to_undirected(g) data_dict = { 'degrees': sorted([d for n, d in g.degree()], reverse=True), 'clusterCoeff': nx.average_clustering(g), 'reciprocity': nx.reciprocity(g) } # extra culture analysis data_dict = culture_analyze(data_dict, g, culturemat, culture_change_all, norm) giant = max(nx.connected_components(g_undir), key=len) data_dict['giantComponent'] = len(giant) / len(g.nodes()) # analyze cultures try: data_dict['diam'] = nx.diameter( g_undir) # goto except if g_undir unconnected # g connected: data_dict['SPL'] = nx.average_shortest_path_length( g) # goes to except if g not weakly connected except nx.NetworkXError: # graph not strongly connected, throw away trial num_connected_components = len(list(nx.connected_components(g_undir))) d = nx.diameter(g_undir.subgraph(giant)) data_dict['diam'] = (d, num_connected_components) # analyze biggest component # todo what is going on? spl = nx.average_shortest_path_length(g.subgraph(giant)) data_dict['SPL'] = (spl, num_connected_components) data_dict['CD'] = culture_distance(g, culturemat, culture_change_all, norm) return data_dict
def get_network_stats(g): """ Compute basic properties of a network :param g: input network as an NetworkX graph :return: dictionary with basic network properties as keys """ result = {} result['num_nodes'] = nx.number_of_nodes(g) result['num_edges'] = nx.number_of_edges(g) result['transitivity'] = nx.transitivity(g) if nx.is_directed(g): if nx.is_weakly_connected(g): result['average_shortest_path'] = nx.average_shortest_path_length( g) if nx.is_strongly_connected(g): result['diameter'] = nx.diameter(g) else: result['average_shortest_path'] = nx.average_shortest_path_length(g) result['diameter'] = nx.diameter(g) result['reciprocity'] = nx.reciprocity(g) return result
def clustering_analys(DF_adj, re_type): #测试参数的函数。re_type是返回值的类型 labels = list(DF_adj.index) #print(DF_adj_1,DF_adj) #Network graph G = nx.Graph() G_i = nx.DiGraph() G.add_nodes_from(labels) G_i.add_nodes_from(labels) #Connect nodes for i in range(DF_adj.shape[0]): col_label = DF_adj.columns[i] for j in range(DF_adj.shape[1]): row_label = DF_adj.index[j] node = DF_adj.iloc[i,j] if node != 0: #print(node,DF_adj[labels[i]][labels[j]]) #print(node) G.add_edge(col_label,row_label,weight = node) G_i.add_edge(col_label,row_label,weight = node) if(re_type == 1): return dict_avg(nx.clustering(G))#取平均,队伍或者队员都可以 elif(re_type == 2): L = nx.normalized_laplacian_matrix(G) e = np.linalg.eigvals(L.A) #print("Largest eigenvalue:", max(e))#衡量什么同行网络 return max(e) elif(re_type == 3): return nx.algebraic_connectivity(G) elif(re_type == 4): return(nx.reciprocity(G_i)) elif(re_type == 5): return(nx.transitivity(G_i)) elif(re_type == 6): return(dict_max(nx.in_degree_centrality(G_i))) elif(re_type == 7): return(dict_max(nx.out_degree_centrality(G_i))) elif(re_type == 8): try: return(dict_avg(nx.pagerank(G, alpha=0.9))) except: return(0.01) elif(re_type == 9): try: return(dict_avg(nx.eigenvector_centrality(G))) except: return(0.25) elif(re_type == 10): return(dict_avg(nx.average_neighbor_degree(G_i))) print("-----------------") print(nx.closeness_centrality(G))#衡量星际球员 print("-----------------") print(nx.pagerank(G, alpha=0.9))#衡量球员 print("-----------------") print(nx.eigenvector_centrality(G))#衡量球员 print("-----------------") print()#宏观的连通性 print("-----------------")
def clustering_analys(DF_adj, re_type): #测试参数的函数。re_type是返回值的类型 labels = list(DF_adj.index) #print(DF_adj_1,DF_adj) #Network graph G = nx.Graph() G_i = nx.DiGraph() G.add_nodes_from(labels) G_i.add_nodes_from(labels) #Connect nodes for i in range(DF_adj.shape[0]): col_label = DF_adj.columns[i] for j in range(DF_adj.shape[1]): row_label = DF_adj.index[j] node = DF_adj.iloc[i, j] if node != 0: #print(node,DF_adj[labels[i]][labels[j]]) #print(node) G.add_edge(col_label, row_label, weight=node) G_i.add_edge(col_label, row_label, weight=node) # else: # G.add_edge(col_label,row_label,weight = 100000) # G_i.add_edge(col_label,row_label,weight = 100000) if (re_type == 1): return nx.clustering(G) elif (re_type == 2): return nx.clustering(G) # print(nx.clustering(G))#取平均,队伍或者队员都可以 # print("-----------------") # print(nx.in_degree_centrality(G_i),nx.out_degree_centrality(G_i))#用来评价星际球员 # print("-----------------") # print(nx.closeness_centrality(G))#衡量星际球员 # print("-----------------") # print(nx.pagerank(G, alpha=0.9))#衡量球员 # print("-----------------") # print(nx.eigenvector_centrality(G))#衡量球员 # print("-----------------") # print(nx.algebraic_connectivity(G))#宏观的连通性 # print("-----------------") # L = nx.normalized_laplacian_matrix(G) # e = np.linalg.eigvals(L.A) # print("Largest eigenvalue:", max(e))#衡量什么同行网络 # print("-----------------") if (re_type == 3): #print(nx.attr_matrix(G_i)) return (nx.reciprocity(G_i)) if (re_type == 5): return (nx.eigenvector_centrality(G_i)) if (re_type == 6): return (dict_max(nx.in_degree_centrality(G_i)))
def main(): for legislacao in range(ano_inicial, ano_final + 1, 4): inicio = str(legislacao) fim = str(legislacao + 3) g = load("gml/deputados-" + inicio + "-" + fim) bc = nx.betweenness_centrality(g) dc = nx.degree_centrality(g) ec = nx.eigenvector_centrality(g, max_iter=10000) rm = nx.reciprocity(g) print("Reciprocity {}-{}: {}".format(inicio, fim, rm)) deputados = pd.read_csv('ArquivosLimpos/deputados-' + inicio + '-' + fim + '.csv') deputados_bc = pd.DataFrame.from_dict( bc, orient='index', dtype=None, columns=["betweenness_centrality"]) deputados_dc = pd.DataFrame.from_dict(dc, orient='index', dtype=None, columns=["degree_centrality"]) deputados_ec = pd.DataFrame.from_dict( ec, orient='index', dtype=None, columns=["eigenvecto_centrality"]) deputados_pre = pd.merge(deputados, deputados_bc, left_on='deputado_id', right_on=None, right_index=True) deputados_inter = pd.merge(deputados_pre, deputados_dc, left_on='deputado_id', right_on=None, right_index=True) deputados_final = pd.merge(deputados_inter, deputados_ec, left_on='deputado_id', right_on=None, right_index=True) deputados_final.to_csv('ArquivosLimpos/deputados-' + inicio + '-' + fim + '.csv', encoding='utf-8', index=False) print("Gml para rede") print("---------------------")
def centrality_measures(g): bet_cen = nx.betweenness_centrality(g) clo_cen = nx.closeness_centrality(g) eig_cen = nx.eigenvector_centrality(g) pagerank_cen = nx.pagerank(g) reciprocity_cen = nx.reciprocity(g) top_bet_cen = get_top_keys(bet_cen, 10) top_clo_cen = get_top_keys(clo_cen, 10) top_eig_cen = get_top_keys(eig_cen, 10) top_pagerank_cen = get_top_keys(pagerank_cen, 10) print(f"Top 10 betweenness centrality: {top_bet_cen}") print(f"Top 10 closeness centrality: {top_clo_cen}") print(f"Top 10 eigenvector centrality: {top_eig_cen}") print(f"Top 10 pagerank centrality: {top_pagerank_cen}") print(f"Reciprocity of the graph: {reciprocity_cen}") print(f"Diameter of the largest connected component: {nx.diameter(g)}") print(f"Correlation coefficient: {nx.transitivity(g)}")
def get_net_features(self): num_nodes = nx.number_of_nodes(self._G) # 节点数 num_edges = nx.number_of_edges(self._G) # 连接数 density = nx.density(self._G) # 密度 clusterint_coefficient = nx.average_clustering(self._G) # 平均聚集系数/局部聚集系数 transitivity = nx.transitivity(self._G) # 传递性/全局聚集系数 reciprocity = nx.reciprocity(self._G) # 互惠性 print('节点个数: ', num_nodes) print('连接数: ', num_edges) print('密度: ', density) print('局部聚集系数: ', clusterint_coefficient) print('全局聚集系数: ', transitivity) print('互惠性: ', reciprocity) # 中心度计算 out_degree = nx.out_degree_centrality(self._G) # 出度中心度 in_degree = nx.in_degree_centrality(self._G) # 入度中心度 out_closeness = nx.closeness_centrality(self._G.reverse()) # 出接近中心度 in_closeness = nx.closeness_centrality(self._G) # 入接近中心度 betweenness = nx.betweenness_centrality(self._G) # 中介中心度 print('出度中心度: ', out_degree) print('入度中心度: ', in_degree) print('出接近中心度: ', out_closeness) print('入接近中心度: ', in_closeness) print('中介中心度: ', betweenness) # 中心势计算 # 在networkx中没有似乎没有直接计算中心势的方法,这里我们可以根据公式自己计算。 max_ = 0 s = 0 for out in out_degree.keys(): if out_degree[out] > max_: max_ = out_degree[out] s = s + out_degree[out] print('出度中心势:', (num_nodes * max_ - s) / (num_nodes - 2)) max_ = 0 s = 0 for in_ in in_degree.keys(): if in_degree[in_] > max_: max_ = in_degree[in_] s = s + in_degree[in_] print('入度中心势:', (num_nodes * max_ - s) / (num_nodes - 2)) max_ = 0 s = 0 for b in out_closeness.keys(): if (out_closeness[b] > max_): max_ = out_closeness[b] s = s + out_closeness[b] print('出接近中心势:', (num_nodes * max_ - s) / (num_nodes - 1) / (num_nodes - 2) * (2 * num_nodes - 3)) max_ = 0 s = 0 for b in in_closeness.keys(): if (in_closeness[b] > max_): max_ = in_closeness[b] s = s + in_closeness[b] print('入接近中心势:', (num_nodes * max_ - s) / (num_nodes - 1) / (num_nodes - 2) * (2 * num_nodes - 3)) max_ = 0 s = 0 for b in betweenness.keys(): if (betweenness[b] > max_): max_ = betweenness[b] s = s + betweenness[b] print('中介中心势:', (num_nodes * max_ - s) / (num_nodes - 1))
def test_reciprocity_graph_isolated_nodes(self): DG = nx.DiGraph([(1, 2)]) DG.add_node(4) nx.reciprocity(DG, 4)
def test_reciprocity_graph_isolated_nodes(self): with pytest.raises(nx.NetworkXError): DG = nx.DiGraph([(1, 2)]) DG.add_node(4) nx.reciprocity(DG, 4)
def test_reciprocity_digraph(self): DG = nx.DiGraph([(1, 2), (2, 1)]) reciprocity = nx.reciprocity(DG) assert reciprocity == 1.0
def test_reciprocity_graph_nodes(self): DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)]) reciprocity = nx.reciprocity(DG, [1, 2]) expected_reciprocity = {1: 0.0, 2: 0.6666666666666666} assert reciprocity == expected_reciprocity
def test_reciprocity_graph_node(self): DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)]) reciprocity = nx.reciprocity(DG, 2) assert reciprocity == 0.6666666666666666
indegree_predecessors[user] = statistics.median(ind) outdegree_predecessors[user] = statistics.median(outd) reputation_predecessors[user] = statistics.median(rep) favorites_predecessors[user] = statistics.median(fav) status_predecessors[user] = statistics.median(status) listed_predecessors[user] = statistics.median(listed) age_predecessors[user] = statistics.median(age) default_predecessors[user] = statistics.mean(default) default_image_predecessors[user] = statistics.mean(default_image) # Set graph features ego = nx.ego_graph(G, user) ego_nodes[user] = ego.number_of_nodes() ego_edges[user] = ego.number_of_edges() ego_density[user] = nx.density(ego) try: ego_reciprocity[user] = nx.reciprocity(ego) except: ego_reciprocity[user] = 0 ego_assortativity[user] = nx.attribute_assortativity_coefficient( ego, "followers") data = [ indegree_predecessors, indegree_successors, outdegree_predecessors, outdegree_successors, reputation_predecessors, reputation_successors, favorites_predecessors, favorites_successors, status_predecessors, status_successors, listed_predecessors, listed_successors, age_predecessors, age_successors, default_predecessors, default_successors, default_image_predecessors, default_image_successors, ego_edges, ego_nodes, ego_density, ego_reciprocity, ego_assortativity ]
def sf_internal_recipetaory(g, sg): score = nx.reciprocity(sg) return score
print(n, c) """### Betweeness centrality""" b = nx.betweenness_centrality(G) print(b) """### Closeness centrality""" c = nx.closeness_centrality(G) print(c) """### Group Centrality""" """### Clustering Coefficient""" cc = nx.average_clustering(G) print(cc) nx.reciprocity(G, nodes=None) """### Overall reciprocity ## Transitivity """ t = nx.transitivity(G) print(t) draw(my_graph, pos, nx.in_degree_centrality(my_graph), 'Degree Centrality') from graphviz import Digraph g = Digraph('G', filename='ll.txt') g.view() """**Community Detection**"""
def parse_all_metrics(api, edge_df, user_id, directory=None, long=False): ''' Will get all Tier 3 metrics for a user_id Parameters ---------- api : Tweepy API hook edge_df : Edgelist of Pandas DataFrame user_id : User ID string directory : Directory to look for data The default is None. long : Whether to get metrics that take a long time. The default is False. Returns ------- Feature Data Frame ''' import pandas as pd import twitter_col import json, io, gzip, os import time import progressbar import networkx as nx from collections import Counter import community import numpy as np # user_id = '1919751' G = nx.from_pandas_edgelist(edge_df, 'from', 'to', edge_attr=['type'], create_using=nx.DiGraph()) # G=nx.gnp_random_graph(100, 0.4, seed=None, directed=True) G2 = G.to_undirected() largest_component = max(nx.connected_component_subgraphs(G2), key=len) print("Nodes in largest compo:", len(largest_component.nodes)) data = { "user_id": [], "scrape_date": [], "num_nodes": [], "num_links": [], "density": [], "isolates": [], "dyad_isolates": [], "triad_isolates": [], "compo_over_4": [], # "average_shortest_path_length": [], "clustering_coefficient": [], "transitivity": [], # "network_diameter": [], "reciprocity": [], "graph_degree_centrality": [], "graph_betweenness_centrality": [], "mean_eigen_centrality": [], "simmelian_ties": [], "triad_003": [], "triad_012": [], "triad_102": [], "triad_021D": [], "triad_021U": [], "triad_021C": [], "triad_111D": [], "triad_111U": [], "triad_030T": [], "triad_030C": [], "triad_201": [], "triad_120D": [], "triad_120U": [], "triad_120C": [], "triad_210": [], "triad_300": [], "num_louvaine_groups": [], "size_largest_louvaine_group": [], "ego_effective_size": [] } if long: data.pop("graph_betweenness_centrality") data.pop("ego_effective_size") data.pop("simmelian_ties") data['user_id'].append(user_id) data['scrape_date'].append(time.strftime('%Y%m%d-%H%M%S')) data['num_nodes'].append(nx.number_of_nodes(G)) data['num_links'].append(nx.number_of_edges(G)) data['density'].append(nx.density(G)) compo_sizes = [ len(c) for c in sorted(nx.connected_components(G2), key=len, reverse=True) ] compo_freq = Counter(compo_sizes) # print('isolates') data['isolates'].append(compo_freq[1]) # print('triad_islolates') data['triad_isolates'].append(compo_freq[3]) data['dyad_isolates'].append(compo_freq[2]) data['compo_over_4'].append(len([x for x in compo_sizes if x > 3])) # print('shortest path') # data['average_shortest_path_length'].append(nx.average_shortest_path_length(largest_component)) # print('clustering_coefficient') data['clustering_coefficient'].append(nx.average_clustering(G2)) # print('transitivity') data['transitivity'].append(nx.transitivity(G)) # print('diameter') # data['network_diameter'].append(nx.diameter(largest_component)) # print('reciprocity') data['reciprocity'].append(nx.reciprocity(G)) # print('effective size') if not long: if user_id in list(G.nodes): ef = nx.effective_size(G, nodes=[user_id]) data['ego_effective_size'].append(ef[user_id]) else: data['ego_effective_size'].append(0) # print('degree') data['graph_degree_centrality'].append(graph_centrality(G, kind='degree')) # print('betweenness') if not long: data['graph_betweenness_centrality'].append( graph_centrality(largest_component, kind='betweenness')) # print('eigen_centrality') try: eig = list(nx.eigenvector_centrality_numpy(G).values()) data['mean_eigen_centrality'].append(np.mean(eig)) except: data['mean_eigen_centrality'].append(0) # print('simmelian') # if long: data['simmelian_ties'].append(get_simmelian_ties(G, sparse=True)) # print('census') census = nx.triadic_census(G) data['triad_003'].append(census['003']) data['triad_012'].append(census['012']) data['triad_102'].append(census['021C']) data['triad_021D'].append(census['021D']) data['triad_021U'].append(census['021U']) data['triad_021C'].append(census['030C']) data['triad_111D'].append(census['030T']) data['triad_111U'].append(census['102']) data['triad_030T'].append(census['111D']) data['triad_030C'].append(census['111U']) data['triad_201'].append(census['120C']) data['triad_120D'].append(census['120D']) data['triad_120U'].append(census['120U']) data['triad_120C'].append(census['201']) data['triad_210'].append(census['210']) data['triad_300'].append(census['300']) partition = community.best_partition(G2) p_df = pd.DataFrame.from_dict(partition, orient='index') # print('louvaine') data['num_louvaine_groups'].append(len(set(partition.values()))) data['size_largest_louvaine_group'].append(p_df[0].value_counts().max()) df = pd.DataFrame(data) return (df)
n = n - 1 if n == 0: break print(temp) # 读取边的文件 edges = open("data_2/edge.csv", "r") edgeReader = csv.reader(edges) # 将边加入图中 G = nx.DiGraph() for item in edgeReader: G.add_edge(item[0], item[1]) edges.close() # 测度 print("degree centrality(度中心性):", ) print(sorted(centralityDegree(G))[:5]) PrintNOfType(centralityEigenvector(G), 5, "Eigenvector centrality(特征向量中心性):") PrintNOfType(centralityKatz(G), 5, "Kartz Centality(Katz中心性):") PrintNOfType(PageRank(G), 5, "PageRank(网页排名):") PrintNOfType(centralityBetweenness(G), 5, "Betweenness Centrality(间接中心性):") PrintNOfType(closenessCentrality(G), 5, "Closeness Centrality(紧密中心性):") print("Transitivity(传递性):") print(nx.transitivity(G)) print("Reciprocity(互易性):") print(nx.reciprocity(G))
pass G = nx.DiGraph() #初始化一个有向图 G.add_edge('A', 'B') #添加边 G.add_edge('A', 'D') G.add_edge('B', 'C') G.add_edge('B', 'D') G.add_edge('D', 'E') G.add_edge('E', 'D') # 网络的基本性质 num_nodes = nx.number_of_nodes(G) # 节点数 num_edges = nx.number_of_edges(G) # 连接数 density = nx.density(G) # 密度 clusterint_coefficient = nx.average_clustering(G) # 平均聚集系数/局部聚集系数 transitivity = nx.transitivity(G) # 传递性/全局聚集系数 reciprocity = nx.reciprocity(G) # 互惠性 print('节点个数: ', num_nodes) print('连接数: ', num_edges) print('密度: ', density) print('局部聚集系数: ', clusterint_coefficient) print('全局聚集系数: ', transitivity) print('互惠性: ', reciprocity) # 中心度计算 out_degree = nx.out_degree_centrality(G) # 出度中心度 in_degree = nx.in_degree_centrality(G) # 入度中心度 out_closeness = nx.closeness_centrality(G.reverse()) # 出接近中心度 in_closeness = nx.closeness_centrality(G) # 入接近中心度 betweenness = nx.betweenness_centrality(G) # 中介中心度 print('出度中心度: ', out_degree)
def sf_recipetaory(g, sg): nodes = list(sg.nodes) score_dict = nx.reciprocity(g, nodes) list_score = list(score_dict.values()) score = np.mean(list_score) return score
def main(): df = pd.read_csv( "/home/nicolas/Documentos/redes-complexas/matematica-redes-complexas/data/rede2.csv" ) start = time.time() #list = [] #for index, row in df.iterrows(): # list.append(row['Source']) # list.append(row['Target']) #print(len(np.unique(list))) #undirected_adjacency_matrix = get_undirected_adjacency_matrix(df) #count_nonzero(undirected_adjacency_matrix) #directed_adjacency_matrix = get_directed_adjacency_matrix(df) #transposed_dir_adjacency_matrix = np.transpose(directed_adjacency_matrix) #cocitation_matrix = np.matmul(directed_adjacency_matrix, transposed_dir_adjacency_matrix) #coupling_matrix = np.matmul(transposed_dir_adjacency_matrix, directed_adjacency_matrix) #strongest_coupling = 0 #strongest_cocitation = 0 #strongest_index = -1 #for i in range(len(cocitation_matrix)): # if cocitation_matrix[i][i] > strongest_cocitation and coupling_matrix[i][i] > strongest_coupling: # strongest_cocitation = cocitation_matrix[i][i] # strongest_coupling = coupling_matrix[i][i] # strongest_index = i #print(strongest_index) #print(strongest_coupling) #print(strongest_cocitation) #G = nx.DiGraph() #for index, row in df.iterrows(): # G.add_edge(row['Source'], row['Target']) #print(nx.number_of_nodes(G)) #reciprocity = nx.reciprocity(G) #print(reciprocity) #g = ig.Graph() #g.add_vertex(6005) #for index, row in df.iterrows(): # g.add_edge(row['Source'], row['Target']) G = nx.read_pajek( "/home/nicolas/Documentos/redes-complexas/matematica-redes-complexas/data/rede3.paj" ) #print(nx.number_of_nodes(G)) #print(nx.is_directed_acyclic_graph(G)) reciprocity = nx.reciprocity(G) #print("reciprocity ", reciprocity) adjacency_matrix = nx.adjacency_matrix(G).toarray() count_nonzero(adjacency_matrix) transposed_dir_adjacency_matrix = np.transpose(adjacency_matrix) cocitation_matrix = np.matmul(adjacency_matrix, transposed_dir_adjacency_matrix) coupling_matrix = np.matmul(transposed_dir_adjacency_matrix, adjacency_matrix) strongest_coupling = 0 strongest_cocitation = 0 strongest_index = -1 for i in range(len(cocitation_matrix)): if cocitation_matrix[i][i] > strongest_cocitation and coupling_matrix[ i][i] > strongest_coupling: strongest_cocitation = cocitation_matrix[i][i] strongest_coupling = coupling_matrix[i][i] strongest_index = i print(strongest_index) print("cocitation ", strongest_cocitation) print("coupling ", strongest_coupling) end = time.time() print(end - start)