Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ml.py @ 953:989917b1ed85
assign and learn work
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 25 Jul 2017 17:36:07 -0400 |
| parents | a9b2beef0db4 |
| children | ec1682ed999f |
comparison
equal
deleted
inserted
replaced
| 952:a9b2beef0db4 | 953:989917b1ed85 |
|---|---|
| 136 'Represents a cluster, with a prototype id and the list of instances in cluster' | 136 'Represents a cluster, with a prototype id and the list of instances in cluster' |
| 137 def __init__(prototypeId, memberIndices = []): | 137 def __init__(prototypeId, memberIndices = []): |
| 138 self.prototypeId = prototypeId | 138 self.prototypeId = prototypeId |
| 139 self.memberIndices = memberIndices | 139 self.memberIndices = memberIndices |
| 140 | 140 |
| 141 def assignToPrototypeClusters(instances, prototypeIndices, similarities, minSimilarity, similarityFunc = None, minClusterSize = None): | 141 def assignToPrototypeClusters(instances, prototypeIndices, similarities, minSimilarity, similarityFunc = None, minClusterSize = 0): |
| 142 '''Assigns instances to prototypes | 142 '''Assigns instances to prototypes |
| 143 if minClusterSize is not None, the clusters will be refined by removing iteratively the smallest clusters | 143 if minClusterSize is not None, the clusters will be refined by removing iteratively the smallest clusters |
| 144 and reassigning all elements in the cluster until no cluster is smaller than minClusterSize''' | 144 and reassigning all elements in the cluster until no cluster is smaller than minClusterSize''' |
| 145 indices = [i for i in range(len(instances)) if i not in prototypeIndices] | 145 indices = [i for i in range(len(instances)) if i not in prototypeIndices] |
| 146 labels = [-1]*len(instances) | 146 labels = [-1]*len(instances) |
| 164 assign = (clusterSizes[smallestClusterIndex] < minClusterSize) | 164 assign = (clusterSizes[smallestClusterIndex] < minClusterSize) |
| 165 if assign: | 165 if assign: |
| 166 prototypeIndices.remove(smallestClusterIndex) | 166 prototypeIndices.remove(smallestClusterIndex) |
| 167 indices = [i for i in range(similarities.shape[0]) if labels[i] == smallestClusterIndex] | 167 indices = [i for i in range(similarities.shape[0]) if labels[i] == smallestClusterIndex] |
| 168 return prototypeIndices, labels | 168 return prototypeIndices, labels |
| 169 | 169 def prototypeCluster(instances, similarities, minSimilarity, similarityFunc = None, optimizeCentroid = True, randomInitialization = False, initialPrototypeIndices = None): |
| 170 def prototypeCluster(instances, similarities, minSimilarity, similarityFunc = None, minClusterSize = 0, optimizeCentroid = True, randomInitialization = False, initialPrototypeIndices = None): | |
| 171 '''Finds exemplar (prototype) instance that represent each cluster | 170 '''Finds exemplar (prototype) instance that represent each cluster |
| 172 Returns the prototype indices (in the instances list) | 171 Returns the prototype indices (in the instances list) |
| 173 | 172 |
| 174 the elements in the instances list must have a length (method __len__), or one can use the random initialization | 173 the elements in the instances list must have a length (method __len__), or one can use the random initialization |
| 175 the positions in the instances list corresponds to the similarities | 174 the positions in the instances list corresponds to the similarities |
