📄 bcx.c
字号:
/*---------------------------------------------------------------------- File : bcx.c Contents: naive and full Bayes classifier execution Author : Christian Borgelt History : 08.12.1998 file created from file dtc.c 13.02.1999 input from stdin, output to stdout added 17.04.1999 simplified using the new module 'io' 20.11.2000 option -m (max. likelihood est. for var.) added 21.11.2000 adapted to redesigned module nbayes 30.11.2000 full Bayes classifier execution added 16.07.2001 adapted to modified module scan 18.09.2002 bug concerning missing target fixed 02.02.2003 bug in alignment in connection with -d fixed 23.04.2003 missing AS_MARKED added for second reading 16.08.2003 slight changes in error message output 22.02.2005 classification threshold added (option -t)----------------------------------------------------------------------*/#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.9 (2005.02.22) " \ "(c) 1998-2005 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 */} CRINFO; /* (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 CRINFO cri = { /* classification result information */ NULL, /* class attribute */ "bc", 0, 0, /* data for classification column */ NULL, 0, 0.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 */ char buf[32]; /* buffer for output */ if (mode & AS_ATT) { /* if to write header */ class = cri.n_class; /* get name of classification column */ prob = cri.n_prob; /* and name of probability column */ if (mode & AS_ALIGN) { /* if to align fields */ if ((mode & AS_WEIGHT) || prob) { i = att_valwd(cri.att, 0); k = (int)strlen(class); cri.w_class = (i > k) ? i : k; } /* compute width of class column */ if (prob && (mode & AS_WEIGHT)) { k = (int)strlen(prob); cri.w_prob = (4 > k) ? 4 : k; } } } /* compute width of prob. column */ else { /* if to write a normal record */ class = att_valname(cri.att, cri.class); if (cri.n_prob) { /* if to print a probability field */ if (cri.prob >= 0.9995) sprintf(buf, "1.00"); else sprintf(buf, ".%03.0f", cri.prob *1000); } /* format the probability */ prob = buf; /* and get the buffer */ } /* get and format field contents */ k = fprintf(file, class); /* write classification result */ for (i = cri.w_class -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ if (cri.n_prob) { /* if to write class probability */ fputc(seps[1], file); /* write field separator */ fputs(prob, file); /* and number of errors */ for (i = cri.w_prob -k; --i >= 0; ) fputc(seps[0], file); } /* if to align, pad with blanks */} /* 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 *uvchars = NULL; /* unknown value characters */ double lcorr = -DBL_MAX; /* Laplace correction value */ double thresh = 0.5; /* classification threshold */ int distuv = 0; /* distribute weight of unknowns */ 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 */ int attid; /* loop variable for attributes */ float wgt; /* tuple/instantiation weight */ int mode; /* classifier setup mode */ TFSERR *err; /* error information */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -