somsc.py
1 """!
2 
3 @brief Cluster analysis algorithm: SOM-SC (Self-Organized Feature Map for Simple Clustering)
4 @details There is no paper on which implementation is based. Algorithm SOM-SC is adaptation of SOM for cluster analysis in simple way.
5  Basic idea: amount of cluster that should be allocated is defines amount of neurons in the self-organized map. SOM-SC can be
6  considered as neural network implementation of K-Means algorithm.
7  Implementation based on paper @cite article::nnet::som::1.
8 
9 @authors Andrei Novikov (pyclustering@yandex.ru)
10 @date 2014-2019
11 @copyright GNU Public License
12 
13 @cond GNU_PUBLIC_LICENSE
14  PyClustering is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  PyClustering is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 @endcond
27 
28 """
29 
30 
31 from pyclustering.cluster.encoder import type_encoding
32 
33 from pyclustering.nnet.som import som
34 from pyclustering.nnet.som import type_conn
35 
36 
37 class somsc:
38  """!
39  @brief Class represents simple clustering algorithm based on self-organized feature map.
40  @details This algorithm uses amount of clusters that should be allocated as a size of SOM map. Captured objects by neurons are clusters.
41  Algorithm is able to process data with Gaussian distribution that has spherical forms.
42 
43  Example:
44  @code
45  from pyclustering.cluster import cluster_visualizer
46  from pyclustering.cluster.somsc import somsc
47  from pyclustering.samples.definitions import FCPS_SAMPLES
48  from pyclustering.utils import read_sample
49 
50  # Load list of points for cluster analysis
51  sample = read_sample(FCPS_SAMPLES.SAMPLE_TWO_DIAMONDS)
52 
53  # Create instance of SOM-SC algorithm to allocated two clusters
54  somsc_instance = somsc(sample, 2)
55 
56  # Run cluster analysis and obtain results
57  somsc_instance.process()
58  clusters = somsc_instance.get_clusters()
59 
60  # Visualize clustering results.
61  visualizer = cluster_visualizer()
62  visualizer.append_clusters(clusters, sample)
63  visualizer.show()
64  @endcode
65 
66  """
67 
68  def __init__(self, data, amount_clusters, epouch=100, ccore=True):
69  """!
70  @brief Creates SOM-SC (Self Organized Map for Simple Clustering) algorithm for clustering analysis.
71 
72  @param[in] data (list): List of points that are used for processing.
73  @param[in] amount_clusters (uint): Amount of clusters that should be allocated.
74  @param[in] epouch (uint): Number of epochs for training of SOM.
75  @param[in] ccore (bool): If it is True then CCORE implementation will be used for clustering analysis.
76 
77  """
78 
79  self.__data_pointer = data
80  self.__amount_clusters = amount_clusters
81  self.__epouch = epouch
82  self.__ccore = ccore
83 
84  self.__network = None
85 
86  self.__verify_arguments()
87 
88 
89  def process(self):
90  """!
91  @brief Performs cluster analysis by competition between neurons of SOM.
92 
93  @return (somsc) Returns itself (SOM Simple Clustering instance).
94 
95  @see get_clusters()
96 
97  """
98 
99  self.__network = som(1, self.__amount_clusters, type_conn.grid_four, None, self.__ccore)
100  self.__network.train(self.__data_pointer, self.__epouch, True)
101  return self
102 
103 
104  def get_clusters(self):
105  """!
106  @brief Returns list of allocated clusters, each cluster contains indexes of objects in list of data.
107 
108  @see process()
109 
110  """
111 
112  return self.__network.capture_objects
113 
114 
116  """!
117  @brief Returns clustering result representation type that indicate how clusters are encoded.
118 
119  @return (type_encoding) Clustering result representation.
120 
121  @see get_clusters()
122 
123  """
124 
125  return type_encoding.CLUSTER_INDEX_LIST_SEPARATION
126 
127 
128  def __verify_arguments(self):
129  """!
130  @brief Verify input parameters for the algorithm and throw exception in case of incorrectness.
131 
132  """
133  if len(self.__data_pointer) == 0:
134  raise ValueError("Input data is empty (size: '%d')." % len(self.__data_pointer))
135 
136  if self.__amount_clusters <= 0:
137  raise ValueError("Amount of clusters (current value: '%d') should be greater than 0." %
138  self.__amount_clusters)
139 
140  if self.__epouch < 0:
141  raise ValueError("Amount of epouch (current value: '%d') should be greater or equal to 0." %
142  self.__epouch)
def __init__(self, data, amount_clusters, epouch=100, ccore=True)
Creates SOM-SC (Self Organized Map for Simple Clustering) algorithm for clustering analysis...
Definition: somsc.py:68
def get_cluster_encoding(self)
Returns clustering result representation type that indicate how clusters are encoded.
Definition: somsc.py:115
Module for representing clustering results.
Definition: encoder.py:1
def __verify_arguments(self)
Verify input parameters for the algorithm and throw exception in case of incorrectness.
Definition: somsc.py:128
def get_clusters(self)
Returns list of allocated clusters, each cluster contains indexes of objects in list of data...
Definition: somsc.py:104
Class represents simple clustering algorithm based on self-organized feature map. ...
Definition: somsc.py:37
def process(self)
Performs cluster analysis by competition between neurons of SOM.
Definition: somsc.py:89
Neural Network: Self-Organized Feature Map.
Definition: som.py:1
Represents self-organized feature map (SOM).
Definition: som.py:115