Mercurial Hosting > traffic-intelligence
diff python/ml.py @ 526:21bdeb29f855
corrected bug in initialization of lists and loading trajectories from vissim files
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 20 Jun 2014 17:45:32 -0400 |
parents | 727e3c529519 |
children | 39de5c532559 |
line wrap: on
line diff
--- a/python/ml.py Fri Jun 20 00:20:29 2014 -0400 +++ b/python/ml.py Fri Jun 20 17:45:32 2014 -0400 @@ -58,7 +58,7 @@ Either the initialCentroids or k are passed''' pass -def assignCluster(data, similarFunc, initialCentroids = [], shuffleData = True): +def assignCluster(data, similarFunc, initialCentroids = None, shuffleData = True): '''k-means algorithm with similarity function Two instances should be in the same cluster if the sameCluster function returns true for two instances. It is supposed that the average centroid of a set of instances can be computed, using the function. The number of clusters will be determined accordingly @@ -71,10 +71,10 @@ localdata = copy(data) # shallow copy to avoid modifying data if shuffleData: shuffle(localdata) - if initialCentroids: + if initialCentroids == None: + centroids = [Centroid(localdata[0])] + else: centroids = deepcopy(initialCentroids) - else: - centroids = [Centroid(localdata[0])] for instance in localdata[1:]: i = 0 while i<len(centroids) and not similarFunc(centroids[i].instance, instance):