2013-04-02 2 views
-1

Я новичок здесь, и мне очень жаль, что эта «проблема» для некоторых из вас слишком глупа. Мне нужно сделать проект 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 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]); 
     } 
    } 


} 

} 

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

+0

Что такое K в этом контексте? Я имею в виду, сколько кластеров вы хотели бы разбить? – taocp

+1

Домашнее задание? – Roalt

+0

Что такое G-матрица? –

ответ

0

Ну, переведите эту третью колонку в G-матрицу.

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

Смежные вопросы