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

📄 corr.c

📁 数据挖掘中的bayes算法,很好的代码
💻 C
📖 第 1 页 / 共 3 页
字号:
  const char *name;             /* to traverse the column names */  double     t;                 /* expected value and variance */  char       *s1, *s2, *s3;     /* separators */  char       *end;              /* end of line */  assert(mvn && out);           /* check the function arguments */  cs_prepare(cset);             /* prepare columns for output */  if (tex) {                    /* if to generate LaTeX code */    fputs("\\begin{tabular}{|r|l|r|r|r|}\\hline\n", out);    fputs("\\multicolumn{5}{|l|}", out);    fputs("{attribute statistics\\rule{0pt}{2.4ex}} \\\\\n", out);    fputs("\\hline\\hline\\rule{0pt}{2.4ex}%\n", out);    fputs("no & attribute", out);  /* print a header */    for (k = 9; k <  cset->maxlen; k++) fputc(' ', out);    fputs(" & exp. val. &  variance & std. dev.", out);    fputs(" \\\\\n\\hline\\rule{0pt}{2.4ex}%\n", out);    s1 = " & "; s2 = " &$"; s3 = "$&$"; end = "$ \\\\\n"; }  else {                        /* if to generate normal text */    fputs("attribute statistics\n", out);    fputs(" no | attribute", out);  /* print a header */    for (k = 9; k < cset->maxlen; k++) fputc(' ', out);    fputs(" | exp. val. |  variance | std. dev.", out);    if (cnt) fputs(" |   cnt", out);    fputs("\n----+-", out);    for (k = 0; k < cset->maxlen; k++) fputc('-', out);    fputs("-+-----------+-----------+----------", out);    if (cnt) fputs("-+------", out);    s1 = s2 = s3 = " | "; end = "\n";    fputc('\n', out);  }  for (i = 0; i < cset->colcnt; i++) {    name = cset->names[i];      /* traverse the matrix columns, */    if (!name) continue;        /* but skip invalidated columns */    fprintf(out, "%3i", ++n);   /* print the column number, */    fputs(s1,   out);           /* a separator, */    fputs(name, out);           /* and the column name */    for (k = (int)strlen(name); k < cset->maxlen; k++)      fputc(' ', out);          /* fill the output field */    fputs(s2, out);             /* retrieve and */    t = mvn_exp(mvn, i);        /* print the expected value */    if (t <= MVN_NULL) fputs("        ?", out);    else               dblout(out, t,       9);    t = mvn_var(mvn, i);        /* retrieve and */    fputs(s3, out);             /* print the variance */    if (t < 0)         fputs("        ?", out);    else               dblout(out, t,       9);    fputs(s3, out);             /* print the standard deviation */    if (t < 0)         fputs("        ?", out);    else               dblout(out, sqrt(t), 9);    if (!tex && cnt)            /* if to print counters */      fprintf(out, " | %5g", mvn_cnt(mvn, i));    fputs(end, out);            /* terminate the output line */  }  if (tex)                      /* if to generate LaTeX output */    fputs("\\hline\n\\end{tabular}\n", out);}  /* expvar() *//*--------------------------------------------------------------------*/static void covar (COLSET *cset, MVNORM *mvn, int tex, FILE *out){                               /* --- print covariance matrix */  int        x, y, n = 0;       /* loop variables */  const char *xname, *yname;    /* to traverse the column names */  double     cov;               /* covariance */  char       *s1, *s2, *s3;     /* separators */  char       *end;              /* end of line */  assert(cset && mvn && out);   /* check the function arguments */  cs_prepare(cset);             /* prepare columns for output */  if (tex) {                    /* if to generate LaTeX output */    fprintf(out, "\\begin{tabular}{|r|l|*{%d}{r|}}", cset->vldcnt);    fputs("\\hline\n", out);    /* print a header */    fprintf(out, "\\multicolumn{%d}{|l|}", cset->vldcnt +2);    fputs("{covariance matrix\\rule{0pt}{2.4ex}} \\\\\n", out);    fputs("\\hline\\hline\\rule{0pt}{2.4ex}%\n", out);    fputs("no & attribute", out);    for (x = 9; x <  cset->maxlen; x++) fputc(' ', out);    for (x = 1; x <= cset->vldcnt; x++) fprintf(out, " & %8i", x);    fputs(" \\\\\n\\hline\\rule{0pt}{2.4ex}%\n", out);    s1 = " & "; s2 = " &"; s3 = "$&$"; end = "$ \\\\\n"; }  else {                        /* if to generate text output */    fputs("covariance matrix\nno | attribute", out);    for (x = 9; x <  cset->maxlen; x++) fputc(' ', out);    fputs(" |", out);           /* print a header */    for (x = 1; x <= cset->vldcnt; x++) fprintf(out, " %8i", x);    fputs("\n---+-", out);    for (x = 0; x <  cset->maxlen; x++) fputc('-', out);    fputs("-+", out);    for (x = 0; x <  cset->vldcnt; x++) fputs("---------", out);    fputc('\n', out);           /* print a separating line */    s1 = " | "; s2 = " |"; s3 = " "; end = "\n";  }  for (y = 0; y < cset->colcnt; y++) {    yname = cset->names[y];     /* traverse the matrix columns, */    if (!yname) continue;       /* but skip invalidated columns */    fprintf(out, "%2i", ++n);   /* print the column number, */    fputs(s1,    out);          /* a separator, */    fputs(yname, out);          /* and the column name */    for (x = (int)strlen(yname); x < cset->maxlen; x++)      fputc(' ', out);          /* fill the output field */    fputs(s2, out);             /* print a separator */    for (x = 1; x < n; x++) {   /* skip lower left part of matrix */      fputs("         ", out); if (tex) fputs(s2, out); }    fputc(s3[0], out);          /* start a number */    for (x = y; x < cset->colcnt; x++) {      xname = cset->names[x];   /* traverse the remaining columns, */      if (!xname) continue;     /* but skip invalidated columns */      if (x > y) fputs(s3,out); /* print a separator */      cov = mvn_cov(mvn, x, y); /* get and print the covariance */      if (cov <= MVN_NULL) fputs("      ?", out);      else                 dblout(out, cov, 8);    }    fputs(end, out);            /* terminate the output line */  }  if (tex)                      /* if to generate LaTeX output */    fputs("\\hline\n\\end{tabular}\n", out);}  /* covar() *//*--------------------------------------------------------------------*/static void correl (COLSET *cset, MVNORM *mvn, int tex, FILE *out){                               /* --- print correlation coefficients */  int        x, y, n = 0;       /* loop variables */  const char *xname, *yname;    /* to traverse the column names */  double     r, t;              /* correlation coefficient, buffer */   char       *s1, *s2, *s3;     /* separators */  char       *end;              /* end of line */  assert(cset && mvn && out);   /* check the function arguments */  cs_prepare(cset);             /* prepare columns for output */  if (tex) {                    /* if to generate LaTeX output */    fprintf(out, "\\begin{tabular}{|r|l|*{%d}{r|}}", cset->vldcnt);    fputs("\\hline\n", out);    /* print a header */    fprintf(out, "\\multicolumn{%d}{|l|}", cset->vldcnt +2);    fputs("{correlation coefficients\\rule{0pt}{2.4ex}} \\\\\n", out);    fputs("\\hline\\hline\\rule{0pt}{2.4ex}%\n", out);    fputs("no & attribute", out);    for (x = 9; x <  cset->maxlen; x++) fputc(' ', out);    for (x = 1; x <= cset->vldcnt; x++) fprintf(out, " & %5i", x);    fputs(" \\\\\n\\hline\\rule{0pt}{2.4ex}%\n", out);    s1 = " & "; s2 = " &"; s3 = "$&$"; end = "$ \\\\\n"; }  else {                        /* if to generate text output */    fputs("correlation coefficients\nno | attribute", out);    for (x = 9; x <  cset->maxlen; x++) fputc(' ', out);    fputs(" |", out);           /* print a header */    for (x = 1; x <= cset->vldcnt; x++) fprintf(out, " %5i", x);    fputs("\n---+-", out);    for (x = 0; x <  cset->maxlen; x++) fputc('-', out);    fputs("-+", out);    for (x = 0; x <  cset->vldcnt; x++) fputs("------", out);    fputc('\n', out);           /* print a separating line */    s1 = " | "; s2 = " |"; s3 = " "; end = "\n";  }  for (y = 0; y < cset->colcnt; y++) {    yname = cset->names[y];     /* traverse the matrix columns, */    if (!yname) continue;       /* but skip invalidated columns */    fprintf(out, "%2i", ++n);   /* print the column number, */    fputs(s1,    out);          /* a separator, */    fputs(yname, out);          /* and the column name */    for (x = (int)strlen(yname); x < cset->maxlen; x++)      fputc(' ', out);          /* fill the output field */    fputs(s2, out);             /* print a separator */    for (x = 1; x < n; x++) {   /* skip lower left part of matrix */      fputs("      ", out); if (tex) fputs(s2, out); }    fputc(s3[0], out);          /* start a number */    fputs(" 1.00", out);        /* the diagonal is always 1 */    for (x = y+1; x < cset->colcnt; x++) {      xname = cset->names[x];   /* traverse the remaining columns, */      if (!xname) continue;     /* but skip invalidated columns */      fputs(s3, out);           /* print a separator */      r = mvn_corr(mvn, x, y);  /* get and check the correl. coeff. */      if (r < MVN_NULL) { fputs("     ?", out); continue; }      t = fabs(r);              /* print the correlation coefficient */      fputs(((r >= 0) || (t < 0.0005)) ? " " : "-", out);      if (t >= 0.9995) fputs("1.00", out);      else fprintf(out, ".%03.0f", t *1000);    }    fputs(end, out);            /* terminate the output line */  }  if (tex)                      /* if to generate LaTeX output */    fputs("\\hline\n\\end{tabular}\n", out);}  /* correl() *//*----------------------------------------------------------------------  Functions----------------------------------------------------------------------*/static void error (int code, ...){                               /* --- print error message */  va_list    args;              /* list of variable arguments */  const char *msg;              /* error message */  if ((code > 0) || (code < E_UNKNOWN))    code = E_UNKNOWN;           /* check error code */  msg = errmsgs[-code];         /* get error message */  if (!msg) msg = errmsgs[-E_UNKNOWN];  fprintf(stderr, "\n%s: ", prgname);  va_start(args, code);         /* get variable arguments */  vfprintf(stderr, msg, args);  /* print error message */  va_end(args);                 /* end argument evaluation */  #ifndef NDEBUG  if (mvnorm) mvn_delete(mvnorm);  if (colset) cs_delete(colset);  if (tscan)  ts_delete(tscan);    /* clean up memory */  if (symtab) st_delete(symtab);   /* and close files */  if (in  && (in  != stdin))  fclose(in);  if (out && (out != stdout)) fclose(out);  #endif  #ifdef STORAGE  showmem("at end of program"); /* check memory usage */  #endif  exit(code);                   /* abort the program */}  /* error() *//*--------------------------------------------------------------------*/int main (int argc, char *argv[]){                               /* --- main function */  int    i, k = 0;              /* loop variables, counters */  char   *s;                    /* to traverse options */  char   **optarg = NULL;       /* option argument */  char   *fn_hdr  = NULL;       /* name of table header file */  char   *fn_tab  = NULL;       /* name of table file */  char   *fn_mat  = NULL;       /* name of matrix file */  char   *fname   = NULL;       /* buffer for file name */  char   *blanks  = NULL;       /* blank  characters */  char   *fldseps = NULL;       /* field  separators */  char   *recseps = NULL;       /* record separators */

⌨️ 快捷键说明

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