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

📄 bci.c

📁 程序用C语言实现了贝叶斯在数据挖掘中分类和预测中的应用
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------  File    : bci.c  Contents: naive and full Bayes classifier induction  Author  : Christian Borgelt  History : 08.12.1998 file created from file dti.c            13.02.1999 input from stdin, output to stdout added            25.03.1999 weight distribution (option -w) added            17.04.1999 simplified using the new module 'io'            15.05.1999 check of class count moved behind table reading            21.11.2000 adapted to redesigned module nbayes            30.11.2000 full Bayes classifier induction added            11.02.2001 bug in attribute output (full Bayes) fixed            16.07.2001 adapted to modified module scan            16.08.2003 slight changes in error message output----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.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.6 (2004.04.15)         " \                    "(c) 1998-2004   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 */#define E_CTYPE    (-13)        /* class is not symbolic */#define E_UNKNOWN  (-14)        /* unknown error *//*----------------------------------------------------------------------  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 \"%s\" in file %s\n",  /* E_CTYPE   -13 */  "class \"%s\" is not symbolic\n",  /* E_UNKNOWN -14 */  "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   *uvchars = NULL;       /* unknown value characters */  char   *clscol  = NULL;       /* class column name */  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 */  TFSERR *err;                  /* error information */  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 unknown 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");

⌨️ 快捷键说明

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