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

📄 bci.c

📁 数据挖掘中的bayes算法,很好的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------  File    : bci.c  Contents: naive and full Bayes classifier induction  Author  : Christian Borgelt  History : 1998.12.08 file created from file dti.c            1999.02.13 input from stdin, output to stdout added            1999.03.25 weight distribution (option -t) added            1999.04.17 simplified using the new module 'io'            1999.05.15 check of class count moved behind table reading            2000.11.21 adapted to redesigned module nbayes            2000.11.30 full Bayes classifier induction added            2001.02.11 bug in attribute output (full Bayes) fixed            2001.07.16 adapted to modified module scan            2003.08.16 slight changes in error message output            2007.02.13 adapted to modified module attset            2007.10.10 evaluation of attribute directions added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <time.h>#include <assert.h>#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef TAB_RDWR#define TAB_RDWR#endif#include "io.h"#ifndef NBC_INDUCE#define NBC_INDUCE#endif#include "nbayes.h"#ifndef FBC_INDUCE#define FBC_INDUCE#endif#include "fbayes.h"#ifdef STORAGE#include "storage.h"#endif/*----------------------------------------------------------------------  Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME     "bci"#define DESCRIPTION "naive and full Bayes classifier induction"#define VERSION     "version 2.9 (2007.10.10)         " \                    "(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)        /* file open failed */#define E_FREAD     (-3)        /* file read failed */#define E_FWRITE    (-4)        /* file write failed */#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_BALANCE  (-10)        /* unknown balancing mode */#define E_SIMP     (-11)        /* unknown simplification mode */#define E_CLASS    (-12)        /* missing class attribute */#define E_MULTCLS  (-13)        /* multiple class attributes */#define E_CTYPE    (-14)        /* class attribute is not nominal */#define E_UNKNOWN  (-15)        /* unknown error */#define SEC_SINCE(t)  ((clock()-(t)) /(double)CLOCKS_PER_SEC)/*----------------------------------------------------------------------  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_BALANCE -10 */  "unknown balancing mode %c\n",  /* E_SIMP    -11 */  "unknown simplification mode %c\n",  /* E_CLASS   -12 */  "missing class attribute \"%s\" in file %s\n",  /* E_MULTCLS -13 */  "multiple class attributes\n",  /* E_CTYPE   -14 */  "class attribute \"%s\" is not nominal\n",  /* E_UNKNOWN -15 */  "unknown error\n"};/*----------------------------------------------------------------------  Global Variables----------------------------------------------------------------------*/const  char   *prgname = NULL;  /* program name for error messages */static SCAN   *scan    = NULL;  /* scanner */static ATTSET *attset  = NULL;  /* attribute set */static TABLE  *table   = NULL;  /* table */static NBC    *nbc     = NULL;  /* naive Bayes classifier */static FBC    *fbc     = NULL;  /* full  Bayes classifier */static FILE   *in      = NULL;  /* input  file */static FILE   *out     = NULL;  /* output file *//*----------------------------------------------------------------------  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 the 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 the 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);  if (table)  tab_delete(table, 0);  /* 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 the program */}  /* error() *//*--------------------------------------------------------------------*/int main (int argc, char *argv[]){                               /* --- main function */  int     i, k = 0;             /* loop variables, counter, 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_dom  = NULL;      /* name of domain file */  char    *fn_bc   = NULL;      /* name of classifier 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 */  char    *clsname = NULL;      /* name of the class attribute */  int     full     = 0;         /* flag for a full Bayes classifier */  int     flags    = AS_NOXATT; /* table file read flags */  int     balance  = 0;         /* flag for balancing class freqs. */  int     simp     = 0;         /* flag for classifier simplification */  double  lcorr    = 0;         /* Laplace correction value */  int     maxlen   = 0;         /* maximal output line length */  int     setup    = 0;         /* setup/induction mode */  int     desc     = 0;         /* description mode */  int     attcnt   = 0;         /* number of attributes */  int     tplcnt   = 0;         /* number of tuples */  double  tplwgt   = 0.0;       /* weight of tuples */  int     clsid;                /* id of class column */  ATT     *att;                 /* to traverse attributes */  TSINFO  *err;                 /* error information */  clock_t t;                    /* timer for measurements */  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 given */    printf("usage: %s [options] domfile "                     "[-d|-h hdrfile] tabfile bcfile\n", argv[0]);    printf("%s\n", DESCRIPTION);    printf("%s\n", VERSION);    printf("-F       induce a full Bayes classifier "                    "(default: naive Bayes)\n");    printf("-c#      class field name (default: last field)\n");    printf("-w#      balance class frequencies (weight tuples)\n");    printf("         l: lower, b: boost, s: shift weights\n");    printf("-s#      simplify classifier (naive Bayes only)\n"           "         a: by adding, r: by removing attributes\n");    printf("-L#      Laplace correction (default: %g)\n", lcorr);    printf("-t       distribute tuple weight for null values\n");    printf("-m       use maximum likelihood estimate "                    "for the variance\n");    printf("-p       print relative frequencies (in percent)\n");    printf("-l#      output line length (default: no limit)\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("-n       number of tuple occurrences in last field\n");    printf("domfile  file containing domain descriptions\n");    printf("-d       use default table 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");

⌨️ 快捷键说明

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