C++ C++;:K-均值聚类G-矩阵
我是新来的,很抱歉这个“问题”对你们有些人来说太愚蠢了。 我必须在大学里做一个关于K-均值聚类的C++项目,我需要一些帮助。 这是代码。它正在工作。 现在,我要分别建立G矩阵。在代码中,我得到以下信息:C++ C++;:K-均值聚类G-矩阵,c++,cluster-analysis,k-means,C++,Cluster Analysis,K Means,我是新来的,很抱歉这个“问题”对你们有些人来说太愚蠢了。 我必须在大学里做一个关于K-均值聚类的C++项目,我需要一些帮助。 这是代码。它正在工作。 现在,我要分别建立G矩阵。在代码中,我得到以下信息: 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 质心的坐标为: 4.50 3.50 1.50 1.00 这很好
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
质心的坐标为:
4.50
3.50
1.50
1.00
这很好,但我需要一个附加的G矩阵中的1,1,0,0
,如下所示:
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 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]);
}
}
}
}
float-dmin,dpoint;
浮点数[2][2];
int集群[4],计数[4],组;
浮动翻转;
const int rows=4;
const int columns=2;
常数int crows=2;
常数int ccolumns=2;
//初始化点
int点[行][列]={{1,1},{2,1},{4,3},{5,4};
//初始化质心
双质心[crows][ccolumns]={{1,1},{2,1};
// ...
对于(i=0;i0){
翻转=0;
对于(j=0;j<2;j++)
{
计数[j]=0;
对于(i=0;i<2;i++)
总和[j][i]=0;
}
//现在,我们需要计算距离
对于(i=0;i<4;i++){
dmin=2;组=cluster[i];
对于(j=0;j<2;j++)
{
dpoint=0.0;
dpoint+=sqrt(pow((点[i][0]-质心[j][0]),2)+pow((点[i][1]-质心[j][1]),2));
fprintf(标准输出,“%5.2f”,dpoint);//显示距离的值
if(dpoint
谢谢你的帮助 好吧,把你的第三列翻译成G矩阵
这其实很琐碎。该列给出了要设置为1的行号。在此上下文中,K是什么?我的意思是你想分成多少个簇?在这个练习中,我有4个点,我必须分配给两个质心。我还有一个练习有100个点和10个质心,所以这个方法应该能够扩展。。。G矩阵显示每个点的对应组。因此,A B C D 1 1 0 0->c1 0 0 1 1->c2意味着A点和B点属于第一组,C点和D点属于第二组。