📄 mpx.c
字号:
/*---------------------------------------------------------------------- File : mpx.c Contents: multivariate polynomial regression execution Author : Christian Borgelt History : 2007.04.12 file created from file mlpx.c 2007.05.18 execution time measurements added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <math.h>#include <time.h>#include <assert.h>#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#include "io.h"#ifndef MAT_READ#define MAT_READ#endif#include "matrix.h"#ifndef REG_PARSE#define REG_PARSE#endif#ifndef REG_EXTFN#define REG_EXTFN#endif#include "regress.h"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "mpx"#define DESCRIPTION "multivariate polynomial regression execution"#define VERSION "version 1.4 (2007.09.02) " \ "(c) 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 errors on input file */#define E_PATCNT (-10) /* no pattern found */#define E_PATSIZE (-11) /* invalid pattern size */#define E_TARGET (-12) /* unknown target attribute */#define E_UNKNOWN (-18) /* 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_conf; /* name of confidence column */ int w_conf; /* width of confidence column */ double conf; /* confidence of prediction */ char *format; /* number output format */} RESULT; /* (prediction result) *//*---------------------------------------------------------------------- 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_PATCNT -10 */ "no pattern in file %s\n", /* E_PATSIZE -11 */ "invalid pattern size %d\n", /* E_TARGET -12 */ "unknown or invalid target attribute %s\n", /* -13 to -15 */ NULL, NULL, NULL, /* E_VALUE -16 */ "file %s, record %d: " "invalid value %s in field %d\n", /* E_FLDCNT -17 */ "file %s, record %d: " "%s, %d field(s) expected\n", /* E_UNKNOWN -18 */ "unknown error\n",};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static SCAN *scan = NULL; /* scanner */static TABSCAN *tscan = NULL; /* table scanner */static ATTSET *attset = NULL; /* attribute set */static ATTMAP *attmap = NULL; /* attribute map */static REGRESS *reg = NULL; /* regression object */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static RESULT res = { /* prediction result */ NULL, AT_NOM, /* target attribute */ "reg", 0, { 0 }, /* data for prediction column */ NULL, 0, 0.0F, "%g" }; /* data for confidence column *//*---------------------------------------------------------------------- Main 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 /* clean up memory */ if (reg) reg_delete(reg); /* and close files */ if (attmap) am_delete(attmap); if (attset) as_delete(attset); if (tscan) ts_delete(tscan); if (scan) sc_delete(scan); if (in && (in != stdin)) fclose(in); if (out && (out != stdout)) fclose(out); #endif exit(code); /* abort the program */} /* error() *//*--------------------------------------------------------------------*/static void infout (ATTSET *set, FILE *file, int mode, const char *seps){ /* --- write additional information */ int i, k; /* loop variables, buffers */ CCHAR *pred; /* name of prediction column/value */ char *conf; /* confidence of the prediction */ char b0[32], b1[32]; /* buffers for output */ if (mode & AS_ATT) { /* if to write the header */ pred = res.n_pred; /* get names of the prediction column */ conf = res.n_conf; /* and the confidence column */ if (mode & AS_ALIGN) { /* if to align fields */ if ((mode & AS_WEIGHT) || conf) { i = att_valwd(res.att, 0); k = (int)strlen(pred); res.w_pred = (i > k) ? i : k; } /* compute width of class 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_NOM) /* if the target att. is nominal */ pred = att_valname(res.att, res.pred.i); else { /* if the target att. is metric */ if (res.type == AT_INT) sprintf(b0, "%d", res.pred.i); else sprintf(b0, res.format, res.pred.f); pred = b0; /* format the number in a buffer */ } /* and get this buffer */ if (res.n_conf) sprintf(b1, res.format, res.conf); conf = b1; /* 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_conf) { /* if to write a confidence field */ 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() *//*--------------------------------------------------------------------*/int main (int argc, char *argv[]){ /* --- main function */ int i, k = 0; /* loop variables, counters */ char *s; /* to traverse options */ char **optarg = NULL; /* option argument */ char *fn_hdr = NULL; /* name of table header file */ char *fn_reg = NULL; /* name of network file */ char *fn_tab = NULL; /* name of input pattern file */ char *fn_out = NULL; /* name of output pattern file */ char *blanks = NULL; /* blank characters */ char *fldseps = NULL; /* field separators */ char *recseps = NULL; /* record separators */ char *comment = NULL; /* comment characters */ char seps[4] = " \n"; /* separator characters */ int matinp = 0; /* flag for numerical matrix input */ double thresh = 0.5; /* classification threshold */ int patcnt = 0; /* number of test patterns */ int valcnt = 0; /* number of values in a pattern */ int incnt = 0; /* number of inputs in a pattern */ int inflags = 0; /* table file read flags */ int outflags = AS_ATT, f; /* table file write flags */ int verb = 0; /* flag for verbose message output */ int tplcnt = 0; /* number of tuples */ double tplwgt = 0.0; /* weight of tuples */ double errcnt = 0.0; /* number of misclassifications */ double *pat; /* to traverse the patterns */ INST *inst; /* to access the target value */ double pred; /* prediction value */ double diff; /* difference to true value */ float wgt; /* tuple/instantiation weight */ double sse; /* (weighted) sum of squared errors */ TSINFO *err; /* error information */ clock_t t; /* for time 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 is given */ printf("usage: %s [options] regfile [-d|-h hdrfile] " "tabfile [outfile]\n", argv[0]); printf("%s\n", DESCRIPTION); printf("%s\n", VERSION); printf("-p# prediction field name " "(default: \"%s\")\n", res.n_pred); printf("-c# confidence field name " "(default: no confidence field)\n"); printf("-t# classification threshold " "(default: %g)\n", thresh); printf("-o# output format for numbers " "(default: \"%s\")\n", res.format); printf("-a align fields (default: do not align)\n"); printf("-w do not write field names to output file\n"); printf("-v verbose message output " "(print data vector counter)\n"); printf("-b/f/r# blank characters, field and record separators\n" " (default: \" \\t\\r\", \" ,\\t\", \"\\n\")\n"); printf("-C# comment characters (default: \"#\")\n"); printf("regfile file to read regression polynomial from\n"); printf("-d use default 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"); printf("tabfile table file to read " "(field names in first record)\n"); printf("outfile file to write output table to (optional)\n"); return 0; /* print a usage message */ } /* and abort the program */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -