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): 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 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): 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 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 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 hub_matrix(graph): return nx.hub_matrix(graph)