def hits_numpy(G): """Return HITS hubs and authorities values for nodes. The HITS algorithm computes two numbers for a node. Authorities estimates the node value based on the incoming links. Hubs estimates the node value based on outgoing links. Parameters ----------- G : graph A NetworkX graph Returns ------- (hubs,authorities) : two-tuple of dictionaries Two dictionaries keyed by node containing the hub and authority values. Examples -------- >>> G=nx.path_graph(4) >>> h,a=nx.hits(G) Notes ----- The eigenvector calculation uses NumPy's interface to LAPACK. The HITS algorithm was designed for directed graphs but this algorithm does not check if the input graph is directed and will execute on undirected graphs. References ---------- .. [1] A. Langville and C. Meyer, "A survey of eigenvector methods of web information retrieval." http://citeseer.ist.psu.edu/713792.html .. [2] Jon Kleinberg, Authoritative sources in a hyperlinked environment Journal of the ACM 46 (5): 604-32, 1999. doi:10.1145/324133.324140. http://www.cs.cornell.edu/home/kleinber/auth.pdf. """ try: import numpy as np except ImportError: raise ImportError(\ "hits_numpy() requires NumPy: http://scipy.org/") if len(G) == 0: return {}, {} H = nx.hub_matrix(G, G.nodes()) e, ev = np.linalg.eig(H) m = e.argsort()[-1] # index of maximum eigenvalue h = np.array(ev[:, m]).flatten() A = nx.authority_matrix(G, G.nodes()) e, ev = np.linalg.eig(A) m = e.argsort()[-1] # index of maximum eigenvalue a = np.array(ev[:, m]).flatten() hubs = dict(zip(G.nodes(), map(float, h / h.sum()))) authorities = dict(zip(G.nodes(), map(float, a / a.sum()))) return hubs, authorities
def test_empty(self): G=networkx.Graph() assert_equal(networkx.hits(G),({},{})) assert_equal(networkx.hits_numpy(G),({},{})) assert_equal(networkx.hits_scipy(G),({},{})) assert_equal(networkx.authority_matrix(G).shape,(0,0)) assert_equal(networkx.hub_matrix(G).shape,(0,0))
def hits_numpy(G): """Return HITS hubs and authorities values for nodes. The HITS algorithm computes two numbers for a node. Authorities estimates the node value based on the incoming links. Hubs estimates the node value based on outgoing links. Parameters ----------- G : graph A NetworkX graph Returns ------- (hubs,authorities) : two-tuple of dictionaries Two dictionaries keyed by node containing the hub and authority values. Examples -------- >>> G=nx.path_graph(4) >>> h,a=nx.hits(G) Notes ----- The eigenvector calculation uses NumPy's interface to LAPACK. The HITS algorithm was designed for directed graphs but this algorithm does not check if the input graph is directed and will execute on undirected graphs. References ---------- .. [1] A. Langville and C. Meyer, "A survey of eigenvector methods of web information retrieval." http://citeseer.ist.psu.edu/713792.html .. [2] Jon Kleinberg, Authoritative sources in a hyperlinked environment Journal of the ACM 46 (5): 604-32, 1999. doi:10.1145/324133.324140. http://www.cs.cornell.edu/home/kleinber/auth.pdf. """ try: import numpy as np except ImportError: raise ImportError(\ "hits_numpy() requires NumPy: http://scipy.org/") if len(G) == 0: return {},{} H=nx.hub_matrix(G,G.nodes()) e,ev=np.linalg.eig(H) m=e.argsort()[-1] # index of maximum eigenvalue h=np.array(ev[:,m]).flatten() A=nx.authority_matrix(G,G.nodes()) e,ev=np.linalg.eig(A) m=e.argsort()[-1] # index of maximum eigenvalue a=np.array(ev[:,m]).flatten() hubs=dict(zip(G.nodes(),h/h.sum())) authorities=dict(zip(G.nodes(),a/a.sum())) return hubs,authorities
def test_empty(self): numpy = pytest.importorskip('numpy') G = networkx.Graph() assert networkx.hits(G) == ({}, {}) assert networkx.hits_numpy(G) == ({}, {}) assert networkx.authority_matrix(G).shape == (0, 0) assert networkx.hub_matrix(G).shape == (0, 0)
def test_empty(self): G = nx.Graph() assert nx.hits(G) == ({}, {}) assert nx.hits_numpy(G) == ({}, {}) assert _hits_python(G) == ({}, {}) assert nx.hits_scipy(G) == ({}, {}) assert nx.authority_matrix(G).shape == (0, 0) assert nx.hub_matrix(G).shape == (0, 0)
def test_empty(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') G = networkx.Graph() assert_equal(networkx.hits(G), ({}, {})) assert_equal(networkx.hits_numpy(G), ({}, {})) assert_equal(networkx.authority_matrix(G).shape, (0, 0)) assert_equal(networkx.hub_matrix(G).shape, (0, 0))
def test_empty(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') G=networkx.Graph() assert_equal(networkx.hits(G),({},{})) assert_equal(networkx.hits_numpy(G),({},{})) assert_equal(networkx.authority_matrix(G).shape,(0,0)) assert_equal(networkx.hub_matrix(G).shape,(0,0))
def _authority_matrix(graph, anchors, normalize): aut = {} node_to_num = dict((node, i) for i, node in enumerate(graph.nodes())) num_to_node = dict(enumerate(graph.nodes())) aut_mat = nx.authority_matrix(graph) for num, node in enumerate(graph.nodes()): d = [] for anchor in anchors: a_num = node_to_num[anchor] d.append(aut_mat[num, a_num]) if normalize: d = normalized(d) aut[node] = np.array(d) return aut
def test_hubs_authority_matrix(self): G = self.G try: import numpy import numpy.linalg H = networkx.hub_matrix(G, nodelist=None) e, ev = numpy.linalg.eig(H) m = e.argsort()[-1] # index of maximum eigenvalue h = numpy.array(ev[:, m]).flatten() A = networkx.authority_matrix(G, nodelist=None) e, ev = numpy.linalg.eig(A) m = e.argsort()[-1] # index of maximum eigenvalue a = numpy.array(ev[:, m]).flatten() h = h / h.sum() a = a / a.sum() h, a = networkx.hits_scipy(G, tol=1.e-08) for (x, y) in zip(sorted(h), self.G.h): assert_almost_equal(x, y, places=5) for (x, y) in zip(sorted(a), self.G.a): assert_almost_equal(x, y, places=5) except ImportError: print "Skipping hub_authority_matrix test"
def test_hubs_authority_matrix(self): G=self.G try: import numpy import numpy.linalg H=networkx.hub_matrix(G,nodelist=None) e,ev=numpy.linalg.eig(H) m=e.argsort()[-1] # index of maximum eigenvalue h=numpy.array(ev[:,m]).flatten() A=networkx.authority_matrix(G,nodelist=None) e,ev=numpy.linalg.eig(A) m=e.argsort()[-1] # index of maximum eigenvalue a=numpy.array(ev[:,m]).flatten() h=h/h.sum() a=a/a.sum() h,a=networkx.hits_scipy(G,tol=1.e-08) for (x,y) in zip(sorted(h),self.G.h): assert_almost_equal(x,y,places=5) for (x,y) in zip(sorted(a),self.G.a): assert_almost_equal(x,y,places=5) except ImportError: print "Skipping hub_authority_matrix test"
def authority_matrix(graph): return nx.authority_matrix(graph)
def driver(): # use this dataset: - leave_one_out_results_linearregression.xlsx data = pd.read_excel('leave_one_out_results_linearregression.xlsx' ) #X_train.csv')#.head(150) column_ID = 'CONNID' feature_1 = 'sertraline' feature_2 = 'venlafaxine' feature_3 = 'escitalopram' feature_4 = 'age' feature_5 = 'gender' feature_6 = 'education' feature_7 = 'Amygdala_Clust1' feature_8 = 'Insula_Clus1' feature_9 = 'Insula_Clust2' feature_10 = 'Nac_Clust1' feature_11 = 'Nac_Clust2' feature_vector = [ feature_1, feature_2, feature_3, feature_4, feature_5, feature_6, feature_7, feature_8, feature_9, feature_10, feature_11 ] data_to_merge = data[[column_ID, feature_vector]].dropna( subset=[feature_vector] ) #.dropna(subset=[column_edge1]).drop_duplicates() #select columns, remove NaN #create graph G = nx.Graph() nodes_for_adding = np.array(data.loc[:, 'CONNID']) #print(nodes_for_adding) #attaribute list attrlist = {} #creating nodes and adding attributes to the nodes. for nid in nodes_for_adding: df = data.loc[data['CONNID'] == nid] dict = {} if len(df.index) != 0: ff = df.to_dict() for key, val in ff.items(): for k, v in val.items(): dict[key] = v attrlist[nid] = dict #create node with attribute list G.add_node(nid, attr_dict=attrlist) #creating edges #algo k = 15 i = 0 n = len(nodes_for_adding) #print("n",n) #build the graph for i in range(1 + k, n - k): k += 1 i += 1 for j in range(i - k, i + k): if i != j and j < n: nid_i = nodes_for_adding[i] nid_j = nodes_for_adding[j] G.add_edge(nid_i, nid_j, weight=1) v_i = G.node[nid_i] v_j = G.node[nid_j] j += 1 ################################################################## #create edges. edges = G.edges() #print("number of edges",len(edges)) #####################node2vec################################ # the value of p and q - to accurately access homophily ############################################################ p = 10 q = .1 nv_G = node2vec.Graph(G, False, 100, .1) nv_G.preprocess_transition_probs() walks = nv_G.simulate_walks(5, 10) model = learn_embeddings(walks) #print("nodes ",G.nodes()) l = list(G.nodes()) walkmap = {} details = [] for node in l: #print(" most similar : ",model.most_similar(str(node))) walkmap[node] = model.most_similar(str(node)) for k, v in walkmap.items(): for i in v: details.append(i[0]) walkmap[k] = details #print("walkmap: ",k,walkmap[k]) details = [] dict = walkmap df = pd.DataFrame.from_dict(dict) scoresdict = {} for k, v in dict.items(): #print(k,v) scoresdict[k] = v #print( scoresdict, len(scoresdict),data.shape) scores = getScores(data, scoresdict, feature_vector) #print ( scores) linearRegression(data, scores) em_gmm(data, scores) correl = corelcoeffpearsonr(data, scores) print(correl) featurelist = {} ############################################################### plt.figure(figsize=(20, 20)) pos = nx.spring_layout(G, k=0.0319) nx.draw_networkx(G, pos, node_size=100, node_color='pink', font_size=8, alpha=0.8) ########################################################## #Graph characteristics. ########################################################################### printNetworkCharactics(G) # Compute the average clustering coefficient print(" avg clustering", nx.average_clustering(G)) hierchical_clustering(G) jaccardIndex(G, 140) localEfficiency(G) pos = nx.spring_layout(G) M = nx.authority_matrix(G, [163, 140]) #print(M) betweenness_centrality(G) pagerank = get_pagerank(G) for p in pagerank: print("pagerank for : ", p)