pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
__init__.py
1 """!
2 
3 @brief pyclustering module for cluster analysis.
4 
5 @authors Andrei Novikov (pyclustering@yandex.ru)
6 @date 2014-2020
7 @copyright BSD-3-Clause
8 
9 """
10 
11 import itertools
12 import math
13 
14 import matplotlib.pyplot as plt
15 import matplotlib.gridspec as gridspec
16 
17 from pyclustering.utils.color import color as color_list
18 
19 
21  """!
22  @brief Description of cluster for representation on canvas.
23 
24  """
25 
26  def __init__(self, cluster, data, marker, markersize, color):
27  """!
28  @brief Constructor of cluster representation on the canvas.
29 
30  @param[in] cluster (list): Single cluster that consists of objects or indexes from data.
31  @param[in] data (list): Objects that should be displayed, can be None if clusters consist of objects instead of indexes.
32  @param[in] marker (string): Type of marker that is used for drawing objects.
33  @param[in] markersize (uint): Size of marker that is used for drawing objects.
34  @param[in] color (string): Color of the marker that is used for drawing objects.
35 
36  """
37 
38  self.cluster = cluster
39 
40 
41  self.data = data
42 
43 
44  self.marker = marker
45 
46 
47  self.markersize = markersize
48 
49 
50  self.color = color
51 
52 
53  self.attributes = []
54 
55 
57  """!
58  @brief Visualizer for cluster in multi-dimensional data.
59  @details This cluster visualizer is useful for clusters in data whose dimension is greater than 3. The
60  multidimensional visualizer helps to overcome 'cluster_visualizer' shortcoming - ability to display
61  clusters in 1D, 2D or 3D dimensional data space.
62 
63  Example of clustering results visualization where 'Iris' is used:
64  @code
65  from pyclustering.utils import read_sample
66  from pyclustering.samples.definitions import FAMOUS_SAMPLES
67  from pyclustering.cluster import cluster_visualizer_multidim
68 
69  # load 4D data sample 'Iris'
70  sample_4d = read_sample(FAMOUS_SAMPLES.SAMPLE_IRIS)
71 
72  # initialize 3 initial centers using K-Means++ algorithm
73  centers = kmeans_plusplus_initializer(sample_4d, 3).initialize()
74 
75  # performs cluster analysis using X-Means
76  xmeans_instance = xmeans(sample_4d, centers)
77  xmeans_instance.process()
78  clusters = xmeans_instance.get_clusters()
79 
80  # visualize obtained clusters in multi-dimensional space
81  visualizer = cluster_visualizer_multidim()
82  visualizer.append_clusters(clusters, sample_4d)
83  visualizer.show(max_row_size=3)
84  @endcode
85 
86  Visualized clustering results of 'Iris' data (multi-dimensional data):
87  @image html xmeans_clustering_famous_iris.png "Fig. 1. X-Means clustering results (data 'Iris')."
88 
89  Sometimes no need to display results in all dimensions. Parameter 'filter' can be used to display only
90  interesting coordinate pairs. Here is an example of visualization of pair coordinates (x0, x1) and (x0, x2) for
91  previous clustering results:
92  @code
93  visualizer = cluster_visualizer_multidim()
94  visualizer.append_clusters(clusters, sample_4d)
95  visualizer.show(pair_filter=[[0, 1], [0, 2]])
96  @endcode
97 
98  Visualized results of specified coordinate pairs:
99  @image html xmeans_clustering_famous_iris_filtered.png "Fig. 2. X-Means clustering results (x0, x1) and (x0, x2) (data 'Iris')."
100 
101  """
102 
103  def __init__(self):
104  """!
105  @brief Constructs cluster visualizer for multidimensional data.
106  @details The visualizer is suitable more data whose dimension is bigger than 3.
107 
108  """
109  self.__clusters = []
110  self.__figure = None
111  self.__grid_spec = None
112 
113 
114  def append_cluster(self, cluster, data = None, marker = '.', markersize = None, color = None):
115  """!
116  @brief Appends cluster for visualization.
117 
118  @param[in] cluster (list): cluster that may consist of indexes of objects from the data or object itself.
119  @param[in] data (list): If defines that each element of cluster is considered as a index of object from the data.
120  @param[in] marker (string): Marker that is used for displaying objects from cluster on the canvas.
121  @param[in] markersize (uint): Size of marker.
122  @param[in] color (string): Color of marker.
123 
124  @return Returns index of cluster descriptor on the canvas.
125 
126  """
127  if len(cluster) == 0:
128  raise ValueError("Empty cluster is provided.")
129 
130  markersize = markersize or 5
131  if color is None:
132  index_color = len(self.__clusters) % len(color_list.TITLES)
133  color = color_list.TITLES[index_color]
134 
135  cluster_descriptor = canvas_cluster_descr(cluster, data, marker, markersize, color)
136  self.__clusters.append(cluster_descriptor)
137 
138 
139  def append_clusters(self, clusters, data=None, marker='.', markersize=None):
140  """!
141  @brief Appends list of cluster for visualization.
142 
143  @param[in] clusters (list): List of clusters where each cluster may consist of indexes of objects from the data or object itself.
144  @param[in] data (list): If defines that each element of cluster is considered as a index of object from the data.
145  @param[in] marker (string): Marker that is used for displaying objects from clusters on the canvas.
146  @param[in] markersize (uint): Size of marker.
147 
148  """
149 
150  for cluster in clusters:
151  self.append_cluster(cluster, data, marker, markersize)
152 
153 
154  def save(self, filename, **kwargs):
155  """!
156 
157  @brief Saves figure to the specified file.
158 
159  @param[in] filename (string): File where the visualized clusters should be stored.
160  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'visible_axis' 'visible_labels', 'visible_grid', 'row_size', 'show').
161 
162  <b>Keyword Args:</b><br>
163  - visible_axis (bool): Defines visibility of axes on each canvas, if True - axes are visible.
164  By default axis of each canvas are not displayed.
165  - visible_labels (bool): Defines visibility of labels on each canvas, if True - labels is displayed.
166  By default labels of each canvas are displayed.
167  - visible_grid (bool): Defines visibility of grid on each canvas, if True - grid is displayed.
168  By default grid of each canvas is displayed.
169  - max_row_size (uint): Maximum number of canvases on one row.
170 
171  """
172 
173  if len(filename) == 0:
174  raise ValueError("Impossible to save visualization to file: empty file path is specified.")
175 
176  self.show(None,
177  visible_axis=kwargs.get('visible_axis', False),
178  visible_labels=kwargs.get('visible_labels', True),
179  visible_grid=kwargs.get('visible_grid', True),
180  max_row_size=kwargs.get('max_row_size', 4))
181  plt.savefig(filename)
182 
183 
184  def show(self, pair_filter=None, **kwargs):
185  """!
186  @brief Shows clusters (visualize) in multi-dimensional space.
187 
188  @param[in] pair_filter (list): List of coordinate pairs that should be displayed. This argument is used as a filter.
189  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'visible_axis' 'visible_labels', 'visible_grid', 'row_size', 'show').
190 
191  <b>Keyword Args:</b><br>
192  - visible_axis (bool): Defines visibility of axes on each canvas, if True - axes are visible.
193  By default axis of each canvas are not displayed.
194  - visible_labels (bool): Defines visibility of labels on each canvas, if True - labels is displayed.
195  By default labels of each canvas are displayed.
196  - visible_grid (bool): Defines visibility of grid on each canvas, if True - grid is displayed.
197  By default grid of each canvas is displayed.
198  - max_row_size (uint): Maximum number of canvases on one row. By default the maximum value is 4.
199  - show (bool): If True - then displays visualized clusters. By default is `True`.
200 
201  """
202 
203  if not len(self.__clusters) > 0:
204  raise ValueError("There is no non-empty clusters for visualization.")
205 
206  cluster_data = self.__clusters[0].data or self.__clusters[0].cluster
207  dimension = len(cluster_data[0])
208 
209  acceptable_pairs = pair_filter or []
210  pairs = []
211  amount_axis = 1
212  axis_storage = []
213 
214  if dimension > 1:
215  pairs = self.__create_pairs(dimension, acceptable_pairs)
216  amount_axis = len(pairs)
217 
218  self.__figure = plt.figure()
219  self.__grid_spec = self.__create_grid_spec(amount_axis, kwargs.get('max_row_size', 4))
220 
221  for index in range(amount_axis):
222  ax = self.__create_canvas(dimension, pairs, index, **kwargs)
223  axis_storage.append(ax)
224 
225  for cluster_descr in self.__clusters:
226  self.__draw_canvas_cluster(axis_storage, cluster_descr, pairs)
227 
228  if kwargs.get('show', True):
229  plt.show()
230 
231 
232  def __create_grid_spec(self, amount_axis, max_row_size):
233  """!
234  @brief Create grid specification for figure to place canvases.
235 
236  @param[in] amount_axis (uint): Amount of canvases that should be organized by the created grid specification.
237  @param[in] max_row_size (max_row_size): Maximum number of canvases on one row.
238 
239  @return (gridspec.GridSpec) Grid specification to place canvases on figure.
240 
241  """
242  row_size = amount_axis
243  if row_size > max_row_size:
244  row_size = max_row_size
245 
246  col_size = math.ceil(amount_axis / row_size)
247  return gridspec.GridSpec(col_size, row_size)
248 
249 
250  def __create_pairs(self, dimension, acceptable_pairs):
251  """!
252  @brief Create coordinate pairs that should be displayed.
253 
254  @param[in] dimension (uint): Data-space dimension.
255  @param[in] acceptable_pairs (list): List of coordinate pairs that should be displayed.
256 
257  @return (list) List of coordinate pairs that should be displayed.
258 
259  """
260  if len(acceptable_pairs) > 0:
261  return acceptable_pairs
262 
263  return list(itertools.combinations(range(dimension), 2))
264 
265 
266  def __create_canvas(self, dimension, pairs, position, **kwargs):
267  """!
268  @brief Create new canvas with user defined parameters to display cluster or chunk of cluster on it.
269 
270  @param[in] dimension (uint): Data-space dimension.
271  @param[in] pairs (list): Pair of coordinates that will be displayed on the canvas. If empty than label will not
272  be displayed on the canvas.
273  @param[in] position (uint): Index position of canvas on a grid.
274  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'visible_axis' 'visible_labels', 'visible_grid').
275 
276  <b>Keyword Args:</b><br>
277  - visible_axis (bool): Defines visibility of axes on each canvas, if True - axes are visible.
278  By default axis are not displayed.
279  - visible_labels (bool): Defines visibility of labels on each canvas, if True - labels is displayed.
280  By default labels are displayed.
281  - visible_grid (bool): Defines visibility of grid on each canvas, if True - grid is displayed.
282  By default grid is displayed.
283 
284  @return (matplotlib.Axis) Canvas to display cluster of chuck of cluster.
285 
286  """
287  visible_grid = kwargs.get('visible_grid', True)
288  visible_labels = kwargs.get('visible_labels', True)
289  visible_axis = kwargs.get('visible_axis', False)
290 
291  ax = self.__figure.add_subplot(self.__grid_spec[position])
292 
293  if dimension > 1:
294  if visible_labels:
295  ax.set_xlabel("x%d" % pairs[position][0])
296  ax.set_ylabel("x%d" % pairs[position][1])
297  else:
298  ax.set_ylim(-0.5, 0.5)
299  ax.set_yticklabels([])
300 
301  if visible_grid:
302  ax.grid(True)
303 
304  if not visible_axis:
305  ax.set_yticklabels([])
306  ax.set_xticklabels([])
307 
308  return ax
309 
310 
311  def __draw_canvas_cluster(self, axis_storage, cluster_descr, pairs):
312  """!
313  @brief Draw clusters.
314 
315  @param[in] axis_storage (list): List of matplotlib axis where cluster dimensional chunks are displayed.
316  @param[in] cluster_descr (canvas_cluster_descr): Canvas cluster descriptor that should be displayed.
317  @param[in] pairs (list): List of coordinates that should be displayed.
318 
319  """
320 
321  for index_axis in range(len(axis_storage)):
322  for item in cluster_descr.cluster:
323  if len(pairs) > 0:
324  self.__draw_cluster_item_multi_dimension(axis_storage[index_axis], pairs[index_axis], item, cluster_descr)
325  else:
326  self.__draw_cluster_item_one_dimension(axis_storage[index_axis], item, cluster_descr)
327 
328 
329  def __draw_cluster_item_multi_dimension(self, ax, pair, item, cluster_descr):
330  """!
331  @brief Draw cluster chunk defined by pair coordinates in data space with dimension greater than 1.
332 
333  @param[in] ax (axis): Matplotlib axis that is used to display chunk of cluster point.
334  @param[in] pair (list): Coordinate of the point that should be displayed.
335  @param[in] item (list): Data point or index of data point.
336  @param[in] cluster_descr (canvas_cluster_descr): Cluster description whose point is visualized.
337 
338  """
339 
340  index_dimension1 = pair[0]
341  index_dimension2 = pair[1]
342 
343  if cluster_descr.data is None:
344  ax.plot(item[index_dimension1], item[index_dimension2],
345  color=cluster_descr.color, marker=cluster_descr.marker, markersize=cluster_descr.markersize)
346  else:
347  ax.plot(cluster_descr.data[item][index_dimension1], cluster_descr.data[item][index_dimension2],
348  color=cluster_descr.color, marker=cluster_descr.marker, markersize=cluster_descr.markersize)
349 
350 
351  def __draw_cluster_item_one_dimension(self, ax, item, cluster_descr):
352  """!
353  @brief Draw cluster point in one dimensional data space..
354 
355  @param[in] ax (axis): Matplotlib axis that is used to display chunk of cluster point.
356  @param[in] item (list): Data point or index of data point.
357  @param[in] cluster_descr (canvas_cluster_descr): Cluster description whose point is visualized.
358 
359  """
360 
361  if cluster_descr.data is None:
362  ax.plot(item[0], 0.0,
363  color=cluster_descr.color, marker=cluster_descr.marker, markersize=cluster_descr.markersize)
364  else:
365  ax.plot(cluster_descr.data[item][0], 0.0,
366  color=cluster_descr.color, marker=cluster_descr.marker, markersize=cluster_descr.markersize)
367 
368 
369 
371  """!
372  @brief Common visualizer of clusters on 1D, 2D or 3D surface.
373  @details Use 'cluster_visualizer_multidim' visualizer in case of data dimension is greater than 3.
374 
375  @see cluster_visualizer_multidim
376 
377  """
378 
379  def __init__(self, number_canvases=1, size_row=1, titles=None):
380  """!
381  @brief Constructor of cluster visualizer.
382 
383  @param[in] number_canvases (uint): Number of canvases that is used for visualization.
384  @param[in] size_row (uint): Amount of canvases that can be placed in one row.
385  @param[in] titles (list): List of canvas's titles.
386 
387  Example:
388  @code
389  # load 2D data sample
390  sample_2d = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1);
391 
392  # load 3D data sample
393  sample_3d = read_sample(FCPS_SAMPLES.SAMPLE_HEPTA);
394 
395  # extract clusters from the first sample using DBSCAN algorithm
396  dbscan_instance = dbscan(sample_2d, 0.4, 2, False);
397  dbscan_instance.process();
398  clusters_sample_2d = dbscan_instance.get_clusters();
399 
400  # extract clusters from the second sample using DBSCAN algorithm
401  dbscan_instance = dbscan(sample_3d, 1, 3, True);
402  dbscan_instance.process();
403  clusters_sample_3d = dbscan_instance.get_clusters();
404 
405  # create plot with two canvases where each row contains 2 canvases.
406  size = 2;
407  row_size = 2;
408  visualizer = cluster_visualizer(size, row_size);
409 
410  # place clustering result of sample_2d to the first canvas
411  visualizer.append_clusters(clusters_sample_2d, sample_2d, 0, markersize = 5);
412 
413  # place clustering result of sample_3d to the second canvas
414  visualizer.append_clusters(clusters_sample_3d, sample_3d, 1, markersize = 30);
415 
416  # show plot
417  visualizer.show();
418  @endcode
419 
420  """
421 
422  self.__number_canvases = number_canvases
423  self.__size_row = size_row
424  self.__canvas_clusters = [ [] for _ in range(number_canvases) ]
425  self.__canvas_dimensions = [ None for _ in range(number_canvases) ]
426  self.__canvas_titles = [ None for _ in range(number_canvases) ]
427 
428  if titles is not None:
429  self.__canvas_titles = titles
430 
431  self.__default_2d_marker_size = 5
432  self.__default_3d_marker_size = 30
433 
434 
435  def append_cluster(self, cluster, data=None, canvas=0, marker='.', markersize=None, color=None):
436  """!
437  @brief Appends cluster to canvas for drawing.
438 
439  @param[in] cluster (list): cluster that may consist of indexes of objects from the data or object itself.
440  @param[in] data (list): If defines that each element of cluster is considered as a index of object from the data.
441  @param[in] canvas (uint): Number of canvas that should be used for displaying cluster.
442  @param[in] marker (string): Marker that is used for displaying objects from cluster on the canvas.
443  @param[in] markersize (uint): Size of marker.
444  @param[in] color (string): Color of marker.
445 
446  @return Returns index of cluster descriptor on the canvas.
447 
448  """
449 
450  if len(cluster) == 0:
451  return
452 
453  if canvas > self.__number_canvases or canvas < 0:
454  raise ValueError("Canvas index '%d' is out of range [0; %d]." % self.__number_canvases or canvas)
455 
456  if color is None:
457  index_color = len(self.__canvas_clusters[canvas]) % len(color_list.TITLES)
458  color = color_list.TITLES[index_color]
459 
460  added_canvas_descriptor = canvas_cluster_descr(cluster, data, marker, markersize, color)
461  self.__canvas_clusters[canvas].append(added_canvas_descriptor)
462 
463  if data is None:
464  dimension = len(cluster[0])
465  if self.__canvas_dimensions[canvas] is None:
466  self.__canvas_dimensions[canvas] = dimension
467  elif self.__canvas_dimensions[canvas] != dimension:
468  raise ValueError("Only clusters with the same dimension of objects can be displayed on canvas.")
469 
470  else:
471  dimension = len(data[0])
472  if self.__canvas_dimensions[canvas] is None:
473  self.__canvas_dimensions[canvas] = dimension
474  elif self.__canvas_dimensions[canvas] != dimension:
475  raise ValueError("Only clusters with the same dimension of objects can be displayed on canvas.")
476 
477  if (dimension < 1) or (dimension > 3):
478  raise ValueError("Only objects with size dimension 1 (1D plot), 2 (2D plot) or 3 (3D plot) "
479  "can be displayed. For multi-dimensional data use 'cluster_visualizer_multidim'.")
480 
481  if markersize is None:
482  if (dimension == 1) or (dimension == 2):
483  added_canvas_descriptor.markersize = self.__default_2d_marker_size
484  elif dimension == 3:
485  added_canvas_descriptor.markersize = self.__default_3d_marker_size
486 
487  return len(self.__canvas_clusters[canvas]) - 1
488 
489 
490  def append_cluster_attribute(self, index_canvas, index_cluster, data, marker = None, markersize = None):
491  """!
492  @brief Append cluster attribure for cluster on specific canvas.
493  @details Attribute it is data that is visualized for specific cluster using its color, marker and markersize if last two is not specified.
494 
495  @param[in] index_canvas (uint): Index canvas where cluster is located.
496  @param[in] index_cluster (uint): Index cluster whose attribute should be added.
497  @param[in] data (list): List of points (data) that represents attribute.
498  @param[in] marker (string): Marker that is used for displaying objects from cluster on the canvas.
499  @param[in] markersize (uint): Size of marker.
500 
501  """
502 
503  cluster_descr = self.__canvas_clusters[index_canvas][index_cluster]
504  attribute_marker = marker
505  if attribute_marker is None:
506  attribute_marker = cluster_descr.marker
507 
508  attribure_markersize = markersize
509  if attribure_markersize is None:
510  attribure_markersize = cluster_descr.markersize
511 
512  attribute_color = cluster_descr.color
513 
514  added_attribute_cluster_descriptor = canvas_cluster_descr(data, None, attribute_marker, attribure_markersize, attribute_color)
515  self.__canvas_clusters[index_canvas][index_cluster].attributes.append(added_attribute_cluster_descriptor)
516 
517 
518  def append_clusters(self, clusters, data=None, canvas=0, marker='.', markersize=None):
519  """!
520  @brief Appends list of cluster to canvas for drawing.
521 
522  @param[in] clusters (list): List of clusters where each cluster may consist of indexes of objects from the data or object itself.
523  @param[in] data (list): If defines that each element of cluster is considered as a index of object from the data.
524  @param[in] canvas (uint): Number of canvas that should be used for displaying clusters.
525  @param[in] marker (string): Marker that is used for displaying objects from clusters on the canvas.
526  @param[in] markersize (uint): Size of marker.
527 
528  """
529 
530  for cluster in clusters:
531  self.append_cluster(cluster, data, canvas, marker, markersize)
532 
533 
534  def set_canvas_title(self, text, canvas = 0):
535  """!
536  @brief Set title for specified canvas.
537 
538  @param[in] text (string): Title for the canvas.
539  @param[in] canvas (uint): Index of the canvas where title should be displayed.
540 
541  """
542 
543  if canvas > self.__number_canvases:
544  raise ValueError("Canvas with index '%d' does not exists (total amount of canvases: '%d')." %
545  canvas, self.__number_canvases)
546 
547  self.__canvas_titles[canvas] = text
548 
549 
550  def get_cluster_color(self, index_cluster, index_canvas):
551  """!
552  @brief Returns cluster color on specified canvas.
553 
554  """
555  return self.__canvas_clusters[index_canvas][index_cluster].color
556 
557 
558  def save(self, filename, **kwargs):
559  """!
560 
561  @brief Saves figure to the specified file.
562 
563  @param[in] filename (string): File where the visualized clusters should be stored.
564  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'invisible_axis', 'visible_grid').
565 
566  <b>Keyword Args:</b><br>
567  - invisible_axis (bool): Defines visibility of axes on each canvas, if `True` - axes are invisible.
568  By default axis are invisible.
569  - visible_grid (bool): Defines visibility of grid on each canvas, if `True` - grid is displayed.
570  By default grid of each canvas is displayed.
571 
572  There is an example how to save visualized clusters to the PNG file without showing them on a screen:
573  @code
574  from pyclustering.cluster import cluster_visualizer
575 
576  data = [[1.1], [1.7], [3.7], [5.3], [2.5], [-1.5], [-0.9], [6.3], [6.5], [8.1]]
577  clusters = [[0, 1, 2, 4, 5, 6], [3, 7, 8, 9]]
578 
579  visualizer = cluster_visualizer()
580  visualizer.append_clusters(clusters, data)
581  visualizer.save("1-dimensional-clustering.png")
582  @endcode
583 
584  """
585 
586  if len(filename) == 0:
587  raise ValueError("Impossible to save visualization to file: empty file path is specified.")
588 
589  invisible_axis = kwargs.get('invisible_axis', True)
590  visible_grid = kwargs.get('visible_grid', True)
591 
592  self.show(None, invisible_axis, visible_grid, False)
593  plt.savefig(filename)
594 
595 
596  def show(self, figure=None, invisible_axis=True, visible_grid=True, display=True, shift=None):
597  """!
598  @brief Shows clusters (visualize).
599 
600  @param[in] figure (fig): Defines requirement to use specified figure, if None - new figure is created for drawing clusters.
601  @param[in] invisible_axis (bool): Defines visibility of axes on each canvas, if True - axes are invisible.
602  @param[in] visible_grid (bool): Defines visibility of grid on each canvas, if True - grid is displayed.
603  @param[in] display (bool): Defines requirement to display clusters on a stage, if True - clusters are displayed,
604  if False - plt.show() should be called by user."
605  @param[in] shift (uint): Force canvas shift value - defines canvas index from which custers should be visualized.
606 
607  @return (fig) Figure where clusters are shown.
608 
609  """
610 
611  canvas_shift = shift
612  if canvas_shift is None:
613  if figure is not None:
614  canvas_shift = len(figure.get_axes())
615  else:
616  canvas_shift = 0
617 
618  if figure is not None:
619  cluster_figure = figure
620  else:
621  cluster_figure = plt.figure()
622 
623  maximum_cols = self.__size_row
624  maximum_rows = math.ceil( (self.__number_canvases + canvas_shift) / maximum_cols)
625 
626  grid_spec = gridspec.GridSpec(maximum_rows, maximum_cols)
627 
628  for index_canvas in range(len(self.__canvas_clusters)):
629  canvas_data = self.__canvas_clusters[index_canvas]
630  if len(canvas_data) == 0:
631  continue
632 
633  dimension = self.__canvas_dimensions[index_canvas]
634 
635  #ax = axes[real_index];
636  if (dimension == 1) or (dimension == 2):
637  ax = cluster_figure.add_subplot(grid_spec[index_canvas + canvas_shift])
638  else:
639  ax = cluster_figure.add_subplot(grid_spec[index_canvas + canvas_shift], projection='3d')
640 
641  if len(canvas_data) == 0:
642  plt.setp(ax, visible=False)
643 
644  for cluster_descr in canvas_data:
645  self.__draw_canvas_cluster(ax, dimension, cluster_descr)
646 
647  for attribute_descr in cluster_descr.attributes:
648  self.__draw_canvas_cluster(ax, dimension, attribute_descr)
649 
650  if invisible_axis is True:
651  ax.xaxis.set_ticklabels([])
652  ax.yaxis.set_ticklabels([])
653 
654  if dimension == 3:
655  ax.zaxis.set_ticklabels([])
656 
657  if self.__canvas_titles[index_canvas] is not None:
658  ax.set_title(self.__canvas_titles[index_canvas])
659 
660  ax.grid(visible_grid)
661 
662  if display is True:
663  plt.show()
664 
665  return cluster_figure
666 
667 
668  def __draw_canvas_cluster(self, ax, dimension, cluster_descr):
669  """!
670  @brief Draw canvas cluster descriptor.
671 
672  @param[in] ax (Axis): Axis of the canvas where canvas cluster descriptor should be displayed.
673  @param[in] dimension (uint): Canvas dimension.
674  @param[in] cluster_descr (canvas_cluster_descr): Canvas cluster descriptor that should be displayed.
675 
676  @return (fig) Figure where clusters are shown.
677 
678  """
679 
680  cluster = cluster_descr.cluster
681  data = cluster_descr.data
682  marker = cluster_descr.marker
683  markersize = cluster_descr.markersize
684  color = cluster_descr.color
685 
686  for item in cluster:
687  if dimension == 1:
688  if data is None:
689  ax.plot(item[0], 0.0, color = color, marker = marker, markersize = markersize)
690  else:
691  ax.plot(data[item][0], 0.0, color = color, marker = marker, markersize = markersize)
692 
693  elif dimension == 2:
694  if data is None:
695  ax.plot(item[0], item[1], color = color, marker = marker, markersize = markersize)
696  else:
697  ax.plot(data[item][0], data[item][1], color = color, marker = marker, markersize = markersize)
698 
699  elif dimension == 3:
700  if data is None:
701  ax.scatter(item[0], item[1], item[2], c = color, marker = marker, s = markersize)
702  else:
703  ax.scatter(data[item][0], data[item][1], data[item][2], c = color, marker = marker, s = markersize)
pyclustering.cluster.canvas_cluster_descr
Description of cluster for representation on canvas.
Definition: __init__.py:20
pyclustering.cluster.cluster_visualizer.append_cluster
def append_cluster(self, cluster, data=None, canvas=0, marker='.', markersize=None, color=None)
Appends cluster to canvas for drawing.
Definition: __init__.py:435
pyclustering.cluster.canvas_cluster_descr.__init__
def __init__(self, cluster, data, marker, markersize, color)
Constructor of cluster representation on the canvas.
Definition: __init__.py:26
pyclustering.cluster.cluster_visualizer_multidim
Visualizer for cluster in multi-dimensional data.
Definition: __init__.py:56
pyclustering.cluster.cluster_visualizer
Common visualizer of clusters on 1D, 2D or 3D surface.
Definition: __init__.py:370
pyclustering.cluster.cluster_visualizer_multidim.show
def show(self, pair_filter=None, **kwargs)
Shows clusters (visualize) in multi-dimensional space.
Definition: __init__.py:184
pyclustering.cluster.cluster_visualizer_multidim.append_clusters
def append_clusters(self, clusters, data=None, marker='.', markersize=None)
Appends list of cluster for visualization.
Definition: __init__.py:139
pyclustering.cluster.cluster_visualizer_multidim.__clusters
__clusters
Definition: __init__.py:109
pyclustering.cluster.cluster_visualizer.__default_3d_marker_size
__default_3d_marker_size
Definition: __init__.py:432
pyclustering.cluster.canvas_cluster_descr.markersize
markersize
Size of marker that is used for drawing objects.
Definition: __init__.py:47
pyclustering.cluster.cluster_visualizer_multidim.__draw_canvas_cluster
def __draw_canvas_cluster(self, axis_storage, cluster_descr, pairs)
Draw clusters.
Definition: __init__.py:311
pyclustering.cluster.cluster_visualizer.__number_canvases
__number_canvases
Definition: __init__.py:422
pyclustering.cluster.cluster_visualizer.__default_2d_marker_size
__default_2d_marker_size
Definition: __init__.py:431
pyclustering.cluster.cluster_visualizer_multidim.__figure
__figure
Definition: __init__.py:110
pyclustering.cluster.cluster_visualizer.__canvas_dimensions
__canvas_dimensions
Definition: __init__.py:425
pyclustering.cluster.canvas_cluster_descr.cluster
cluster
Cluster that may consist of objects or indexes of objects from data.
Definition: __init__.py:38
pyclustering.cluster.cluster_visualizer.__canvas_titles
__canvas_titles
Definition: __init__.py:426
pyclustering.cluster.canvas_cluster_descr.marker
marker
Marker that is used for drawing objects.
Definition: __init__.py:44
pyclustering.cluster.canvas_cluster_descr.attributes
attributes
Attribures of the clusters - additional collections of data points that are regarded to the cluster.
Definition: __init__.py:53
pyclustering.cluster.cluster_visualizer.__size_row
__size_row
Definition: __init__.py:423
pyclustering.cluster.cluster_visualizer.__canvas_clusters
__canvas_clusters
Definition: __init__.py:424
pyclustering.cluster.canvas_cluster_descr.color
color
Color that is used for coloring marker.
Definition: __init__.py:50
pyclustering.cluster.cluster_visualizer.append_cluster_attribute
def append_cluster_attribute(self, index_canvas, index_cluster, data, marker=None, markersize=None)
Append cluster attribure for cluster on specific canvas.
Definition: __init__.py:490
pyclustering.cluster.cluster_visualizer_multidim.__draw_cluster_item_one_dimension
def __draw_cluster_item_one_dimension(self, ax, item, cluster_descr)
Draw cluster point in one dimensional data space.
Definition: __init__.py:351
pyclustering.cluster.cluster_visualizer_multidim.__init__
def __init__(self)
Constructs cluster visualizer for multidimensional data.
Definition: __init__.py:103
pyclustering.cluster.cluster_visualizer.__draw_canvas_cluster
def __draw_canvas_cluster(self, ax, dimension, cluster_descr)
Draw canvas cluster descriptor.
Definition: __init__.py:668
pyclustering.cluster.cluster_visualizer_multidim.__create_grid_spec
def __create_grid_spec(self, amount_axis, max_row_size)
Create grid specification for figure to place canvases.
Definition: __init__.py:232
pyclustering.cluster.cluster_visualizer_multidim.append_cluster
def append_cluster(self, cluster, data=None, marker='.', markersize=None, color=None)
Appends cluster for visualization.
Definition: __init__.py:114
pyclustering.cluster.canvas_cluster_descr.data
data
Data where objects are stored.
Definition: __init__.py:41
pyclustering.cluster.cluster_visualizer_multidim.__create_canvas
def __create_canvas(self, dimension, pairs, position, **kwargs)
Create new canvas with user defined parameters to display cluster or chunk of cluster on it.
Definition: __init__.py:266
pyclustering.cluster.cluster_visualizer.save
def save(self, filename, **kwargs)
Saves figure to the specified file.
Definition: __init__.py:558
pyclustering.cluster.cluster_visualizer.__init__
def __init__(self, number_canvases=1, size_row=1, titles=None)
Constructor of cluster visualizer.
Definition: __init__.py:379
pyclustering.cluster.cluster_visualizer.append_clusters
def append_clusters(self, clusters, data=None, canvas=0, marker='.', markersize=None)
Appends list of cluster to canvas for drawing.
Definition: __init__.py:518
pyclustering.utils.color
Colors used by pyclustering library for visualization.
Definition: color.py:1
pyclustering.cluster.cluster_visualizer_multidim.save
def save(self, filename, **kwargs)
Saves figure to the specified file.
Definition: __init__.py:154
pyclustering.cluster.cluster_visualizer.show
def show(self, figure=None, invisible_axis=True, visible_grid=True, display=True, shift=None)
Shows clusters (visualize).
Definition: __init__.py:596
pyclustering.cluster.cluster_visualizer.set_canvas_title
def set_canvas_title(self, text, canvas=0)
Set title for specified canvas.
Definition: __init__.py:534
pyclustering.cluster.cluster_visualizer_multidim.__draw_cluster_item_multi_dimension
def __draw_cluster_item_multi_dimension(self, ax, pair, item, cluster_descr)
Draw cluster chunk defined by pair coordinates in data space with dimension greater than 1.
Definition: __init__.py:329
pyclustering.cluster.cluster_visualizer_multidim.__create_pairs
def __create_pairs(self, dimension, acceptable_pairs)
Create coordinate pairs that should be displayed.
Definition: __init__.py:250
pyclustering.cluster.cluster_visualizer.get_cluster_color
def get_cluster_color(self, index_cluster, index_canvas)
Returns cluster color on specified canvas.
Definition: __init__.py:550
pyclustering.cluster.cluster_visualizer_multidim.__grid_spec
__grid_spec
Definition: __init__.py:111