📄 mpr.c
字号:
/*---------------------------------------------------------------------- File : mpr.c Contents: multivariate polynomial regression Author : Christian Borgelt History : 2001.10.13 file created 2001.10.15 first version completed 2001.10.18 some messages added, option -v added 2001.10.20 computation of sum of squared errors added 2001.10.26 adapted to modified module regress 2003.08.16 slight changes in error message output 2003.10.25 logistic regression added (logit transformation) 2007.02.13 adapted to modified module tabscan 2007.05.18 execution time measurements added 2007.10.10 evaluation of attribute directions added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <time.h>#include <assert.h>#include <math.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#ifndef REG_EXTFN#define REG_EXTFN#endif#include "regress.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "mpr"#define DESCRIPTION "multivariate polynomial regression"#define VERSION "version 2.5 (2007.10.10) " \ "(c) 2001-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_TARGET (-10) /* unknown/invalid target attribute */#define E_MULTTRG (-11) /* multiple target attributes */#define E_DEGREE (-12) /* invalid degree of polynomial */#define E_DIM (-13) /* too few fields */#define E_LOGIT (-14) /* logit transformation failed */#define E_REGRESS (-15) /* regression failed */#define E_UNKNOWN (-18) /* 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_TARGET -10 */ "unknown or invalid target attribute \"%s\"\n", /* E_MULTTRG -11 */ "multiple target attributes\n", /* E_DEGREE -12 */ "invalid degree of polynomial %d\n", /* E_DIM -13 */ "too few fields (need at least 2)\n", /* E_LOGIT -14 */ "logit transformation failed: %g\n", /* E_REGRESS -15 */ "regression failed " "(cannot solve equation system)\n", /* 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) instead of %d\n", /* E_UNKNOWN -18 */ "unknown error\n"};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static ATTSET *attset = NULL; /* attribute set */static ATTMAP *attmap = NULL; /* attribute map */static TABSCAN *tscan = NULL; /* table scanner */static SCAN *scan = NULL; /* scanner */static double *vec = NULL; /* vector to read table rows */static REGRESS *reg = NULL; /* regression object *//*---------------------------------------------------------------------- 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 (vec) free(vec); /* and close files */ if (reg) reg_deletex(reg, 0); 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 #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, f; /* loop variables, counter */ char *s; /* to traverse options */ char **optarg = NULL; /* option argument */ char *fn_dom = NULL; /* name of domain file */ char *fn_hdr = NULL; /* name of table header file */ char *fn_tab = NULL; /* name of table file */ char *fn_reg = NULL; /* name of regression function file */ char *blanks = NULL; /* blanks */ char *fldseps = NULL; /* field separators */ char *recseps = NULL; /* record separators */ char *comment = NULL; /* comment characters */ char *trgname = NULL; /* target attribute name */ int matinp = 0; /* flag for numerical matrix input */ int flags = AS_NOXATT|AS_NONULL; /* table file read flags */ int dim = -1; /* vector dimension */ int deg = 1; /* degree of polynomial */ double logit = -1; /* scale for logit transformation */ int cnt = 0; /* number of data vectors */ int verb = 0; /* flag for verbose message output */ int attcnt = 0; /* number of attributes */ int tplcnt = 0; /* number of tuples */ double tplwgt = 0.0; /* weight of tuples */ int mode = REG_TITLE; /* description mode */ TSINFO *err = NULL; /* error information of scanner */ int trgid; /* id of the target column */ ATT *att; /* to traverse attributes */ 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] [-M|domfile [-d|-h hdrfile]] " "tabfile regfile\n", argv[0]); printf("%s\n", DESCRIPTION); printf("%s\n", VERSION); printf("-M input is a numerical matrix " "(default: input is a table)\n"); printf("-o# output/target attribute name " "(default: last attribute)\n"); printf("-x# degree of regression polynomial " "(default: %d, i.e., linear)\n", deg); printf("-l# maximum for logit transformation " "(default: normal regression)\n"); printf("-e print exponents of regression variables\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("domfile file containing domain descriptions\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("regfile file to write the regression model to\n"); return 0; /* print a usage message */ } /* and abort the program */ /* --- evaluate arguments --- */ for (i = 1; i < argc; i++) { /* traverse the arguments */ s = argv[i]; /* get option argument */ if (optarg) { *optarg = s; optarg = NULL; continue; } if ((*s == '-') && *++s) { /* -- if argument is an option */ while (1) { /* traverse characters */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -