User Tools

Site Tools


k-means

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

k-means [2012/11/16 10:18]
melancon created
k-means [2012/11/16 10:23] (current)
melancon
Line 15: Line 15:
 from random import * from random import *
  
-class KMeans:+class KMeansEuclidean: 
 + 
 +    '''​ 
 +    Computes k clusters based on the classical K-means algorithm 
 +    Because nodes are colored using predefined colors, 
 +    a maximal number of clusters is imposed 
 +    '''​
  
     def __init__(self,​ graph, k):     def __init__(self,​ graph, k):
-        ​Self.graph = graph+        ​self.graph = graph
         self.k = k         self.k = k
         self.gravitors = []         self.gravitors = []
Line 28: Line 34:
         self.yellow = tlp.Color(255,​ 255, 50, 255)         self.yellow = tlp.Color(255,​ 255, 50, 255)
         self.orange = tlp.Color(255,​ 125, 75, 255)         self.orange = tlp.Color(255,​ 125, 75, 255)
-        self.purple = tlp.Color(120, 0, 180, 255) +        self.purple = tlp.Color(170, 0, 255) 
-        self.cyan = tlp.Color(255, ​85, 255, 255+        self.pink = tlp.Color(255, ​0, 255
-        self.fullColorSet = [self.blue, self.green, self.cyan, self.yellow,​ self.orange,​ self.red, self.purple]+        self.brown = tlp.Color(17085, 0
 +        self.fullColorSet = [self.red, self.blue, self.green, self.yellow,​ self.orange,​ self.purple, self.pink, self.brown]
         self.epsilon = -1         self.epsilon = -1
         '''​ used as stopping criterion, will be initialized when calling run() '''​         '''​ used as stopping criterion, will be initialized when calling run() '''​
         ​         ​
-    def run(self, stop):+    def run(self, stop = 0.01): 
 +        if self.k > len(self.fullColorSet):​ 
 +            print "A maximum of ", len(self.fullColorSet),​ " clusters may be specified"​ 
 +            return False
         viewColor = self.graph.getColorProperty("​viewColor"​)         viewColor = self.graph.getColorProperty("​viewColor"​)
         layout = self.graph.getLayoutProperty("​viewLayout"​)         layout = self.graph.getLayoutProperty("​viewLayout"​)
Line 47: Line 57:
                 node = nodes.next()                 node = nodes.next()
                 viewColor.setNodeValue(node,​ self.colors[self.selectGravitor(layout.getNodeValue(node))])                 viewColor.setNodeValue(node,​ self.colors[self.selectGravitor(layout.getNodeValue(node))])
-                updateVisualization() 
             self.epsilon = self.updateGravitors(self.graph,​ viewColor, layout)             self.epsilon = self.updateGravitors(self.graph,​ viewColor, layout)
 +            updateVisualization()
                     ​                     ​
         ​         ​
/net/html/perso/melancon/Visual_Analytics_Course/data/pages/k-means.txt · Last modified: 2012/11/16 10:23 by melancon