/*********************************************************/ /* */ /* k-means.c */ /* ========= */ /* */ /* C programme to demonstrate k-means */ /* clustering on 2D data */ /* */ /* Written for EE3J2 Data Mining */ /* */ /* Version 1: Martin Russell 26/02/04 */ /* */ /* Dept. Electronic, Electrical & Computer Engineering */ /* University of Birmingham */ /* */ /* To compile under linux: */ /* gcc -lm k-means.c */ /* mv a.out k-means */ /* */ /* To run: */ /* k-means ipFile centroids opFile numIt */ /* */ /*********************************************************/ #define MAX_STR_LEN 512 #define MAX_LIN_LEN 30 #define BIGNUM 1.0e12 #define PRTINC 10 #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *ipFile; FILE *centFile; FILE *opFile; int i; int j; int k; float **x; float **cent; float *mean; float **dist; int *bestCent; char **label; char **cLabel; int it; int numRows; int numCols; int numCols2; int numCent; float *distortion; int NUMIT; /* Check correct number of input parameters */ if ((argc!=5)&&(argc!=4)) { printf("format: k-means ipFile centroids opFile \n"); exit(1); } if (argc==4) NUMIT=10; /* allocate space for distortion scores */ distortion=(float *)calloc(NUMIT, sizeof(float)); printf("open input file\n"); /* set pointer to input data file */ argv++; if ((ipFile=fopen(*argv,"r"))==NULL) { printf("Error: can't open input file %s\n",*argv); exit(1); } printf("open input file\n"); /* set pointer to centroid data file */ argv++; if ((centFile=fopen(*argv,"r"))==NULL) { printf("Error: can't open input file %s\n",*argv); exit(1); } printf("open output file\n"); /* set pointer to outnput data file */ argv++; if ((opFile=fopen(*argv,"w"))==NULL) { printf("Error: can't open output file %s\n",*argv); exit(1); } /* set pointer to number of iterations */ argv++; NUMIT=atoi(*argv); printf("Number of iterations = %d\n",NUMIT); /* read number of data points */ if(fscanf(ipFile,"# num rows=%d num columns=%d\n",&numRows,&numCols)!=2) { printf("Format error in first line\n"); exit(1); } printf("numRows=%d numCols=%d\n",numRows,numCols); /* allocate memory */ x=(float **)calloc(numRows,sizeof(float *)); dist=(float **)calloc(numRows,sizeof(float *)); label=(char **)calloc(numRows,sizeof(char *)); mean=(float *)calloc(numCols,sizeof(float)); for (j=0; j 0) { int e; for (e=0; e