K-means algorithm C + + implementation

Source: Internet
Author: User

Tag:k-means    Data mining        c++    based on partitioning;

#include <bits/stdc++.h> #define Dimense 10//10 Dimensional data # define N 5005#define MAX 0xffffff#define CLR (a) memset (a,0,    sizeof (a)) using namespace Std;struct point{double dir[dimense]; int belong;}; int num=5000;//Data Volume int k=10;//k center point center[15]; Point Data[n]; Point Save[15][n];int ct[15];    Point Mid, point A[],int Num, {point mid;    CLR (Mid.dir);        for (int ii=0;ii<num;ii++) for (int iii=0;iii<dimense;iii++) {MID.DIR[III]+=A[II].DIR[III];    } for (int iii=0;iii<dimense;iii++) Mid.dir[iii]/=num; return mid;}    Double Dis (point A,point b) {double ret=0;    for (int ii=0;ii<dimense;ii++) {ret+= (A.dir[ii]-b.dir[ii]) * (A.dir[ii]-b.dir[ii]); } return sqrt (ret);}    int Signate (point a) {double tmp=max;    int POS;    for (int ii=0;ii<k;ii++) {if (DIS (a,center[ii]) <tmp) Pos=ii,tmp=dis (A,center[ii]); } return POS;}    Double Rec[n][15];int sig[15];int vis[n];int main () {int i,j;    CLR (VIS);    CLR (CT); Freopen ("In.txT "," R ", stdin);    Freopen ("Out.txt", "w", stdout);    for (i=0;i<num;i++) for (j=0;j<dimense;j++) scanf ("%lf", &data[i].dir[j]);    Srand (Time (NULL));    int tmp,cnt=0;        while (cnt<k) {Tmp=rand ()%num;        if (vis[tmp]) continue;    else vis[tmp]=1,center[cnt++]=data[tmp];        } for (i=0;i<k;i++) {printf ("%d Center:", i+1); for (j=0;j<dimense;j++) {printf ("%lf%c", center[i].dir[j],j==dimense-1? ')        \ n ': ');        }} for (i=0;i<num;i++) {data[i].belong=signate (data[i]);    Save[data[i].belong][ct[data[i].belong]++]=data[i];    } for (i=0;i<k;i++) printf ("Number of%d clusters exactly how many points%d\n", i+1,ct[i]);    for (i=0;i<k;i++) Center[i]=mid (Save[i],ct[i]);        for (i=0;i<k;i++) {printf ("%d centers:", i+1); for (j=0;j<dimense;j++) {printf ("%lf%c", center[i].dir[j],j==dimense-1? ')        \ n ': ');    }} point last[15]; for (i=0;i<k;i++) for (j=0;j<dimense;j++) LAST[I].DIR[J]=CENTER[I].DIR[J];    int bre=1;    int nnn=0;        while (BRE) {nnn++;        bre=0;        CLR (save);        CLR (CT);            for (i=0;i<num;i++) {data[i].belong=signate (data[i]);        Save[data[i].belong][ct[data[i].belong]++]=data[i];        } for (i=0;i<k;i++) printf ("Number of%d clusters exactly how many points%d\n", i+1,ct[i]);        for (i=0;i<k;i++) Center[i]=mid (Save[i],ct[i]);                    for (i=0;i<k;i++) {for (j=0;j<dimense;j++) if (Last[i].dir[j]!=center[i].dir[j]) {                    bre=1;                Break            };        if (bre==1) break;        } for (i=0;i<k;i++) for (j=0;j<dimense;j++) last[i].dir[j]=center[i].dir[j];            for (i=0;i<k;i++) {printf ("%d centers:", i+1); for (j=0;j<dimense;j++) {printf ("%lf%c", center[i].dir[j],j==dimense-1? ')            \ n ': '); }}} for (i=0;i<num;i++) {printF ("%d points of%d clusters \ n", i+1,data[i].belong+1);    } printf ("Number%d\n", nnn); return 0;}

Copy to Google TranslateTranslation Results

K-means algorithm C + + implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.