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

📄 bcx.c

📁 数据挖掘中的bayes算法,很好的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------  File    : bcx.c  Contents: naive and full Bayes classifier execution  Author  : Christian Borgelt  History : 1998.12.08 file created from file dtc.c            1999.02.13 input from stdin, output to stdout added            1999.04.17 simplified using the new module 'io'            2000.11.20 option -m (max. likelihood est. for var.) added            2000.11.21 adapted to redesigned module nbayes            2000.11.30 full Bayes classifier execution added            2001.07.16 adapted to modified module scan            2002.09.18 bug concerning missing target fixed            2003.02.02 bug in alignment in connection with -d fixed            2003.04.23 missing AS_MARKED added for second reading            2003.08.16 slight changes in error message output            2005.02.22 classification threshold added (option -t)            2006.01.17 format specification for confidence added            2007.02.13 adapted to modified module attset----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <assert.h>#ifndef SC_SCAN#define SC_SCAN#endif#include "scan.h"#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#include "io.h"#ifndef NBC_PARSE#define NBC_PARSE#endif#include "nbayes.h"#ifndef FBC_PARSE#define FBC_PARSE#endif#include "fbayes.h"#ifdef STORAGE#include "storage.h"#endif/*----------------------------------------------------------------------  Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME     "bcx"#define DESCRIPTION "naive and full Bayes classifier execution"#define VERSION     "version 2.16 (2007.10.19)        " \                    "(c) 1998-2007   Christian Borgelt"/* --- error codes --- */#define OK            0         /* no error */#define E_NONE        0         /* no error */#define E_NOMEM     (-1)        /* not enough memory */#define E_FOPEN     (-2)        /* cannot open file */#define E_FREAD     (-3)        /* read error on file */#define E_FWRITE    (-4)        /* write error on file */#define E_OPTION    (-5)        /* unknown option */#define E_OPTARG    (-6)        /* missing option argument */#define E_ARGCNT    (-7)        /* wrong number of arguments */#define E_STDIN     (-8)        /* double assignment of stdin */#define E_PARSE     (-9)        /* parse error */#define E_CLASS    (-10)        /* missing class */#define E_NEGLC    (-11)        /* negative Laplace correction */#define E_UNKNOWN  (-12)        /* unknown error *//*----------------------------------------------------------------------  Type Definitions----------------------------------------------------------------------*/typedef struct {                /* --- classification result info.--- */  ATT    *att;                  /* class attribute */  char   *n_class;              /* name  of classification column */   int    w_class;               /* width of classification column */  int    class;                 /* class (classification result) */  char   *n_prob;               /* name  of probability column */  int    w_prob;                /* width of probability column */  double prob;                  /* probability of result */  char   *format;               /* number output format */  int    all;                   /* whether to show all probabilities */} RESULT;                       /* (classification result info.) *//*----------------------------------------------------------------------  Constants----------------------------------------------------------------------*/static const char *errmsgs[] = {   /* error messages */  /* E_NONE      0 */  "no error\n",  /* E_NOMEM    -1 */  "not enough memory\n",  /* E_FOPEN    -2 */  "cannot open file %s\n",  /* E_FREAD    -3 */  "read error on file %s\n",  /* E_FWRITE   -4 */  "write error on file %s\n",  /* E_OPTION   -5 */  "unknown option -%c\n",  /* E_OPTARG   -6 */  "missing option argument\n",  /* E_ARGCNT   -7 */  "wrong number of arguments\n",  /* E_STDIN    -8 */  "double assignment of standard input\n",  /* E_PARSE    -9 */  "parse error(s) on file %s\n",  /* E_CLASS   -10 */  "missing class \"%s\" in file %s\n",  /* E_NEGLC   -11 */  "Laplace correction must not be negative\n",  /* E_UNKNOWN -12 */  "unknown error\n"};/*----------------------------------------------------------------------  Global Variables----------------------------------------------------------------------*/const  char   *prgname = NULL;  /* program name for error messages */static SCAN   *scan    = NULL;  /* scanner */static NBC    *nbc     = NULL;  /* naive Bayes classifier */static FBC    *fbc     = NULL;  /* full  Bayes classifier */static ATTSET *attset  = NULL;  /* attribute set */static FILE   *in      = NULL;  /* input  file */static FILE   *out     = NULL;  /* output file */static RESULT res = {           /* classification result information */  NULL,                         /* class attribute */  "bc", 0, 0,                   /* data for classification column */  NULL, 0, 0.0, "%.3f", 0 };    /* data for probability    column *//*----------------------------------------------------------------------  Functions----------------------------------------------------------------------*/static void error (int code, ...){                               /* --- print error message */  va_list    args;              /* list of variable arguments */  const char *msg;              /* error message */  assert(prgname);              /* check the program name */  if (code < E_UNKNOWN) code = E_UNKNOWN;  if (code < 0) {               /* if to report an error, */    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 (nbc)    nbc_delete(nbc, 0);  if (fbc)    fbc_delete(fbc, 0);  if (attset) as_delete(attset);   /* clean up memory */  if (scan)   sc_delete(scan);     /* 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 programm */}  /* error() *//*--------------------------------------------------------------------*/static void infout (ATTSET *set, FILE *file, int mode, CCHAR *seps){                               /* --- write additional information */  int    i, k;                  /* loop variables, buffers */  CCHAR  *class;                /* class attribute/value name */  char   *prob;                 /* name of probability column */  double p;                     /* buffer for probability */  char   buf[32];               /* buffer for output */  if (mode & AS_ATT) {          /* if to write header */    class = res.n_class;        /* get name of classification column */    prob  = res.n_prob;         /* and name of probability column */    if (mode & AS_ALIGN) {      /* if to align fields */      if ((mode & AS_WEIGHT) || prob) {        i = att_valwd(res.att, 0);        k = (int)strlen(class); res.w_class = (i > k) ? i : k;      }                         /* compute width of class column */      if (prob && (mode & AS_WEIGHT)) {        k = (int)strlen(prob);  res.w_prob  = (4 > k) ? 4 : k; }    } }                         /* compute width of prob. column */  else {                        /* if to write a normal record */    class = att_valname(res.att, res.class);    if (res.n_prob) sprintf(buf, res.format, res.prob);    prob = buf;                 /* format the probability */  }                             /* get and format field contents */  k = fprintf(file, class);     /* write classification result */  for (i = res.w_class -k; --i >= 0; )    fputc(seps[0], file);       /* if to align, pad with blanks */  if (res.n_prob) {             /* if to write class probability */    fputc(seps[1], file);       /* write field separator */    fputs(prob, file);          /* and number of errors */    for (i = res.w_prob -k; --i >= 0; ) fputc(seps[0], file);  }                             /* if to align, pad with blanks */  if (!res.all) return;         /* if not to show probs., abort */  k = att_valcnt(res.att);      /* get the number of values */  if (mode & AS_ATT) {          /* if to write the header */    for (i = 0; i < k; i++) {   /* traverse the target values */      fputc(seps[1], file);     /* print a separator */      fputs(att_valname(res.att, i), file);    } }                         /* print the value name */  else {                        /* if to write the activations */    for (i = 0; i < k; i++) {   /* traverse the values */      fputc(seps[1], file);     /* print a separator */      p = (fbc) ? fbc_post(fbc, i) : nbc_post(nbc, i);      fprintf(file, res.format, p);    }                           /* print the probability */  }                             /* (extended confidence information) */}  /* infout() *//*--------------------------------------------------------------------*/int main (int argc, char* argv[]){                               /* --- main function */  int    i, k = 0, f;           /* loop variables, buffer */  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_bc   = NULL;       /* name of classifier file */  char   *fn_out  = NULL;       /* name of output file */  char   *blanks  = NULL;       /* blanks */  char   *fldseps = NULL;       /* field  separators */  char   *recseps = NULL;       /* record separators */  char   *nullchs = NULL;       /* null value characters */  char   *comment = NULL;       /* comment characters */  double lcorr    = -DBL_MAX;   /* Laplace correction value */  double thresh   = 0.5;        /* classification threshold */  int    dwnull   = 0;          /* distribute weight of null values */  int    maxllh   = 0;          /* max. likelihood est. of variance */  int    inflags  = 0;          /* table file read  flags */  int    outflags = AS_ATT;     /* table file write flags */  int    tplcnt   = 0;          /* number of tuples */  double tplwgt   = 0;          /* weight of tuples */  double errcnt   = 0;          /* number of misclassifications */  int    clscnt;                /* number of classes */

⌨️ 快捷键说明

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