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)
74 rhs_mus->copy_feature_matrix(centers);
78 #pragma omp parallel for firstprivate(lhs_size, dim, num_centers) \
79 shared(centers, cluster_assignments, weights_set) \
80 reduction(+:changed) if (!fixed_centers)
82 for (int32_t i=0; i<lhs_size; i++)
84 const int32_t cluster_assignments_i=cluster_assignments[i];
85 int32_t min_cluster, j;
90 for (j=1; j<num_centers; j++)
100 if (min_cluster!=cluster_assignments_i)
104 ++weights_set[min_cluster];
106 --weights_set[cluster_assignments_i];
111 float64_t temp_min = 1.0 / weights_set[min_cluster];
114 for (j=0; j<dim; j++)
116 centers(j, min_cluster)+=
117 (vec[j]-centers(j, min_cluster))*temp_min;
124 if (weights_set[cluster_assignments_i]!=0)
126 float64_t temp_i = 1.0 / weights_set[cluster_assignments_i];
129 for (j=0; j<dim; j++)
131 centers(j, cluster_assignments_i)-=
132 (vec1[j]-centers(j, cluster_assignments_i))*temp_i;
139 for (j=0; j<dim; j++)
140 centers(j, cluster_assignments_i)=0;
145 cluster_assignments[i] = min_cluster;
156 Map<MatrixXd> map_centers(centers.matrix, centers.num_rows, centers.num_cols);
158 for (int32_t i=0; i<lhs_size; i++)
160 int32_t cluster_i=cluster_assignments[i];
165 map_centers.col(cluster_i) += map_vec;
170 for (int32_t i=0; i<num_centers; i++)
172 if (weights_set[i]!=0)
173 map_centers.col(i)*=1.0/weights_set[i];
176 if (iter%(max_iter/10) == 0)
177 SG_SINFO(
"Iteration[%d/%d]: Assignment of %i patterns changed.\n", iter, max_iter, changed)
185 bool CKMeans::train_machine(
CFeatures* data)
188 Lloyd_KMeans(
mus,
k);
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
Class Distance, a base class for all the distances used in the Shogun toolbox.
int32_t get_num_features() const
virtual void reset_precompute()
SGMatrix< float64_t > mus
void compute_cluster_variances()
void initialize_training(CFeatures *data=NULL)
virtual int32_t get_num_vectors() const
virtual CFeatures * replace_rhs(CFeatures *rhs)
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
all of classes and functions are contained in the shogun namespace
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
The class Features is the base class of all feature objects.
virtual void precompute_lhs()
static CDenseFeatures * obtain_from_generic(CFeatures *const base_features)