Skip to content

Instantly share code, notes, and snippets.

@smly
Created February 27, 2009 21:29
Show Gist options
  • Save smly/71710 to your computer and use it in GitHub Desktop.
Save smly/71710 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# HITS (on memory)
from numarray import *
from numarray.matrix import *
EPS = 1.0e-10
class HITS():
def __init__(self, adj_matrix):
self._cnt = 0
self._err = 0
self._dim = adj_matrix[0].size()
self._adj = adj_matrix
def calc(self):
self._cnt = 0
auth = ones( self._dim, Float64 )
hub = ones( self._dim, Float64 )
while 1:
self._cnt += 1
auth_prev = auth
hub_prev = hub
temp_auth = dot(transpose(self._adj), hub)
auth = 1. / sum(temp_auth) * temp_auth
temp_hub = dot(self._adj, auth)
hub = 1. / sum(temp_hub) * temp_hub
self._err = self.diff(auth, auth_prev, hub, hub_prev)
if (self._err < EPS): break
return (auth, hub)
def diff(self, a, a_, h, h_):
err = 0
for i in range(self._dim):
err += absolute(h_[i] - h[i])
err += absolute(a_[i] - a[i])
return err
Adj = Matrix([array([0, 0, 1, 1]),
array([0, 0, 1, 1]),
array([1, 1, 0, 0]),
array([0, 1, 1, 0])])
hits = HITS(Adj)
(auth,hub) = hits.calc()
print "auth:", auth
print "hub:", hub
print "error:", hits._err
print "iteration:", hits._cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment