elbow.py
1 """!
2 
3 @brief Elbow method to determine the optimal number of clusters for k-means clustering.
4 @details Implementation based on paper @cite article::cluster::elbow::1.
5 
6 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2019
8 @copyright GNU Public License
9 
10 @cond GNU_PUBLIC_LICENSE
11  PyClustering is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  PyClustering is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23 @endcond
24 
25 """
26 
27 
28 import math
29 
30 from pyclustering.cluster.kmeans import kmeans
31 from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer, random_center_initializer
32 from pyclustering.core.wrapper import ccore_library
33 
34 import pyclustering.core.elbow_wrapper as wrapper
35 
36 
37 class elbow:
38  """!
39  @brief Class represents Elbow method that is used to find out appropriate amount of clusters in a dataset.
40  @details Elbow method performs clustering using K-Means algorithm for each K and estimate clustering results using
41  sum of square erros. By default K-Means++ algorithm is used to calculate initial centers that are used by
42  K-Means algorithm.
43 
44  The Elbow is determined by max distance from each point (x, y) to segment from kmin-point (x0, y0) to kmax-point (x1, y1),
45  where 'x' is K (amount of clusters), and 'y' is within-cluster error. Following expression is used to calculate Elbow
46  length:
47  \f[Elbow_{k} = \frac{\left ( y_{0} - y_{1} \right )x_{k} + \left ( x_{1} - x_{0} \right )y_{k} + \left ( x_{0}y_{1} - x_{1}y_{0} \right )}{\sqrt{\left ( x_{1} - x_{0} \right )^{2} + \left ( y_{1} - y_{0} \right )^{2}}}\f]
48 
49  Usage example of Elbow method for cluster analysis:
50  @code
51  from pyclustering.cluster.kmeans import kmeans, kmeans_visualizer
52  from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
53  from pyclustering.cluster.elbow import elbow
54  from pyclustering.utils import read_sample
55  from pyclustering.samples.definitions import SIMPLE_SAMPLES
56 
57  # read sample 'Simple3' from file (sample contains four clusters)
58  sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE3)
59 
60  # create instance of Elbow method using K value from 1 to 10.
61  kmin, kmax = 1, 10
62  elbow_instance = elbow(sample, kmin, kmax)
63 
64  # process input data and obtain results of analysis
65  elbow_instance.process()
66  amount_clusters = elbow_instance.get_amount() # most probable amount of clusters
67  wce = elbow_instance.get_wce() # total within-cluster errors for each K
68 
69  # perform cluster analysis using K-Means algorithm
70  centers = kmeans_plusplus_initializer(sample, amount_clusters,
71  amount_candidates=kmeans_plusplus_initializer.FARTHEST_CENTER_CANDIDATE).initialize()
72  kmeans_instance = kmeans(sample, centers)
73  kmeans_instance.process()
74 
75  # obtain clustering results and visualize them
76  clusters = kmeans_instance.get_clusters()
77  centers = kmeans_instance.get_centers()
78  kmeans_visualizer.show_clusters(sample, clusters, centers)
79  @endcode
80 
81  By default Elbow uses K-Means++ initializer to calculate initial centers for K-Means algorithm, it can be changed
82  using argument 'initializer':
83  @code
84  # perform analysis using Elbow method with random center initializer for K-Means algorithm inside of the method.
85  kmin, kmax = 1, 10
86  elbow_instance = elbow(sample, kmin, kmax, initializer=random_center_initializer)
87  elbow_instance.process()
88  @endcode
89 
90  @image html elbow_example_simple_03.png "Elbows analysis with further K-Means clustering."
91 
92  """
93 
94  def __init__(self, data, kmin, kmax, **kwargs):
95  """!
96  @brief Construct Elbow method.
97 
98  @param[in] data (array_like): Input data that is presented as array of points (objects), each point should be represented by array_like data structure.
99  @param[in] kmin (int): Minimum amount of clusters that should be considered.
100  @param[in] kmax (int): Maximum amount of clusters that should be considered.
101  @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'ccore', 'initializer').
102 
103  <b>Keyword Args:</b><br>
104  - ccore (bool): If True then CCORE (C++ implementation of pyclustering library) is used (by default True).
105  - initializer (callable): Center initializer that is used by K-Means algorithm (by default K-Means++).
106 
107  """
108 
109  self.__initializer = kwargs.get('initializer', kmeans_plusplus_initializer)
110 
111  self.__ccore = kwargs.get('ccore', True) or \
112  isinstance(self.__initializer, kmeans_plusplus_initializer) or \
113  isinstance(self.__initializer, random_center_initializer)
114 
115  if self.__ccore:
116  self.__ccore = ccore_library.workable()
117 
118  self.__data = data
119  self.__kmin = kmin
120  self.__kmax = kmax
121 
122  self.__wce = []
123  self.__elbows = []
124  self.__kvalue = -1
125 
126  self.__verify_arguments()
127 
128 
129  def process(self):
130  """!
131  @brief Performs analysis to find out appropriate amount of clusters.
132 
133  @return (elbow) Returns itself (Elbow instance).
134 
135  @return
136 
137  """
138  if self.__ccore:
139  self.__process_by_ccore()
140  else:
141  self.__process_by_python()
142 
143  return self
144 
145 
146  def __process_by_ccore(self):
147  """!
148  @brief Performs processing using C++ implementation.
149 
150  """
151  if isinstance(self.__initializer, kmeans_plusplus_initializer):
152  initializer = wrapper.elbow_center_initializer.KMEANS_PLUS_PLUS
153  else:
154  initializer = wrapper.elbow_center_initializer.RANDOM
155 
156  result = wrapper.elbow(self.__data, self.__kmin, self.__kmax, initializer)
157 
158  self.__kvalue = result[0]
159  self.__wce = result[1]
160 
161 
162  def __process_by_python(self):
163  """!
164  @brief Performs processing using python implementation.
165 
166  """
167  for amount in range(self.__kmin, self.__kmax):
168  centers = self.__initializer(self.__data, amount).initialize()
169  instance = kmeans(self.__data, centers, ccore=True)
170  instance.process()
171 
172  self.__wce.append(instance.get_total_wce())
173 
174  self.__calculate_elbows()
175  self.__find_optimal_kvalue()
176 
177 
178  def get_amount(self):
179  """!
180  @brief Returns appropriate amount of clusters.
181 
182  """
183  return self.__kvalue
184 
185 
186  def get_wce(self):
187  """!
188  @brief Returns list of total within cluster errors for each K-value (kmin, kmin + 1, ..., kmax - 1).
189 
190  """
191 
192  return self.__wce
193 
194 
195  def __calculate_elbows(self):
196  """!
197  @brief Calculates potential elbows.
198  @details Elbow is calculated as a distance from each point (x, y) to segment from kmin-point (x0, y0) to kmax-point (x1, y1).
199 
200  """
201 
202  x0, y0 = 0.0, self.__wce[0]
203  x1, y1 = float(len(self.__wce)), self.__wce[-1]
204 
205  for index_elbow in range(1, len(self.__wce) - 1):
206  x, y = float(index_elbow), self.__wce[index_elbow]
207 
208  segment = abs((y0 - y1) * x + (x1 - x0) * y + (x0 * y1 - x1 * y0))
209  norm = math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2)
210  distance = segment / norm
211 
212  self.__elbows.append(distance)
213 
214 
215  def __find_optimal_kvalue(self):
216  """!
217  @brief Finds elbow and returns corresponding K-value.
218 
219  """
220  optimal_elbow_value = max(self.__elbows)
221  self.__kvalue = self.__elbows.index(optimal_elbow_value) + 1 + self.__kmin
222 
223 
224  def __verify_arguments(self):
225  """!
226  @brief Verify input parameters for the algorithm and throw exception in case of incorrectness.
227 
228  """
229  if len(self.__data) == 0:
230  raise ValueError("Input data is empty (size: '%d')." % len(self.__data))
231 
232  if self.__kmin < 1:
233  raise ValueError("K min value (current value '%d') should be greater or equal to 1." % self.__kmin)
234 
235  if self.__kmax - self.__kmin < 3:
236  raise ValueError("Amount of K (" + str(self.__kmax - self.__kmin) + ") is too small for analysis. "
237  "It is require to have at least three K to build elbow.")
238 
239  if len(self.__data) < self.__kmax:
240  raise ValueError("K max value '%d' is greater than amount of points in data '%d'." %
241  (self.__kmax, len(self.__data)))
def __calculate_elbows(self)
Calculates potential elbows.
Definition: elbow.py:195
The module contains K-Means algorithm and other related services.
Definition: kmeans.py:1
def __process_by_python(self)
Performs processing using python implementation.
Definition: elbow.py:162
def __find_optimal_kvalue(self)
Finds elbow and returns corresponding K-value.
Definition: elbow.py:215
Class implements K-Means clustering algorithm.
Definition: kmeans.py:272
def __init__(self, data, kmin, kmax, kwargs)
Construct Elbow method.
Definition: elbow.py:94
def __process_by_ccore(self)
Performs processing using C++ implementation.
Definition: elbow.py:146
def process(self)
Performs analysis to find out appropriate amount of clusters.
Definition: elbow.py:129
def __verify_arguments(self)
Verify input parameters for the algorithm and throw exception in case of incorrectness.
Definition: elbow.py:224
Collection of center initializers for algorithm that uses initial centers, for example, for K-Means or X-Means.
Class represents Elbow method that is used to find out appropriate amount of clusters in a dataset...
Definition: elbow.py:37
def get_amount(self)
Returns appropriate amount of clusters.
Definition: elbow.py:178
def get_wce(self)
Returns list of total within cluster errors for each K-value (kmin, kmin + 1, ..., kmax - 1).
Definition: elbow.py:186