19 using namespace Eigen;
54 cluster_assignments.
zero();
60 weights_set[0]=lhs_size;
70 SG_SWARNING(
"KMeans clustering has reached maximum number of ( %d ) iterations without having converged. \ 71 Terminating. \n", iter)
79 #pragma omp parallel for firstprivate(lhs_size, dim, num_centers) \ 80 shared(centers, cluster_assignments, weights_set) \ 81 reduction(+:changed) if (!fixed_centers) 83 for (int32_t i=0; i<lhs_size; i++)
85 const int32_t cluster_assignments_i=cluster_assignments[i];
86 int32_t min_cluster, j;
91 for (j=1; j<num_centers; j++)
101 if (min_cluster!=cluster_assignments_i)
105 ++weights_set[min_cluster];
107 --weights_set[cluster_assignments_i];
112 float64_t temp_min = 1.0 / weights_set[min_cluster];
115 for (j=0; j<dim; j++)
117 centers(j, min_cluster)+=
118 (vec[j]-centers(j, min_cluster))*temp_min;
125 if (weights_set[cluster_assignments_i]!=0)
127 float64_t temp_i = 1.0 / weights_set[cluster_assignments_i];
130 for (j=0; j<dim; j++)
132 centers(j, cluster_assignments_i)-=
133 (vec1[j]-centers(j, cluster_assignments_i))*temp_i;
140 for (j=0; j<dim; j++)
141 centers(j, cluster_assignments_i)=0;
146 cluster_assignments[i] = min_cluster;
159 for (int32_t i=0; i<lhs_size; i++)
161 int32_t cluster_i=cluster_assignments[i];
166 map_centers.col(cluster_i) += map_vec;
171 for (int32_t i=0; i<num_centers; i++)
173 if (weights_set[i]!=0)
174 map_centers.col(i)*=1.0/weights_set[i];
177 if (iter%(max_iter/10) == 0)
178 SG_SINFO(
"Iteration[%d/%d]: Assignment of %i patterns changed.\n", iter, max_iter, changed)
186 bool CKMeans::train_machine(
CFeatures* data)
189 Lloyd_KMeans(
mus,
k);
void set_feature_matrix(SGMatrix< ST > matrix)
Class Distance, a base class for all the distances used in the Shogun toolbox.
virtual void reset_precompute()
SGMatrix< float64_t > mus
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
void compute_cluster_variances()
void initialize_training(CFeatures *data=NULL)
SGMatrix< T > clone() const
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
virtual CFeatures * replace_rhs(CFeatures *rhs)
int32_t get_num_features() const
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
virtual int32_t get_num_vectors() const
all of classes and functions are contained in the shogun namespace
static CDenseFeatures * obtain_from_generic(CFeatures *const base_features)
The class Features is the base class of all feature objects.
virtual void precompute_lhs()
virtual void precompute_rhs()