User Tools

Site Tools


chicoutimi_2016:eigenvector

Visual Analytics Course

Instructor: Guy Melançon (email: Guy dot Melancon at labri dot fr)

Chicoutimi Summer 2016 roadmap / May 3, 3pm course

Mesure EigenVector

EigenVector.py
from tulip import *
import numpy
import time
 
class EigenVector(object):
	"""docstring for EigenVector"""
	def __init__(self, graph):
		super(EigenVector, self).__init__()
		self.graph = graph
		self.adjacency_matrix = numpy.matrix([[1 if self.graph.existEdge(n,m,False).isValid() else 0 \
									for n in self.graph.getNodes()] \
									for m in self.graph.getNodes()])
 
	def eigenvector_metric(self, precision = 0.1):
		prev_vector = numpy.matrix([[1.0 for n in self.graph.getNodes()]])
		prev_vector /= self.signed_magnitude(prev_vector)
		vector = self.adjacency_matrix.dot(numpy.transpose(prev_vector))
		vector /= self.signed_magnitude(vector)
		while numpy.linalg.norm(vector - prev_vector) > precision:
			prev_vector = vector
			vector = self.adjacency_matrix.dot(prev_vector)
			vector /= self.signed_magnitude(vector)
			print 'prev vect'
			print prev_vector
			print 'new vector'
			print vector
			print 'norm diff ', numpy.linalg.norm(vector - prev_vector)
			#time.sleep(0.5)
		return vector
 
	def signed_magnitude(self, vector):
		max_mag = vector.item(0)
		for i in range(len(vector)):
			if numpy.abs(vector.item(i)) > numpy.abs(max_mag):
				max_mag = vector.item(i)
		return max_mag
 
def main(graph):
	eig = EigenVector(graph)
	print 'matrix shape ', eig.adjacency_matrix.shape
	print eig.adjacency_matrix
	v = eig.eigenvector_metric(0.0001)
	print v
	print eig.adjacency_matrix.dot(v)
	print
	print numpy.linalg.eigvals(eig.adjacency_matrix)
/net/html/perso/melancon/Visual_Analytics_Course/data/pages/chicoutimi_2016/eigenvector.txt · Last modified: 2016/05/26 03:46 by melancon