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. 120 @return (cluster_encoder) Return itself. 128 if (encoding == type_encoding.CLUSTER_INDEX_LIST_SEPARATION):
135 if (encoding == type_encoding.CLUSTER_INDEX_LABELING):
142 if (encoding == type_encoding.CLUSTER_INDEX_LABELING):
152 def __convert_index_to_label(self):
153 clusters = [0] * len(self.
__data)
157 for index_object
in cluster:
158 clusters[index_object] = index_cluster
165 def __convert_index_to_object(self):
166 clusters = [ []
for _
in range(len(self.
__clusters)) ];
167 for index_cluster
in range(len(self.
__clusters)):
168 for index_object
in self.
__clusters[index_cluster]:
169 data_object = self.
__data[index_object];
170 clusters[index_cluster].append(data_object);
175 def __convert_object_to_label(self):
177 clusters = [0] * len(self.
__data);
181 for data_object
in cluster:
183 hashable_data_object = str(data_object);
184 if (hashable_data_object
in positions):
185 index_object = self.
__data.index(data_object, positions[hashable_data_object] + 1);
187 index_object = self.
__data.index(data_object);
189 clusters[index_object] = index_cluster;
190 positions[hashable_data_object] = index_object;
197 def __convert_object_to_index(self):
199 clusters = [ []
for _
in range(len(self.
__clusters)) ];
200 for index_cluster
in range(len(self.
__clusters)):
201 for data_object
in self.
__clusters[index_cluster]:
203 hashable_data_object = str(data_object);
204 if (hashable_data_object
in positions):
205 index_object = self.
__data.index(data_object, positions[hashable_data_object] + 1);
207 index_object = self.
__data.index(data_object);
209 clusters[index_cluster].append(index_object);
210 positions[hashable_data_object] = index_object;
215 def __convert_label_to_index(self):
216 clusters = [ []
for _
in range(max(self.
__clusters) + 1) ];
218 for index_object
in range(len(self.
__data)):
219 index_cluster = self.
__clusters[index_object];
220 clusters[index_cluster].append(index_object);
225 def __convert_label_to_object(self):
226 clusters = [ []
for _
in range(max(self.
__clusters) + 1) ];
228 for index_object
in range(len(self.
__data)):
229 index_cluster = self.
__clusters[index_object];
230 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)