/usr/include/shogun/clustering/KMeans.h is in libshogun-dev 3.2.0-7.5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2008 Gunnar Raetsch
* Written (W) 2007-2009 Soeren Sonnenburg
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _KMEANS_H__
#define _KMEANS_H__
#include <stdio.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/distance/Distance.h>
#include <shogun/machine/DistanceMachine.h>
namespace shogun
{
class CDistanceMachine;
/** training method */
enum EKMeansMethod
{
KMM_MINI_BATCH,
KMM_LLOYD
};
/** @brief KMeans clustering, partitions the data into k (a-priori specified) clusters.
*
* It minimizes
* \f[
* \sum_{i=1}^k\sum_{x_j\in S_i} (x_j-\mu_i)^2
* \f]
*
* where \f$\mu_i\f$ are the cluster centers and \f$S_i,\;i=1,\dots,k\f$ are the index
* sets of the clusters.
*
* Beware that this algorithm obtains only a <em>local</em> optimum.
*
* cf. http://en.wikipedia.org/wiki/K-means_algorithm
*
*/
class CKMeans : public CDistanceMachine
{
public:
/** default constructor */
CKMeans();
/** constructor
*
* @param k parameter k
* @param d distance
* @param f train_method value
*/
CKMeans(int32_t k, CDistance* d, EKMeansMethod f);
/** constructor
*
* @param k parameter k
* @param d distance
* @param kmeanspp true for using KMeans++ (default false)
* @param f train_method value
*/
CKMeans(int32_t k, CDistance* d, bool kmeanspp=false, EKMeansMethod f=KMM_LLOYD);
/** constructor for supplying initial centers
* @param k_i parameter k
* @param d_i distance
* @param centers_i initial centers for KMeans algorithm
* @param f train_method value
*/
CKMeans(int32_t k_i, CDistance* d_i, SGMatrix<float64_t> centers_i, EKMeansMethod f=KMM_LLOYD);
virtual ~CKMeans();
MACHINE_PROBLEM_TYPE(PT_MULTICLASS)
/** get classifier type
*
* @return classifier type KMEANS
*/
virtual EMachineType get_classifier_type() { return CT_KMEANS; }
/** load distance machine from file
*
* @param srcfile file to load from
* @return if loading was successful
*/
virtual bool load(FILE* srcfile);
/** save distance machine to file
*
* @param dstfile file to save to
* @return if saving was successful
*/
virtual bool save(FILE* dstfile);
/** set k
*
* @param p_k new k
*/
void set_k(int32_t p_k);
/** get k
*
* @return the parameter k
*/
int32_t get_k();
/** set use_kmeanspp attribute
*
* @param kmpp true=>use KMeans++ false=>don't use KMeans++
*/
void set_use_kmeanspp(bool kmpp);
/** get use_kmeanspp attribute
*
* @return use_kmeanspp true=>use KMeans++ false=>don't use KMeans++
*/
bool get_use_kmeanspp() const;
/** set fixed centers
*
* @param fixed true if fixed cluster centers are intended
*/
void set_fixed_centers(bool fixed);
/** get fixed centers
*
* @return whether boolean centers are to be used
*/
bool get_fixed_centers();
/** set maximum number of iterations
*
* @param iter the new maximum
*/
void set_max_iter(int32_t iter);
/** get maximum number of iterations
*
* @return maximum number of iterations
*/
float64_t get_max_iter();
/** get radiuses
*
* @return radiuses
*/
SGVector<float64_t> get_radiuses();
/** get centers
*
* @return cluster centers or empty matrix if no radiuses are there (not trained yet)
*/
SGMatrix<float64_t> get_cluster_centers();
/** get dimensions
*
* @return number of dimensions
*/
int32_t get_dimensions();
/** @return object name */
virtual const char* get_name() const { return "KMeans"; }
/** set the initial cluster centers
*
* @param centers matrix with cluster centers (k colums, dim rows)
*/
virtual void set_initial_centers(SGMatrix<float64_t> centers);
/** set training method
*
*@param f minibatch if mini-batch KMeans
*/
void set_train_method(EKMeansMethod f);
/** get training method
*
*@return training method used - minibatch or lloyd
*/
EKMeansMethod get_train_method() const;
/** set batch size for mini-batch KMeans
*
*@param b batch size int32_t(greater than 0)
*/
void set_mbKMeans_batch_size(int32_t b);
/** get batch size for mini-batch KMeans
*
*@return batch size
*/
int32_t get_mbKMeans_batch_size() const;
/** set no. of iterations for mini-batch KMeans
*
*@param t no. of iterations int32_t(greater than 0)
*/
void set_mbKMeans_iter(int32_t t);
/** get no. of iterations for mini-batch KMeans
*
*@return no. of iterations
*/
int32_t get_mbKMeans_iter() const;
/** set batch size and no. of iteration for mini-batch KMeans
*
*@param b batch size
*@param t no. of iterations
*/
void set_mbKMeans_params(int32_t b, int32_t t);
private:
/** train k-means
*
* @param data training data (parameter can be avoided if distance or
* kernel-based classifiers are used and distance/kernels are
* initialized with train data)
*
* @return whether training was successful
*/
virtual bool train_machine(CFeatures* data=NULL);
/** Ensures cluster centers are in lhs of underlying distance */
virtual void store_model_features();
virtual bool train_require_labels() const { return false; }
/** kmeans++ algorithm to initialize cluster centers
*
* @return initial cluster centers: matrix (k columns, dim rows)
*/
SGMatrix<float64_t> kmeanspp();
void init();
/** algorithm to initialize random cluster centers
*
* @return initial cluster centers: matrix (k columns, dim rows)
*/
void set_random_centers(SGVector<float64_t> weights_set, SGVector<int32_t> ClList, int32_t XSize);
void set_initial_centers(SGVector<float64_t> weights_set,
SGVector<int32_t> ClList, int32_t XSize);
void compute_cluster_variances();
private:
/// maximum number of iterations
int32_t max_iter;
/// whether to keep cluster centers fixed or not
bool fixed_centers;
/// the k parameter in KMeans
int32_t k;
/// number of dimensions
int32_t dimensions;
/// radi of the clusters (size k)
SGVector<float64_t> R;
///initial centers supplied
SGMatrix<float64_t> mus_initial;
///flag to check if kmeans++ has to be used
bool use_kmeanspp;
///batch size for mini-batch KMeans
int32_t batch_size;
///number of iterations for mini-batch KMeans
int32_t minib_iter;
/// temp variable for cluster centers
SGMatrix<float64_t> mus;
/// set minibatch to use mini-batch KMeans
EKMeansMethod train_method;
};
}
#endif
|