Mercurial Hosting > traffic-intelligence
diff python/ml.py @ 952:a9b2beef0db4
loading and assigning motion patterns works
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 24 Jul 2017 21:22:18 -0400 |
parents | d6c1c05d11f5 |
children | 989917b1ed85 |
line wrap: on
line diff
--- a/python/ml.py Mon Jul 24 00:28:52 2017 -0400 +++ b/python/ml.py Mon Jul 24 21:22:18 2017 -0400 @@ -167,9 +167,9 @@ indices = [i for i in range(similarities.shape[0]) if labels[i] == smallestClusterIndex] return prototypeIndices, labels -def prototypeCluster(instances, similarities, minSimilarity, similarityFunc = None, minClusterSize = 0, optimizeCentroid = True, randomInitialization = False, assign = True, initialPrototypeIndices = None): +def prototypeCluster(instances, similarities, minSimilarity, similarityFunc = None, minClusterSize = 0, optimizeCentroid = True, randomInitialization = False, initialPrototypeIndices = None): '''Finds exemplar (prototype) instance that represent each cluster - Returns the prototype indices (in the instances list) and the cluster label of each instance + Returns the prototype indices (in the instances list) the elements in the instances list must have a length (method __len__), or one can use the random initialization the positions in the instances list corresponds to the similarities @@ -236,14 +236,9 @@ newCentroidIdx = clusterIndices[clusterSimilarities.sum(0).argmax()] if prototypeIndices[label] != newCentroidIdx: prototypeIndices[label] = newCentroidIdx - elif randomInitialization: # replace prototype by current instance i if longer - if len(instances[prototypeIndices[label]]) < len(instances[i]): - prototypeIndices[label] = i - - if assign: - return assignToPrototypeClusters(instances, prototypeIndices, similarities, minSimilarity, similarityFunc, minClusterSize) - else: - return prototypeIndices, None + elif len(instances[prototypeIndices[label]]) < len(instances[i]): # replace prototype by current instance i if longer # otherwise, possible to test if randomInitialization or initialPrototypes is not None + prototypeIndices[label] = i + return prototypeIndices def computeClusterSizes(labels, prototypeIndices, outlierIndex = -1): clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices}