3 @brief Module for representing clustering results. 5 @authors Andrei Novikov (pyclustering@yandex.ru) 7 @copyright GNU Public License 9 @cond GNU_PUBLIC_LICENSE 10 PyClustering is free software: you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 15 PyClustering is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 20 You should have received a copy of the GNU General Public License 21 along with this program. If not, see <http://www.gnu.org/licenses/>. 27 from enum
import IntEnum;
32 @brief Enumeration of encoding types (index labeling, index list separation, object list separation). 37 CLUSTER_INDEX_LABELING = 0;
40 CLUSTER_INDEX_LIST_SEPARATION = 1;
43 CLUSTER_OBJECT_LIST_SEPARATION = 2;
48 @brief Provides service to change clustering result representation. 52 # load list of points for cluster analysis 53 sample = read_sample(path); 55 # create instance of K-Means algorithm 56 kmeans_instance = kmeans(sample, [ [0.0, 0.1], [2.5, 2.6] ]); 58 # run cluster analysis and obtain results 59 kmeans_instance.process(); 60 clusters = kmeans_instance.get_clusters(); 62 # by default k-means returns representation CLUSTER_INDEX_LIST_SEPARATION 63 type_repr = kmeans_instance.get_cluster_encoding(); 64 encoder = cluster_encoder(type_repr, clusters, sample); 66 # change representation from index list to label list 67 representor.set_encoding(type_encoding.CLUSTER_INDEX_LABELING); 69 # change representation from label to object list 70 representor.set_encoding(type_encoding.CLUSTER_OBJECT_LIST_SEPARATION); 76 @brief Constructor of clustering result representor. 78 @param[in] encoding (type_encoding): Type of clusters representation (index list, object list or labels). 79 @param[in] clusters (list): Current clusters representation. 80 @param[in] data (list): Data that corresponds to clusters. 92 @brief Returns current cluster representation. 100 @brief Returns clusters representation. 108 @brief Returns data that corresponds to clusters. 116 @brief Change clusters encoding to specified type (index list, object list, labeling). 118 @param[in] encoding (type_encoding): New type of clusters representation. 126 if (encoding == type_encoding.CLUSTER_INDEX_LIST_SEPARATION):
133 if (encoding == type_encoding.CLUSTER_INDEX_LABELING):
140 if (encoding == type_encoding.CLUSTER_INDEX_LABELING):
149 def __convert_index_to_label(self):
150 clusters = [0] * len(self.
__data);
154 for index_object
in cluster:
155 clusters[index_object] = index_cluster;
162 def __convert_index_to_object(self):
163 clusters = [ []
for _
in range(len(self.
__clusters)) ];
164 for index_cluster
in range(len(self.
__clusters)):
165 for index_object
in self.
__clusters[index_cluster]:
166 data_object = self.
__data[index_object];
167 clusters[index_cluster].append(data_object);
172 def __convert_object_to_label(self):
174 clusters = [0] * len(self.
__data);
178 for data_object
in cluster:
180 hashable_data_object = str(data_object);
181 if (hashable_data_object
in positions):
182 index_object = self.
__data.index(data_object, positions[hashable_data_object] + 1);
184 index_object = self.
__data.index(data_object);
186 clusters[index_object] = index_cluster;
187 positions[hashable_data_object] = index_object;
194 def __convert_object_to_index(self):
196 clusters = [ []
for _
in range(len(self.
__clusters)) ];
197 for index_cluster
in range(len(self.
__clusters)):
198 for data_object
in self.
__clusters[index_cluster]:
200 hashable_data_object = str(data_object);
201 if (hashable_data_object
in positions):
202 index_object = self.
__data.index(data_object, positions[hashable_data_object] + 1);
204 index_object = self.
__data.index(data_object);
206 clusters[index_cluster].append(index_object);
207 positions[hashable_data_object] = index_object;
212 def __convert_label_to_index(self):
213 clusters = [ []
for _
in range(max(self.
__clusters) + 1) ];
215 for index_object
in range(len(self.
__data)):
216 index_cluster = self.
__clusters[index_object];
217 clusters[index_cluster].append(index_object);
222 def __convert_label_to_object(self):
223 clusters = [ []
for _
in range(max(self.
__clusters) + 1) ];
225 for index_object
in range(len(self.
__data)):
226 index_cluster = self.
__clusters[index_object];
227 clusters[index_cluster].append(self.
__data[index_object]);
def __convert_index_to_label(self)
def set_encoding(self, encoding)
Change clusters encoding to specified type (index list, object list, labeling).
def __convert_index_to_object(self)
Enumeration of encoding types (index labeling, index list separation, object list separation)...
def get_encoding(self)
Returns current cluster representation.
def __convert_label_to_index(self)
def get_clusters(self)
Returns clusters representation.
Provides service to change clustering result representation.
def get_data(self)
Returns data that corresponds to clusters.
def __convert_label_to_object(self)
def __convert_object_to_index(self)
def __init__(self, encoding, clusters, data)
Constructor of clustering result representor.
def __convert_object_to_label(self)