def templateClusterAllocation(self, path, cluster_sizes, number_clusters, iterations, maxneighbors): result_testing = False # it's randomized algorithm therefore attempts are required for attempt in range(0, 3, 1): sample = read_sample(path) clarans_instance = clarans(sample, number_clusters, iterations, maxneighbors) clarans_instance.process() clusters = clarans_instance.get_clusters() obtained_cluster_sizes = [len(cluster) for cluster in clusters] total_length = sum(obtained_cluster_sizes) if (total_length != len(sample)): continue cluster_sizes.sort() obtained_cluster_sizes.sort() if (cluster_sizes != obtained_cluster_sizes): continue result_testing = True break assert result_testing == True
def templateClusterAllocation(self, path, cluster_sizes, number_clusters, iterations, maxneighbors): result_testing = False; # it's randomized algorithm therefore attempts are required for attempt in range(0, 5, 1): sample = read_sample(path); clarans_instance = clarans(sample, number_clusters, iterations, maxneighbors); clarans_instance.process(); clusters = clarans_instance.get_clusters(); obtained_cluster_sizes = [len(cluster) for cluster in clusters]; total_length = sum(obtained_cluster_sizes); if (total_length != len(sample)): continue; cluster_sizes.sort(); obtained_cluster_sizes.sort(); if (cluster_sizes != obtained_cluster_sizes): continue; result_testing = True; break; assert result_testing == True;
def cluster(number_clusters, iterations, maxneighbours): data = read_sample('data.data') m_clarans = clarans(data, number_clusters, iterations, maxneighbours) (ticks, result) = timedcall(m_clarans.process) print("Execution time: ", ticks, "\n") clusters = m_clarans.get_clusters() draw_clusters(data, clusters)
def setup(self, **keywords): """ Setup the algorithms """ self._init_() for key in keywords.keys(): setattr(self, key, keywords[key]) if self.method == "bsas": self.obj = bsas(self.data_list, self.maximum_clusters, self.threshold1, ccore=self.ccore) if self.method == "mbsas": self.obj = mbsas(self.data_list, self.maximum_clusters, self.threshold1, ccore=self.ccore) if self.method == "ttsas": self.obj = ttsas(self.data_list, self.threshold1, self.threshold2, ccore=self.ccore) if self.method == "clarans": self.obj = clarans(self.data_list, self.n_clusters, self.numlocal, self.maxneighbor) return
def template_clustering(number_clusters, path, iterations, maxneighbors): sample = read_sample(path); clarans_instance = clarans(sample, number_clusters, iterations, maxneighbors); (ticks, result) = timedcall(clarans_instance.process); print("Sample: ", path, "\t\tExecution time: ", ticks, "\n"); clusters = clarans_instance.get_clusters(); draw_clusters(sample, clusters);
def template_clustering(number_clusters, path, iterations, maxneighbors): sample = read_sample(path); clarans_instance = clarans(sample, number_clusters, iterations, maxneighbors); (ticks, result) = timedcall(clarans_instance.process); print("Sample: ", path, "\t\tExecution time: ", ticks, "\n"); clusters = clarans_instance.get_clusters(); draw_clusters(sample, clusters);
def get_modelo(self, algoritmo, eps, neig): print(algoritmo + ' ' + str(eps) + ' - ' + str(neig)) instance = None if algoritmo == 'AGNES': instance = agglomerative(self.amostras, self.numero_clusters, link=None) elif algoritmo == 'BIRCH': instance = birch(self.amostras, self.numero_clusters, entry_size_limit=10000) elif algoritmo == 'CLARANS': instance = clarans(self.amostras, self.numero_clusters, numlocal=100, maxneighbor=1) elif algoritmo == 'CURE': instance = cure(self.amostras, self.numero_clusters, number_represent_points=5, compression=0.5) elif algoritmo == 'DBSCAN': instance = dbscan(self.amostras, eps=eps, neighbors=neig) elif algoritmo == 'FCM': initial_centers = kmeans_plusplus_initializer( self.amostras, self.numero_clusters).initialize() instance = fcm(self.amostras, initial_centers) elif algoritmo == 'KMEANS': initial_centers = kmeans_plusplus_initializer( self.amostras, self.numero_clusters).initialize() instance = kmeans(self.amostras, initial_centers, tolerance=0.001) elif algoritmo == 'KMEDOIDS': instance = kmedoids(self.amostras, initial_index_medoids=[0, 0, 0, 0, 0, 0, 0], tolerance=0.0001) #ajustar o n_de cluster elif algoritmo == 'OPTICS': instance = optics(self.amostras, eps=eps, minpts=neig) elif algoritmo == 'ROCK': instance = rock(self.amostras, eps=eps, number_clusters=self.numero_clusters, threshold=0.5) else: pass instance.process() lista_agrupada = self.get_lista_agrupada(instance.get_clusters()) lista_agrupada = np.array(lista_agrupada) if (neig != 0): n_grupos = len(np.unique(lista_agrupada)) if n_grupos > self.numero_clusters: lista_agrupada = self.get_modelo(algoritmo, eps, neig + 1) return lista_agrupada
def clarans(cls, number_clusters, num_local, max_neighbour): """ Constructor of the CLARANS clustering.rst algorithm :param number_clusters: the number of clusters to be allocated :param num_local: the number of local minima obtained (amount of iterations for solving the problem). :param max_neighbour: the number of local minima obtained (amount of iterations for solving the problem). :return: the resulting clustering.rst object """ model = clarans(None, number_clusters, num_local, max_neighbour) return cls(model)
def compute_clarans(data, k, max_neighbour, num_local): clarans_instance = clarans(data, k, num_local, max_neighbour) start = timer() clarans_instance.process() end = timer() clusters = clarans_instance.get_clusters() medoids = clarans_instance.get_medoids() seconds = end - start return clusters, medoids, seconds
def cluster(points): global clusters clarans_instance = clarans(points, 4, 8, 12) (ticks, result) = timedcall(clarans_instance.process) print("Execution time : ", ticks, "\n") indices = clarans_instance.get_clusters() medoids = clarans_instance.get_medoids() print("Index of the points that are in a cluster : ", indices) print("The index of medoids that algorithm found to be best : ", medoids) clusters = [] for clique in indices: cluster = [] for i in clique: cluster.append(points[i]) clusters.append(cluster) print(f"Clusters: {clusters}") return clusters
def runCLARANS(self, k, X): cluster_points = {} for q in range(k): cluster_points[q] = list() clarans_instance = clarans(data=X, number_clusters=k, numlocal=5, maxneighbor=5) clarans_instance.process() clusters = clarans_instance.get_clusters() for id_point in range(len(X)): for cluster_id in range(len(clusters)): point_ids_in_cluster = [ int(point_id_in_cluster) for point_id_in_cluster in clusters[cluster_id] ] if (id_point in point_ids_in_cluster): cluster_points[cluster_id].append(X[id_point]) return cluster_points
def process_clarans(sample): instance = clarans(sample, NUMBER_CLUSTERS, 10, 3) (ticks, _) = timedcall(instance.process) return ticks
def process_clarans(sample): instance = clarans(sample, NUMBER_CLUSTERS, 10, 3) (ticks, _) = timedcall(instance.process) return ticks
from pyclustering.cluster.clarans import clarans import pandas as pd df = pd.read_csv('Customers.csv') data = list(df['Age'].interpolate()) print("A peek into the dataset : ", data[:4]) clarans_instance = clarans(data, 3, 5, 4) clarans_instance.process() clusters = clarans_instance.get_clusters() medoids = clarans_instance.get_medoids() print("Index of the points that are in a cluster : ", clusters) print("The index of medoids that algorithm found to be best : ", medoids) #Made with ❤ By Dilip Gehlot
# Spectral Clustering y_pred = SpectralClustering(n_clusters=k).fit_predict(X) plt.scatter(X[:, 0], X[:, 1], c=y_pred) plt.title("Spectral Clustering") plt.show() # CURE cure_instance = cure(data=X, number_cluster=k); cure_instance.process(); clusters = cure_instance.get_clusters(); visualizer = cluster_visualizer(titles=["Cure"]); visualizer.append_clusters(clusters, X); visualizer.show(); # CLARANS clarans_instance = clarans(data=X, number_clusters=k, numlocal=5, maxneighbor=5); clarans_instance.process(); clusters = clarans_instance.get_clusters(); visualizer = cluster_visualizer(titles=["Clarans"]); visualizer.append_clusters(clusters, X); visualizer.show(); # Agglomerative # type_link = [SINGLE_LINK, COMPLETE_LINK, AVERAGE_LINK, CENTROID_LINK] agglo_instance = agglomerative(data=X, number_clusters=k, link=type_link.COMPLETE_LINK); agglo_instance.process(); clusters = agglo_instance.get_clusters(); visualizer = cluster_visualizer(titles=["Agglomerative"]); visualizer.append_clusters(clusters, X); visualizer.show();
X = dataset.iloc[:, [3, 4]].values """! The pyclustering library clarans implementation requires list of lists as its input dataset. Thus we convert the data from numpy array to list. """ data = X.tolist() """! @brief Constructor of clustering algorithm CLARANS. @details The higher the value of maxneighbor, the closer is CLARANS to K-Medoids, and the longer is each search of a local minima. @param[in] data: Input data that is presented as list of points (objects), each point should be represented by list or tuple. @param[in] number_clusters: amount of clusters that should be allocated. @param[in] numlocal: the number of local minima obtained (amount of iterations for solving the problem). @param[in] maxneighbor: the maximum number of neighbors examined. """ clarans_instance = clarans(data, 5, 6, 4) #calls the clarans method 'process' to implement the algortihm (ticks, result) = timedcall(clarans_instance.process) print("Execution time : ", ticks, "\n") #returns the clusters clusters = clarans_instance.get_clusters() #returns the mediods medoids = clarans_instance.get_medoids() vis_clusters = [] for i in range(0, len(X)): for j in range(len(clusters)):