кластерный анализ — C ++: K-средняя кластеризация G-Matrix

Я новичок здесь, и мне жаль, что эта «проблема» слишком глупа для некоторых из вас.
Мне нужно сделать проект C ++ по кластеризации k-средних в университете, и мне нужна помощь.
Вот код Это работает.
Теперь я должен построить G-Matrix отдельно. В коде я получаю следующее:

 4.30  0.50   * 1 *
3.54  0.50   * 1 *
0.71  3.20   * 0 *
0.71  4.61   * 0 *

Координаты центроида:

 4.50
3.50

Координаты центроида:

1.50
1.00

Это хорошо, но мне нужно 1,1,0,0 в дополнительной G-матрице, как это:

A B C D
1 1 0 0 ->c1
0 0 1 1 ->c2

где A,B,C,D точки и c1 а также c2 являются центроидами.
Есть идеи, как это отобразить?

Вот мой код:

float dmin, dpoint;
float sum[2][2];
int cluster[4], count[4], group;
float flips;
const int rows = 4;
const int columns = 2;
const int crows = 2;
const int ccolumns = 2;

// initialize the pointsint point[rows][columns]={{1,1},{2,1},{4,3},{5,4}};// initialize the centroids

double centroid [crows][ccolumns] = {{1,1},{2,1}};// ...

for (i = 0; i<4; i++) cluster[i] = 0;

// until there is no change of clusters belonging to each pattern, continue

flips = 4;
while (flips>0) {

flips = 0;

for (j = 0; j < 2; j++)
{
count[j] = 0;
for (i = 0; i < 2; i++)
sum[j][i] = 0;
}// now, we need to calculate the distance

for (i = 0; i < 4; i++) {

dmin = 2; group = cluster[i];
for (j = 0; j < 2; j++)
{

dpoint = 0.0;

dpoint +=  sqrt(pow((point[i][0] - centroid[j][0]),2)+pow((point[i][1] - centroid[j][1]),2));
fprintf(stdout, "%5.2f ", dpoint); // Show the value of the distance
if (dpoint < dmin) {
group = j;
dmin = dpoint;
}
}

// now, we need to calculate the G matrix (1 or 0)

fprintf(stdout, "  * %d *\n", group); // displays 0 or 1 (to which cluster it belongs)

if (cluster[i] != group)
{
flips++;
cluster[i] = group; // repeat this process until G(n)=G(n+1)
}

count[cluster[i]]++;

for (j = 0; j < 2; j++)
sum[cluster[i]][j] += point[i][j];
}

// now, display the coordinates of the centroid

for (i = 0; i < 2; i++) {
fprintf(stderr," The coordinates of the centroid are: \n");
for (j = 0; j < 2; j++) {
centroid[i][j] = sum[i][j]/count[i];
fprintf(stderr, "%5.2f \n", centroid[i][j]);
}
}}

}

Спасибо за вашу помощь!

-1

Решение

Ну, переведите этот третий столбец в G-матрицу.

Это на самом деле тривиально. В этом столбце указывается номер строки, равный 1.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector