File: silhouette_ksearch.hpp

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (249 lines) | stat: -rwxr-xr-x 8,084 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*!

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

*/

#pragma once


#include <pyclustering/cluster/silhouette.hpp>
#include <pyclustering/cluster/silhouette_ksearch_data.hpp>


namespace pyclustering {

namespace clst {


/*!

@class    silhouette_ksearch_allocator silhouette_ksearch.hpp pyclustering/cluster/silhouette_ksearch.hpp

@brief    Interface of silhouette's K-search allocator that is used by the algorithm to define strategy that defines how to
           perform cluster analysis.


@see    kmeans_allocator
@see    kmedians_allocator
@see    kmedoids_allocator

*/
class silhouette_ksearch_allocator {
public:
    /*!

    @brief Defines shared pointer to the interface 'silhouette_ksearch_allocator'.

    */
    using ptr = std::shared_ptr<silhouette_ksearch_allocator>;

public:
    /*!
    
    @brief Default destructor of the algorithm.
    
    */
    virtual ~silhouette_ksearch_allocator() = default;

public:
    /*!

    @brief Performs cluster analysis in order to allocate specified amount of cluster from an input data.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, cluster_sequence & p_clusters) = 0;

    /*!

    @brief Performs cluster analysis in order to allocate specified amount of cluster from an input data.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[in]  p_random_state: seed for random state (value `RANDOM_STATE_CURRENT_TIME` means the current system time is going to used as a seed).
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, const long long p_random_state, cluster_sequence & p_clusters) = 0;
};


/*!

@class    kmeans_allocator silhouette_ksearch.hpp pyclustering/cluster/silhouette_ksearch.hpp

@brief    Silhouette K-search allocator based on K-Means algorithm.

*/
class kmeans_allocator : public silhouette_ksearch_allocator {
public:
    /*!

    @brief Performs cluster analysis using K-Means algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, cluster_sequence & p_clusters) override;

    /*!

    @brief Performs cluster analysis using K-Means algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[in]  p_random_state: seed for random state (value `RANDOM_STATE_CURRENT_TIME` means the current system time is going to used as a seed).
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, const long long p_random_state, cluster_sequence & p_clusters) override;
};


/*!

@class    kmedians_allocator silhouette_ksearch.hpp pyclustering/cluster/silhouette_ksearch.hpp

@brief    Silhouette K-search allocator based on K-Medians algorithm.

*/
class kmedians_allocator : public silhouette_ksearch_allocator {
public:
    /*!

    @brief Performs cluster analysis using K-Medians algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, cluster_sequence & p_clusters) override;

    /*!

    @brief Performs cluster analysis using K-Medians algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[in]  p_random_state: seed for random state (value `RANDOM_STATE_CURRENT_TIME` means the current system time is going to used as a seed).
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, const long long p_random_state, cluster_sequence & p_clusters) override;
};


/*!

@class    kmedoids_allocator silhouette_ksearch.hpp pyclustering/cluster/silhouette_ksearch.hpp

@brief    Silhouette K-search allocator based on K-Medoids algorithm.

*/
class kmedoids_allocator : public silhouette_ksearch_allocator {
public:
    /*!

    @brief Performs cluster analysis using K-Medoids algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, cluster_sequence & p_clusters) override;

    /*!

    @brief Performs cluster analysis using K-Medoids algorithm.

    @param[in]  p_amount: amount of clusters that should be allocated.
    @param[in]  p_data: input data for cluster analysis.
    @param[in]  p_random_state: seed for random state (value `RANDOM_STATE_CURRENT_TIME` means the current system time is going to used as a seed).
    @param[out] p_clusters: container where result (allocated clusters) is placed.

    */
    virtual void allocate(const std::size_t p_amount, const dataset & p_data, const long long p_random_state, cluster_sequence & p_clusters) override;
};



/*!

@class    silhouette_ksearch silhouette_ksearch.hpp pyclustering/cluster/silhouette_ksearch.hpp

@brief    Defines algorithms that is used to find optimal number of cluster using Silhouette method.

*/
class silhouette_ksearch {
private:
    std::size_t m_kmin;
    std::size_t m_kmax;
    silhouette_ksearch_allocator::ptr m_allocator = std::make_shared<kmeans_allocator>();
    long long m_random_state;

public:
    /*!

    @brief    Default constructor of Silhouette search algorithm.

    */
    silhouette_ksearch() = default;

    /*!

    @brief    Constructor of Silhouette search algorithm.

    @param[in] p_kmin: minimum amount of clusters that might be allocated.
    @param[in] p_kmax: maximum amount of clusters that might be allocated.
    @param[in] p_allocator: strategy that is used to allocate clusters or in other words, to perform cluster analysis.
    @param[in] p_random_state: seed for random state (by default is `RANDOM_STATE_CURRENT_TIME`, current system time is used).

    */
    silhouette_ksearch(const std::size_t p_kmin, const std::size_t p_kmax, const silhouette_ksearch_allocator::ptr & p_allocator = std::make_shared<kmeans_allocator>(), const long long p_random_state = RANDOM_STATE_CURRENT_TIME);

    /*!

    @brief    Default copy constructor of Silhouette search algorithm.

    */
    silhouette_ksearch(const silhouette_ksearch & p_other) = default;

    /*!

    @brief    Default move constructor of Silhouette search algorithm.

    */
    silhouette_ksearch(silhouette_ksearch && p_other) = default;

    /*!

    @brief    Default destructor of Silhouette search algorithm.

    */
    ~silhouette_ksearch() = default;

public:
    /*!

    @brief    Performs analysis to find optimal amount of clusters.

    @param[in] p_data: input data that is used for searching optimal amount of clusters.
    @param[in] p_result: analysis result of an input data.

    */
    void process(const dataset & p_data, silhouette_ksearch_data & p_result);
};

}

}