⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 corr.c

📁 数据挖掘中的bayes算法,很好的代码
💻 C
📖 第 1 页 / 共 3 页
字号:
  char   *nullchs = NULL;       /* null value characters */  char   *comment = NULL;       /* comment characters */  int    header   = 0;          /* header type (table/default/file) */  int    fldcnt   = 0;          /* number of fields/columns */  int    wgtflg   = 0;          /* flag for weight in last field */  int    flags    = 0;          /* flags for matrices to print */  int    pos      = 0;          /* flag for positive values */  int    tex      = 0;          /* flag for LaTeX output */  size_t maxlen   = 6, len;     /* (maximal) length of a value name */  int    tplcnt   = 0;          /* number of tuples */  double tplwgt   = 0.0;        /* weight of tuples */  double weight   = 1.0;        /* weight of tuple */  int    d;                     /* delimiter type */  int    *p;                    /* pointer to symbol data */  prgname = argv[0];            /* get program name for error msgs. */  /* --- print startup/usage message --- */  if (argc > 1) {               /* if arguments are given */    fprintf(stderr, "%s - %s\n", argv[0], DESCRIPTION);    fprintf(stderr, VERSION); } /* print a startup message */  else {                        /* if no argument is given */    printf("usage: %s [options] "                     "[-d|-h hdrfile] tabfile [matfile]\n", argv[0]);    printf("%s\n", DESCRIPTION);    printf("%s\n", VERSION);    printf("-x       print expected values and standard deviation\n");    printf("-v       print covariance matrix\n");    printf("-c       print correlation coefficients (default)\n");    printf("-m       use maximum likelihood estimates "                    "for the (co)variances\n");    printf("-p       use only positive values\n");    printf("-t       print output in LaTeX format\n");    printf("-n       number of tuple occurrences in last field\n");    printf("-b/f/r#  blank characters, field and record separators\n"           "         (default: \" \\t\\r\", \" ,\\t\", \"\\n\")\n");    printf("-u#      null value characters (default: \"?*\")\n");    printf("-C#      comment characters    (default: \"#\")\n");    printf("-d       use default header "                    "(field names = field numbers)\n");    printf("-h       read table header (field names) from hdrfile\n");    printf("hdrfile  file containing table header (field names)\n");    printf("tabfile  table file to read "                    "(field names in first record)\n");    printf("matfile  file to write matrix to (optional)\n");    return 0;                   /* print a usage message */  }                             /* and abort the program */  /* --- evaluate arguments --- */  for (i = 1; i < argc; i++) {  /* traverse arguments */    s = argv[i];                /* get option argument */    if (optarg) { *optarg = s; optarg = NULL; continue; }    if ((*s == '-') && *++s) {  /* -- if argument is an option */      while (1) {               /* traverse characters */        switch (*s++) {         /* evaluate option */          case 'x': flags |= MVN_EXPVAR;   break;          case 'v': flags |= MVN_COVAR;    break;          case 'c': flags |= MVN_CORREL;   break;          case 'm': flags |= MVN_MAXLLH;   break;          case 'p': pos    = 1;            break;          case 't': tex    = 1;            break;          case 'n': wgtflg = 1;            break;  	  case 'b': optarg = &blanks;      break;          case 'f': optarg = &fldseps;     break;          case 'r': optarg = &recseps;     break;          case 'u': optarg = &nullchs;     break;          case 'C': optarg = &comment;     break;          case 'd': header = 1;            break;          case 'h': optarg = &fn_hdr;      break;          default : error(E_OPTION, *--s); break;        }                       /* set option variables */        if (!*s) break;         /* if at end of string, abort loop */        if (optarg) { *optarg = s; optarg = NULL; break; }      } }                       /* get option argument */    else {                      /* -- if argument is no option */      switch (k++) {            /* evaluate non-option */        case  0: fn_tab = s;      break;        case  1: fn_mat = s;      break;        default: error(E_ARGCNT); break;      }                         /* note filenames */    }  }  if (optarg) error(E_OPTARG);  /* check option argument */  if ((k < 1) || (k > 2)) error(E_ARGCNT);  if (fn_hdr) {                 /* set the header file flag */    header = 2; if (strcmp(fn_hdr, "-") == 0) fn_hdr = ""; }  if (!flags) flags  = MVN_CORREL;  /* --- create symbol table and table scanner --- */  symtab = st_create(0, 0, (HASHFN*)0, (SYMFN*)0);  if (!symtab) error(E_NOMEM);  /* create symbol table */  tscan = ts_create();          /* and table scanner */  if (!tscan) error(E_NOMEM);   /* and set delimiter characters */  ts_chars(tscan, TS_BLANK,   blanks);  ts_chars(tscan, TS_FLDSEP,  fldseps);  ts_chars(tscan, TS_RECSEP,  recseps);  ts_chars(tscan, TS_NULL,    nullchs);  ts_chars(tscan, TS_COMMENT, comment);  colset = cs_create();         /* create a column set */  if (!colset) error(E_NOMEM);  /* for the table reading */  /* --- read table header/first record --- */  fname = (fn_hdr) ? fn_hdr : fn_tab;  if (!fname || !*fname) {      /* if no proper file name is given, */    in = stdin; fname = "<stdin>"; }    /* read from standard input */  else {                        /* if a proper file name is given */    in = fopen(fname, "r");     /* open file for reading */    if (!in) error(E_FOPEN, fname);  }  fprintf(stderr, "\nreading %s ... ", fname);  do {                          /* read fields of table header */    d = ts_next(tscan, in, rdbuf, BUFSIZE-1);    if (d == TS_ERR) error(E_FREAD, fname);    fldcnt++;                   /* read the next field name */    if ((d == TS_REC) && wgtflg)      break;                    /* skip a tuple weight field */    if (header != 1) {          /* if to use a normal header */      if (!rdbuf[0])   error(E_EMPFLD, fname, 1, rdbuf, fldcnt);      p = (int*)st_insert(symtab, rdbuf, -1, sizeof(int));      if (!p) error(E_NOMEM);   /* insert name into symbol table */      if (p == EXISTS) error(E_DUPFLD, fname, 1, rdbuf, fldcnt);      *p = fldcnt-1;            /* note the field number */      if (cs_add(colset, st_name(p)) != 0)        error(E_NOMEM); }       /* add a column to the column set */    else {                      /* if to use a default header, */      sprintf(fnbuf, "%d", fldcnt);     /* create a field name */      p = (int*)st_insert(symtab, fnbuf, -1, sizeof(int));      if (!p) error(E_NOMEM);   /* insert name into symbol table */      *p = fldcnt-1;            /* note the field number */      if (cs_add(colset, st_name(p)) != 0)        error(E_NOMEM);         /* add a column to the column set */      cs_set(colset, fldcnt-1, (rdbuf[0] == '\0') ? NULL : rdbuf);    }                           /* set the column value */    len = strlen(st_name(p));   /* determine the maximal length */    if (len > maxlen) maxlen = len;       /* of the field names */  } while (d == TS_FLD);        /* while not at end of record */  if (fn_hdr) {                 /* close the table header file */    fclose(in); fprintf(stderr, "done."); }  /* --- compute covariance matrix --- */  mvnorm = mvn_create(colset->colcnt);  if (!mvnorm) error(E_NOMEM);  /* create a normal distribution */  if      (header > 1) {        /* if a table header file is given */    if (fn_tab && *fn_tab)      /* if a proper table name is given, */      in = fopen(fn_tab, "r");  /* open table file for reading */    else {                      /* if no table file name is given, */      in = stdin; fn_tab = "<stdin>"; }    /* read from std. input */    fprintf(stderr, "reading %s ... ", fn_tab);    if (!in) error(E_FOPEN, fn_tab); }  else if (header > 0) {        /* if to use a default header */    if (wgtflg) {               /* if a tuple weight is given, */      weight = strtod(rdbuf,&s);       /* get the tuple weight */      if (!*s || (s == rdbuf) || (weight < 0))        error(E_VALUE, fname, 1, rdbuf, fldcnt);    }    if (pos) cs_procx(colset, mvnorm, weight);    else     cs_proc (colset, mvnorm, weight);    tplwgt += weight;           /* aggregate for the first tuple, */    tplcnt++;                   /* sum the tuple weight and */  }                             /* increment the tuple counter */  while (1) {                   /* read table records */    d = TS_FLD;                 /* read fields in table record */    for (i = 0; (i < fldcnt) && (d == TS_FLD); i++) {      d = ts_next(tscan, in, rdbuf, BUFSIZE-1);      if (d == TS_ERR) error(E_FREAD, fn_tab);      if (d == TS_EOF) {        /* if at end of file, */        i = -1; break; }        /* abort the read loop */      if (i < fldcnt -wgtflg)   /* if this is a normal field */        cs_set(colset, i, (rdbuf[0] == '\0') ? NULL : rdbuf);      else {                    /* if this is the weight field */        weight = strtod(rdbuf, &s);        if (!*s || (s == rdbuf) || (weight < 0))          error(E_VALUE, fn_tab, tplcnt +((header > 0) ? 1 : 2), rdbuf);      }                         /* get the tuple weight/counter */    }                           /* and check for a valid number */    if (i < 0) break;           /* if at end of file, abort loop */    if (i != fldcnt)            /* check number of fields in record */      error(E_FLDCNT, fn_tab, tplcnt +((header > 0) ? 1 : 2), fldcnt);    if (pos) cs_procx(colset, mvnorm, weight);    else     cs_proc (colset, mvnorm, weight);    tplwgt += weight;           /* aggregate for the current tuple */    tplcnt++;                   /* and sum the weight/count the tuple */  }  if (in != stdin) fclose(in);  /* close the input file and */  in = NULL;                    /* clear the file variable */  fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt);  /* --- print matrix/matrices --- */  k = flags;                    /* build the evaluation flags */  if (k & MVN_CORREL) k |= MVN_COVAR;  if (k & MVN_COVAR)  k |= MVN_EXPVAR;  mvn_calc(mvnorm, k);          /* calculate parameters from data */  if (fn_mat)                   /* if a matrix file name is given, */    out = fopen(fn_mat, "w");   /* open correlation matrix file */  else {                        /* if no matrix file name is given, */    out = stdout; fn_mat = "<stdout>"; }         /* write to stdout */  fprintf(stderr, "writing %s ... ", fn_mat);  if (!out) error(E_FOPEN, fn_mat);  if (flags & MVN_EXPVAR) {     /* print the expected values */    expvar(colset, mvnorm, tex, pos, out);    if (flags & (MVN_COVAR|MVN_CORREL)) fputc('\n', out);  }                             /* leave one line empty */  if (flags & MVN_COVAR) {      /* print the covariance matrix */    covar(colset, mvnorm, tex, out);    if (flags & MVN_CORREL) fputc('\n', out);  }                             /* leave one line empty */  if (flags & MVN_CORREL)       /* print the correlation coefficients */    correl(colset, mvnorm, tex, out);  if (out != stdout) {          /* if not written to standard output */    i = fclose(out); out = NULL;    if (i) error(E_FWRITE, fn_mat);  }                             /* close correlation matrix file */  fprintf(stderr, "done.\n");   /* and print a success message */  /* --- clean up --- */  #ifndef NDEBUG                /* if debug version */  mvn_delete(mvnorm);           /* delete normal distribution, */  cs_delete(colset);            /* column set, */  ts_delete(tscan);             /* table scanner, */  st_delete(symtab);            /* and symbol table */  #endif  #ifdef STORAGE  showmem("at end of program"); /* check memory usage */  #endif  return 0;                     /* return 'ok' */}  /* main() */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -