def cluster(self, data, n_clusters): n_samples, _ = shape(data) assert self.n_ants < n_samples, "number of ants must be lower than number of samples" bestscore = float('inf') bestcentroids = None bestweights = None pheromone = self.t0 * ones((n_samples, n_clusters)) for it in range(self.n_iter): for _ in range(self.n_ants): # memory = -1 * ones(n_samples, dtype='int') weights = zeros((n_samples, n_clusters), dtype='bool') centroids = array([data[randint(n_samples), :] for _ in range(n_clusters)], copy=True) for i in permutation(n_samples): scores = self.centroidscore(data[i, :], centroids, pheromone[i, :], self.beta) if randfloat() < self.q0: j = argmax(scores) # exploit else: j = choice(n_clusters, p=(scores / sum(scores))) # explore weights[i, j] = True centroids[j, :] = average(data, axis=0, weights=weights[:, j]) currentscore = score(data, centroids=centroids, norm=self.beta) if currentscore < bestscore: bestscore = currentscore bestcentroids = copy(centroids) bestweights = copy(weights) pheromone = (self.ro * pheromone) + ((1.0 / bestscore) * bestweights) if it % self.printfreq == 0: print "Ant Colony iteration", it, "best score:", bestscore return getlabels(data, centroids=bestcentroids, norm=self.beta)
def getnewcentroids(self, data, locations, k): a = randint(self.n_bees) theta = randfloat(-1.0, 1.0) newcentroids = locations[k, :, :] + (theta * (locations[k, :, :] - locations[a, :, :])) newscore = score(data, centroids=newcentroids, norm=self.norm) return newcentroids, newscore