def pagerank(matrix, d_factor=0.85):
 size = len(matrix)
 epsilon = 0.0001
 matrix = matrix.copy()
 for i in xrange(0, size):
  col_sum = matrix[:, i].sum()
  if col_sum:
   matrix[:, i] /= col_sum
 e = (((1.0 - d_factor) / size) * numpy.ones((size, size)))
 matrix = ((d_factor * matrix) + e)
 result = (numpy.ones(size) / size)
 prev = (numpy.ones(size) / size)
 iteration = 0
 while True:
  result = numpy.dot(matrix, result)
  result /= result.sum()
  diff = numpy.abs((result - prev)).sum()
  print ('Iteration %d, change %f' % (iteration, diff))
  if (diff < epsilon):
   break
  prev = result
  iteration += 1
 return result

def pagerank ( matrix , d_factor = 0.85 ) : size = len ( matrix ) epsilon = 0.0001 matrix = matrix . copy ( ) for i in xrange ( 0 , size ) : col_sum = matrix [ : , i ] . sum ( ) if col_sum : matrix [ : , i ] /= col_sum e = ( ( ( 1.0 - d_factor ) / size ) * numpy . ones ( ( size , size ) ) ) matrix = ( ( d_factor * matrix ) + e ) result = ( numpy . ones ( size ) / size ) prev = ( numpy . ones ( size ) / size ) iteration = 0 while True : result = numpy . dot ( matrix , result ) result /= result . sum ( ) diff = numpy . abs ( ( result - prev ) ) . sum ( ) print ( 'Iteration %d, change %f' % ( iteration , diff ) ) if ( diff < epsilon ) : break prev = result iteration += 1 return result
calculate the pagerank vector of a given adjacency matrix .