-1

我是新来的,我很抱歉这个“问题”对你们中的一些人来说有点太愚蠢了。我必须在大学做一个关于 k-mean 聚类的 C++ 项目,我需要一些帮助。这是代码。这是工作。现在,我必须单独构建 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点在哪里?c1c2质心。知道如何显示这个吗?

这是我的代码:

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 points


int 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]);
        }
    }


}

}

谢谢你的帮助!

4

1 回答 1

0

好吧,将您的第三列转换为 G 矩阵。

这实际上是微不足道的。该列为您提供要设置为 1 的行号。

于 2013-04-03T12:24:55.147 回答