📄 kdd2.c
字号:
/* Scalable K-means clustering softwareCopyright (C) 2000 Fredrik Farnstrom and James LewisThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.See the file README.TXT for more information.*//* kdd2.c */#include <math.h>#include <stdio.h>#include <string.h>static int Dimensions = 56;static int Counter = 95413; //114;#define REAL double// Vector to output converted data.static REAL *Sum;static REAL *SumSqr;static REAL *Count;// File pointers for input and output data files.FILE *DataInFp;FILE *DataOutFp;FILE *DataMSDFp;// Allocate vectors used to preocess data records.int Setup(void){ int i; Sum = 0; SumSqr = 0; Count = 0; // Allocate Data and SumData arrays. if(!(Sum = (REAL*) malloc(sizeof(REAL) * Dimensions))) return 1; if(!(SumSqr = (REAL*) malloc(sizeof(REAL) * Dimensions))) { free(Sum); Sum = 0; return 1; } if(!(Count = (REAL*) malloc(sizeof(REAL) * Dimensions))) { free(Sum); Sum = 0; free(SumSqr); SumSqr = 0; return 1; } for (i = 0; i < Dimensions; i++) { Sum[i] = 0; SumSqr[i] = 0; Count[i] = 0; } return 0;}// Process a record.int Process(void){ int i, j; REAL tmp; REAL t; while (--Counter) { for (i = 0; i < Dimensions; i++) { if ((fscanf(DataInFp, "%lf", &tmp)) != 1) { printf("Error reading data file"); exit(1); } if (tmp == -1) { tmp = 0; } else { Count[i]++; Sum[i] += tmp; t = SumSqr[i]; SumSqr[i] += tmp*tmp; if(SumSqr[i] < t) fprintf(stderr, "Error: %d %d\n", Counter, i); } if ((fprintf(DataOutFp, "%lf ", tmp)) < 0) { printf("Error writing data."); exit(1); } } if ((fprintf(DataOutFp, "\n")) < 0) { printf("Error writing data."); exit(1); } }}// Free memory buffers.void FreeVectors(void){ if (Sum) { free(Sum); Sum = 0; } if (SumSqr) { free(SumSqr); SumSqr = 0; } if (Count) { free(Count); Count = 0; }}// Main routine.int main(int argc, char *argv[]){ int i; REAL tmp; Setup(); // Open input data file. if ((DataInFp = fopen("cup98.dat", "r")) == NULL) { perror("Unable to open data file."); exit(1); } // Open temp data file. if ((DataOutFp = fopen("cup98data.txt", "w")) == NULL) { perror("Unable to open data file."); exit(1); } Process(); fclose(DataInFp); fclose(DataOutFp); // Open temp data file. if ((DataMSDFp = fopen("cup98msd.txt", "w")) == NULL) { perror("Unable to open data file."); exit(1); } for (i = 0; i < Dimensions; i++) { tmp = Sum[i] / Count[i]; fprintf(DataMSDFp, "%f ", tmp); printf("%d %f\n", i, Count[i]); } fprintf(DataMSDFp, "\n"); for (i = 0; i < Dimensions; i++) { tmp = Sum[i] / Count[i]; printf("%f %f ", SumSqr[i]/Count[i], tmp*tmp); tmp = (SumSqr[i] / Count[i]) - (tmp * tmp); fprintf(DataMSDFp, "%f ", sqrt(tmp)); printf("%f\n", sqrt(tmp)); } fprintf(DataMSDFp, "\n"); fclose(DataMSDFp); FreeVectors();}/* End of file kdd2.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -