Mercurial Hosting > traffic-intelligence
changeset 729:dad99b86a104 dev
merge with default
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 10 Aug 2015 17:52:19 -0400 |
parents | 4e89341edd29 (diff) c6d4ea05a2d0 (current diff) |
children | a850a4f92735 |
files | |
diffstat | 1 files changed, 19 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/python/events.py Mon Aug 10 01:06:59 2015 -0400 +++ b/python/events.py Mon Aug 10 17:52:19 2015 -0400 @@ -295,13 +295,15 @@ print('unknown type of point: '+pointType) return allPoints -def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): +def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity, minClusterSize = None): '''Finds exemplar indicator time series for all interactions Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) if an indicator profile (time series) is different enough (<minSimilarity), it will become a new prototype. - Non-prototype interactions will be assigned to an existing prototype''' + Non-prototype interactions will be assigned to an existing prototype + if minClusterSize is not None, the clusters will be refined by removing iteratively the smallest clusters + and reassigning all elements in the cluster until no cluster is smaller than minClusterSize''' # sort indicators based on length indices = range(similarityMatrix.shape[0]) @@ -320,13 +322,22 @@ prototypeIndices.append(i) # assignment - labels = [-1]*similarityMatrix.shape[0] indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices] - for i in prototypeIndices: - labels[i] = i - for i in indices: - prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() - labels[i] = prototypeIndices[prototypeIndex] + assign = True + while assign: + labels = [-1]*similarityMatrix.shape[0] + for i in prototypeIndices: + labels[i] = i + for i in indices: + prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() + labels[i] = prototypeIndices[prototypeIndex] + clusterSizes = {i: sum(np.array(labels) == i) for i in prototypeIndices} + smallestClusterIndex = min(clusterSizes, key = clusterSizes.get) + assign = (clusterSizes[smallestClusterIndex] < minClusterSize) + print prototypeIndices, smallestClusterIndex, clusterSizes[smallestClusterIndex] + if assign: + prototypeIndices.remove(smallestClusterIndex) + indices.append(smallestClusterIndex) return prototypeIndices, labels