39 for (
int d = 0;d <
D;d++) {
double tmp = x[d] - y[d]; acc += tmp * tmp; }
46 for (
int k = 0;k <
K;k++)
c_count[k] = 0;
48 for (
int n = 0;n <
N;n++) {
54 for (
int k = 1;k <
K;k++) {
56 if (tmp_dist < min_dist) { min_dist = tmp_dist; k_winner = k; }
68 for (
int k = 0;k <
K;k++) {
70 for (
int d = 0;d <
D;d++) c_tmpvec[d] = 0.0;
74 for (
int n = 0;n < Nk;n++) {
76 for (
int d = 0;d <
D;d++) c_tmpvec[d] += x[d];
81 for (
int d = 0;d <
D;d++) c_mean[d] = c_tmpvec[d] / Nk;
91 static int counter = 0;
93 bool zombie_mean =
false;
96 int max_count =
count[k];
99 for (
int k = 1;k < K;k++) if (c_count[k] > max_count) { max_count =
c_count[k]; k_hog = k; }
101 for (
int k = 0;k <
K;k++) {
106 it_warning(
"MOG_diag_kmeans_sup::dezombify_means(): detected zombie mean");
109 it_warning(
"MOG_diag_kmeans_sup::dezombify_means(): weirdness: k_hog == k");
113 if (counter >=
c_count[k_hog]) counter = 0;
118 for (
int d = 0;d <
D;d++) c_mean[d] = 0.5 * (
c_means[k_hog][d] + c_x[d]);
133 double tmp_dist = 0.0;
142 for (
int d = 0;d <
D;d++) c_tmpvec[d] = 0.0;
144 for (
int n = 0;n <
N;n++) {
145 double * c_x =
c_X[n];
146 for (
int d = 0;d <
D;d++) c_tmpvec[d] += c_x[d];
149 for (
int d = 0;d <
D;d++) c_tmpvec[d] /= N;
151 int step = int(
floor(
double(N) /
double(
K)));
152 for (
int k = 0;k <
K;k++) {
154 double * c_x =
c_X[k*step];
156 for (
int d = 0;d <
D;d++) c_mean[d] = 0.5 * (c_tmpvec[d] + c_x[d]);
174 if (
verbose) std::cout <<
"MOG_diag_kmeans_sup::iterate(): iteration = " << i <<
" change = " << change << std::endl;
175 if (change == 0)
break;
193 for (
int k = 0;k <
K;k++) {
199 for (
int d = 0;d <
D;d++) c_tmpvec[d] = 0.0;
201 for (
int n = 0;n < Nk;n++) {
203 for (
int d = 0;d <
D;d++) {
double tmp = c_x[d] - c_mean[d]; c_tmpvec[d] += tmp * tmp; }
225 for (
int d = 0;d <
D;d++) {
227 for (
int n = 0;n <
N;n++) acc +=
c_X[n][d];
228 c_norm_mu[d] = acc /
N;
231 for (
int d = 0;d <
D;d++) {
233 for (
int n = 0;n <
N;n++) {
double tmp =
c_X[n][d] - c_norm_mu[d]; acc += tmp * tmp; }
237 for (
int n = 0;n <
N;n++)
for (
int d = 0;d <
D;d++) {
238 c_X[n][d] -= c_norm_mu[d];
239 if (c_norm_sd[d] > 0.0)
c_X[n][d] /= c_norm_sd[d];
247 for (
int n = 0;n <
N;n++)
for (
int d = 0;d <
D;d++) {
248 if (c_norm_sd[d] > 0.0)
c_X[n][d] *= c_norm_sd[d];
249 c_X[n][d] += c_norm_mu[d];
257 for (
int k = 0;k <
K;k++)
for (
int d = 0;d <
D;d++) {
258 if (norm_sd[d] > 0.0)
c_means[k][d] *= c_norm_sd[d];
267 it_assert(model_in.
is_valid(),
"MOG_diag_kmeans_sup::run(): given model is not valid");
268 it_assert((max_iter_in > 0),
"MOG_diag_kmeans_sup::run(): 'max_iter' needs to be greater than zero");
269 it_assert(((trust_in >= 0.0) && (trust_in <= 1.0)),
"MOG_diag_kmeans_sup::run(): 'trust' must be between 0 and 1 (inclusive)");
277 init(means_in, diag_covs_in, weights_in);
281 weights_in.set_size(0);
283 it_assert(
check_size(X_in),
"MOG_diag_kmeans_sup::run(): 'X' is empty or contains vectors of wrong dimensionality");
288 it_warning(
"MOG_diag_kmeans_sup::run(): K > N");
292 it_warning(
"MOG_diag_kmeans_sup::run(): K > N/10");
350 km.
run(model_in, X_in, max_iter_in, trust_in, normalise_in, verbose_in);