📄 dtx.c
字号:
/*---------------------------------------------------------------------- File : dtx.c Contents: decision and regression tree execution Authors : Christian Borgelt History : 01.08.1997 file created (as dtc.c) 04.08.1997 first version completed 28.08.1997 output file made optional 30.08.1997 classification field name made variable 02.01.1998 column/field alignment corrected 11.01.1998 unknown value characters (option -u) added 08.02.1998 adapted to changed parse functions 13.03.1998 no header in output file (option -w) added 23.06.1998 adapted to modified attset functions 03.09.1998 support and confidence (options -s/-c) added 09.02.1999 input from stdin, output to stdout added 17.04.1999 simplified using the new module 'io' 17.12.2000 bug in alignment of support and confidence fixed 18.12.2000 extended to regression trees 23.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 09.11.2004 execution time output added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <math.h>#include <string.h>#include <time.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#ifndef TAB_RDWR#define TAB_RDWR#endif#ifndef DT_PARSE#define DT_PARSE#endif#include "io.h"#include "dtree.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "dtx"#define DESCRIPTION "decision and regression tree execution"#define VERSION "version 3.8 (2006.02.07) " \ "(c) 1997-2006 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_UNKNOWN (-11) /* unknown error */#define SEC_SINCE(t) ((clock()-(t)) /(double)CLOCKS_PER_SEC)/*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- prediction result --- */ ATT *att; /* target attribute */ int type; /* type of the target attribute */ char *n_pred; /* name of prediction column */ int w_pred; /* width of prediction column */ INST pred; /* predicted value */ char *n_supp; /* name of support column */ int w_supp; /* width of support column */ float supp; /* support of prediction */ char *n_conf; /* name of confidence column */ int w_conf; /* width of confidence column */ float conf; /* confidence of prediction */} RESULT; /* (prediction result) *//*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/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_UNKNOWN -11 */ "unknown error\n"};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static SCAN *scan = NULL; /* scanner */static DTREE *dtree = NULL; /* decision/regression tree */static ATTSET *attset = NULL; /* attribute set */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static RESULT res = { /* prediction result */ NULL, AT_SYM, /* target attribute and its type */ "dt", 0, { 0 }, /* data for prediction column */ NULL, 0, 0.0F, /* data for support column */ NULL, 0, 0.0F }; /* data for confidence 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 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 error message */ va_end(args); /* end argument evaluation */ } #ifndef NDEBUG if (dtree) dt_delete(dtree, 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 the program */} /* error() *//*--------------------------------------------------------------------*/static void infout (ATTSET *set, FILE *file, int mode, CCHAR *seps){ /* --- write additional information */ static char *cfmt = "%.2f"; /* format for confidence output */ int i, k; /* loop variables, buffers */ CCHAR *pred; /* name of prediction column/value */ char *supp, *conf; /* support and confidence */ char b0[32], b1[32], b2[32]; /* buffers for output */ if (mode & AS_ATT) { /* if to write header */ cfmt = (res.type == AT_SYM) ? "%.2f" : "%g"; pred = res.n_pred; /* get names of prediction column, */ supp = res.n_supp; /* support column, and */ conf = res.n_conf; /* confidence column */ if (mode & AS_ALIGN) { /* if to align fields */ if ((mode & AS_WEIGHT) || supp || conf) { i = att_valwd(res.att, 0); k = (int)strlen(pred); res.w_pred = (i > k) ? i : k; } /* compute width of class column */ i = sprintf(b1, "%.1f", dt_total(dtree)); if (supp && ((mode & AS_WEIGHT) || conf)) { k = (int)strlen(supp); res.w_supp = (i > k) ? i : k; } /* compute width of support column */ if (conf && (mode & AS_WEIGHT)) { k = (int)strlen(conf); res.w_conf = (k > 4) ? k : 4; } } } /* compute width of errors column */ else { /* if to write normal record */ if (res.type == AT_SYM) /* if the target att. is symbolic */ pred = att_valname(res.att, res.pred.i); else { /* if the target att. is numeric */ sprintf(b0, "%g", res.pred.f); pred = b0; /* print the predicted value and */ } /* get the pointer to the buffer */ if (res.n_supp) sprintf(b1, "%.1f", res.supp); if (res.n_conf) sprintf(b2, cfmt, res.conf); supp = b1; conf = b2; /* get and format field contents */ } /* and get pointers to buffers */ k = fprintf(file, pred); /* write the prediction result */ for (i = res.w_pred -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ if (res.n_supp) { /* if to write the support */ fputc(seps[1], file); /* write a field separator and */ k = fprintf(file, supp); /* the support of the prediction */ for (i = res.w_supp -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ } if (res.n_conf) { /* if to write the confidence */ fputc(seps[1], file); /* write a field separator and the */ k = fprintf(file, conf); /* confidence of the prediction */ for (i = res.w_conf -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ }} /* infout() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -