pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
syncsom.py
1 """!
2 
3 @brief Cluster analysis algorithm: SYNC-SOM
4 @details Implementation based on paper @cite article::syncsom::1.
5 
6 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2020
8 @copyright BSD-3-Clause
9 
10 """
11 
12 
13 from pyclustering.cluster.encoder import type_encoding
14 from pyclustering.cluster.syncnet import syncnet
15 
16 from pyclustering.nnet.som import som, type_conn
17 from pyclustering.nnet import initial_type
18 
19 from pyclustering.utils import euclidean_distance_square
20 
21 
22 class syncsom:
23  """!
24  @brief Class represents clustering algorithm SYNC-SOM. SYNC-SOM is bio-inspired algorithm that is based on oscillatory network
25  that uses self-organized feature map as the first layer.
26 
27  Example:
28  @code
29  # read sample for clustering
30  sample = read_sample(file);
31 
32  # create oscillatory network for cluster analysis where the first layer has
33  # size 10x10 and connectivity radius for objects 1.0.
34  network = syncsom(sample, 10, 10, 1.0);
35 
36  # simulate network (perform cluster analysis) and collect output dynamic
37  (dyn_time, dyn_phase) = network.process(True, 0.998);
38 
39  # obtain encoded clusters
40  encoded_clusters = network.get_som_clusters();
41 
42  # obtain real clusters
43  clusters = network.get_clusters();
44 
45  # show the first layer of the network
46  network.show_som_layer();
47 
48  # show the second layer of the network
49  network.show_sync_layer();
50  @endcode
51 
52  """
53 
54  @property
55  def som_layer(self):
56  """!
57  @brief The first layer of the oscillatory network - self-organized feature map.
58 
59  """
60  return self._som
61 
62 
63  @property
64  def sync_layer(self):
65  """!
66  @brief The second layer of the oscillatory network based on Kuramoto model.
67 
68  """
69  return self._sync
70 
71 
72  def __init__(self, data, rows, cols, radius):
73  """!
74  @brief Constructor of the double layer oscillatory network SYNC-SOM.
75 
76  @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple.
77  @param[in] rows (uint): Rows of neurons (number of neurons in column) in the input layer (self-organized feature map).
78  @param[in] cols (uint): Columns of neurons (number of neurons in row) in the input later (self-organized feature map).
79  @param[in] radius (double): Connectivity radius between objects that defines connection between oscillators in the second layer.
80 
81  """
82 
83  self._data = data
84  self._radius = radius * radius
85 
86  self._som = som(rows, cols, conn_type=type_conn.grid_four, ccore=False) # The first (input) later - SOM layer.
87  self._som_osc_table = list()
88 
89  self._sync = None # The second (output) layer - Sync layer.
90  self._struct = None # Structure of connections between oscillators in the second layer - Sync layer.
91 
92  # For convenience
93  self._analyser = None
94 
95 
96  def process(self, collect_dynamic=False, order=0.999):
97  """!
98  @brief Performs simulation of the oscillatory network.
99 
100  @param[in] collect_dynamic (bool): If True - returns whole dynamic of oscillatory network, otherwise returns only last values of dynamics.
101  @param[in] order (double): Order of process synchronization that should be considered as end of clustering, destributed 0..1.
102 
103  @return (tuple) Dynamic of oscillatory network. If argument 'collect_dynamic' = True, than return dynamic for the whole simulation time,
104  otherwise returns only last values (last step of simulation) of dynamic.
105 
106  @see get_som_clusters()
107  @see get_clusters()
108  """
109 
110  # train self-organization map.
111  self._som.train(self._data, 100)
112 
113  # prepare to build list.
114  weights = list()
115  self._som_osc_table.clear() # must be cleared, if it's used before.
116  for i in range(self._som.size):
117  if self._som.awards[i] > 0:
118  weights.append(self._som.weights[i])
119  self._som_osc_table.append(i)
120 
121  # create oscillatory neural network.
122  self._sync = self.__create_sync_layer(weights)
123  self._analyser = self._sync.process(order, collect_dynamic=collect_dynamic)
124 
125  return self._analyser.time, self._analyser.output
126 
127 
128  def __create_sync_layer(self, weights):
129  """!
130  @brief Creates second layer of the network.
131 
132  @param[in] weights (list): List of weights of SOM neurons.
133 
134  @return (syncnet) Second layer of the network.
135 
136  """
137  sync_layer = syncnet(weights, 0.0, initial_phases = initial_type.RANDOM_GAUSSIAN, ccore=False)
138 
139  for oscillator_index1 in range(0, len(sync_layer)):
140  for oscillator_index2 in range(oscillator_index1 + 1, len(sync_layer)):
141  if self.__has_object_connection(oscillator_index1, oscillator_index2):
142  sync_layer.set_connection(oscillator_index1, oscillator_index2)
143 
144  return sync_layer
145 
146 
147  def __has_object_connection(self, oscillator_index1, oscillator_index2):
148  """!
149  @brief Searches for pair of objects that are encoded by specified neurons and that are connected in line with connectivity radius.
150 
151  @param[in] oscillator_index1 (uint): Index of the first oscillator in the second layer.
152  @param[in] oscillator_index2 (uint): Index of the second oscillator in the second layer.
153 
154  @return (bool) True - if there is pair of connected objects encoded by specified oscillators.
155 
156  """
157  som_neuron_index1 = self._som_osc_table[oscillator_index1]
158  som_neuron_index2 = self._som_osc_table[oscillator_index2]
159 
160  for index_object1 in self._som.capture_objects[som_neuron_index1]:
161  for index_object2 in self._som.capture_objects[som_neuron_index2]:
162  distance = euclidean_distance_square(self._data[index_object1], self._data[index_object2])
163  if distance <= self._radius:
164  return True
165 
166  return False
167 
168 
169  def get_som_clusters(self):
170  """!
171  @brief Returns clusters with SOM neurons that encode input features in line with result of synchronization in the second (Sync) layer.
172 
173  @return (list) List of clusters that are represented by lists of indexes of neurons that encode input data.
174 
175  @see process()
176  @see get_clusters()
177 
178  """
179 
180  sync_clusters = self._analyser.allocate_clusters()
181 
182  # Decode it to indexes of SOM neurons
183  som_clusters = list()
184  for oscillators in sync_clusters:
185  cluster = list()
186  for index_oscillator in oscillators:
187  index_neuron = self._som_osc_table[index_oscillator]
188  cluster.append(index_neuron)
189 
190  som_clusters.append(cluster)
191 
192  return som_clusters
193 
194 
195  def get_clusters(self, eps=0.1):
196  """!
197  @brief Returns clusters in line with ensembles of synchronous oscillators where each synchronous ensemble corresponds to only one cluster.
198 
199  @param[in] eps (double): Maximum error for allocation of synchronous ensemble oscillators.
200 
201  @return (list) List of grours (lists) of indexes of synchronous oscillators that corresponds to index of objects.
202 
203  @see process()
204  @see get_som_clusters()
205 
206  """
207 
208  sync_clusters = self._analyser.allocate_clusters(eps) # it isn't indexes of SOM neurons
209 
210  clusters = list()
211  for oscillators in sync_clusters:
212  cluster = list()
213  for index_oscillator in oscillators:
214  index_neuron = self._som_osc_table[index_oscillator]
215 
216  cluster += self._som.capture_objects[index_neuron]
217 
218  clusters.append(cluster)
219 
220  return clusters
221 
222 
224  """!
225  @brief Returns clustering result representation type that indicate how clusters are encoded.
226 
227  @return (type_encoding) Clustering result representation.
228 
229  @see get_clusters()
230 
231  """
232 
233  return type_encoding.CLUSTER_INDEX_LIST_SEPARATION
234 
235 
236  def show_som_layer(self):
237  """!
238  @brief Shows visual representation of the first (SOM) layer.
239 
240  """
241 
242  self._som.show_network()
243 
244 
245  def show_sync_layer(self):
246  """!
247  @brief Shows visual representation of the second (Sync) layer.
248 
249  """
250 
251  self._sync.show_network()
pyclustering.cluster.syncsom.syncsom.som_layer
def som_layer(self)
The first layer of the oscillatory network - self-organized feature map.
Definition: syncsom.py:55
pyclustering.cluster.syncsom.syncsom.__has_object_connection
def __has_object_connection(self, oscillator_index1, oscillator_index2)
Searches for pair of objects that are encoded by specified neurons and that are connected in line wit...
Definition: syncsom.py:147
pyclustering.cluster.syncsom.syncsom._radius
_radius
Definition: syncsom.py:84
pyclustering.nnet.som.som
Represents self-organized feature map (SOM).
Definition: som.py:97
pyclustering.cluster.syncsom.syncsom.show_sync_layer
def show_sync_layer(self)
Shows visual representation of the second (Sync) layer.
Definition: syncsom.py:245
pyclustering.cluster.syncnet
Cluster analysis algorithm: Sync.
Definition: syncnet.py:1
pyclustering.cluster.syncsom.syncsom.show_som_layer
def show_som_layer(self)
Shows visual representation of the first (SOM) layer.
Definition: syncsom.py:236
pyclustering.cluster.syncsom.syncsom.__create_sync_layer
def __create_sync_layer(self, weights)
Creates second layer of the network.
Definition: syncsom.py:128
pyclustering.cluster.syncsom.syncsom._som
_som
Definition: syncsom.py:86
pyclustering.cluster.syncnet.syncnet
Class represents clustering algorithm SyncNet.
Definition: syncnet.py:146
pyclustering.nnet.som
Neural Network: Self-Organized Feature Map.
Definition: som.py:1
pyclustering.cluster.syncsom.syncsom.get_som_clusters
def get_som_clusters(self)
Returns clusters with SOM neurons that encode input features in line with result of synchronization i...
Definition: syncsom.py:169
pyclustering.cluster.syncsom.syncsom.process
def process(self, collect_dynamic=False, order=0.999)
Performs simulation of the oscillatory network.
Definition: syncsom.py:96
pyclustering.cluster.syncsom.syncsom.sync_layer
def sync_layer(self)
The second layer of the oscillatory network based on Kuramoto model.
Definition: syncsom.py:64
pyclustering.cluster.syncsom.syncsom._struct
_struct
Definition: syncsom.py:90
pyclustering.cluster.syncsom.syncsom.get_cluster_encoding
def get_cluster_encoding(self)
Returns clustering result representation type that indicate how clusters are encoded.
Definition: syncsom.py:223
pyclustering.cluster.syncsom.syncsom._som_osc_table
_som_osc_table
Definition: syncsom.py:87
pyclustering.cluster.syncsom.syncsom.get_clusters
def get_clusters(self, eps=0.1)
Returns clusters in line with ensembles of synchronous oscillators where each synchronous ensemble co...
Definition: syncsom.py:195
pyclustering.nnet
Neural and oscillatory network module. Consists of models of bio-inspired networks.
Definition: __init__.py:1
pyclustering.cluster.syncsom.syncsom._analyser
_analyser
Definition: syncsom.py:93
pyclustering.cluster.syncsom.syncsom.__init__
def __init__(self, data, rows, cols, radius)
Constructor of the double layer oscillatory network SYNC-SOM.
Definition: syncsom.py:72
pyclustering.cluster.syncsom.syncsom
Class represents clustering algorithm SYNC-SOM.
Definition: syncsom.py:22
pyclustering.cluster.syncsom.syncsom._sync
_sync
Definition: syncsom.py:89
pyclustering.utils
Utils that are used by modules of pyclustering.
Definition: __init__.py:1
pyclustering.cluster.syncsom.syncsom._data
_data
Definition: syncsom.py:83
pyclustering.cluster.encoder
Module for representing clustering results.
Definition: encoder.py:1