📄 mpx.c
字号:
/* --- evaluate arguments --- */ for (i = 1; i < argc; i++) { /* traverse 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 */ switch (*s++) { /* evaluate option */ case 'p': optarg = &res.n_pred; break; case 'c': optarg = &res.n_conf; break; case 'o': optarg = &res.format; break; case 't': thresh = strtod(s, &s); break; case 'a': outflags |= AS_ALIGN; break; case 'w': outflags &= ~AS_ATT; break; case 'v': verb = 1; break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'C': optarg = &comment; break; case 'd': inflags |= AS_DFLT; break; case 'h': optarg = &fn_hdr; break; default : error(E_OPTION, *--s); break; } /* set option variables */ if (!*s) break; /* if at end of string, abort loop */ if (optarg) { *optarg = s; optarg = NULL; break; } } } /* get option argument */ else { /* -- if argument is no option */ switch (k++) { /* evaluate non-option */ case 0: fn_reg = s; break; case 1: fn_tab = s; break; case 2: fn_out = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check option argument and */ if ((k != 2) && (k != 3)) /* the number of arguments */ error(E_ARGCNT); if (fn_hdr && (strcmp(fn_hdr, "-") == 0)) fn_hdr = ""; /* convert "-" to "" */ i = (!fn_reg || !*fn_reg) ? 1 : 0; if (!fn_tab || !*fn_tab) i++; if ( fn_hdr && !*fn_hdr) i++; if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (fn_hdr) /* set the header file flag */ inflags = AS_ATT | (inflags & ~AS_DFLT); if ((outflags & AS_ATT) && (outflags & AS_ALIGN)) outflags |= AS_ALNHDR; /* set align to header flag */ /* --- read regression polynomial --- */ t = clock(); /* start the timer */ scan = sc_create(fn_reg); /* create a scanner */ if (!scan) error((!fn_reg || !*fn_reg) ? E_NOMEM : E_FOPEN, fn_reg); fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if (sc_nexter(scan) < 0) error(E_PARSE, sc_fname(scan)); matinp = (sc_token(scan) == T_ID) && (strcmp(sc_value(scan), "dom") != 0); if (matinp) /* if matrix version */ reg = reg_parse(scan); /* parse the input network */ else { /* if table version */ attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM);/* create an attribute set */ if ((as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0)) /* parse the attribute set */ error(E_PARSE, sc_fname(scan)); attmap = am_create(attset, 0, 1.0); if (!attmap) error(E_NOMEM);/* create an attribute map */ reg = reg_parsex(scan, attmap); } /* parse the regression polynomial */ if (!reg || !sc_eof(scan)) error(E_PARSE, sc_fname(scan)); sc_delete(scan); scan = NULL; /* delete the scanner */ fprintf(stderr, "[%d parameters] done", reg_cnt(reg)); fprintf(stderr, " [%.2fs].\n", SEC_SINCE(t)); if (matinp) { /* if matrix version */ incnt = reg_cnt(reg) -1; /* get the number of inputs */ /* --- process patterns --- */ t = clock(); /* start the timer */ if (fn_tab && *fn_tab) /* if a file name is given, */ in = fopen(fn_tab, "r"); /* open the file for reading */ else { /* if no file name is given, */ in = stdin; fn_tab = "<stdin>"; } /* read from stdin */ fprintf(stderr, "reading %s ... ", fn_tab); if (!in) error(E_FOPEN, fn_tab); if (k > 2) { /* if to write an output file */ if (fn_out && *fn_out) /* if a file name is given, */ out = fopen(fn_out,"w");/* open the file for writing */ else { /* if no output file is given, */ fn_out = "<stdout>"; out = stdout; } /* use std. output */ if (!out) error(E_FOPEN, fn_out); } tscan = ts_create(); /* create a table scanner and */ if (!tscan) error(E_NOMEM); /* set the separator characters */ if (blanks) seps[0] = ts_chars(tscan, TS_BLANK, blanks); if (fldseps) seps[1] = ts_chars(tscan, TS_FLDSEP, fldseps); if (recseps) seps[2] = ts_chars(tscan, TS_RECSEP, recseps); if (comment) ts_chars(tscan, TS_COMMENT, comment); ts_chars(tscan,TS_NULL,""); /* remove null value characters */ err = ts_info(tscan); /* get the error information */ sse = 0; /* init. the sum of squared errors */ pat = vec_readx(tscan, in, &valcnt); if (!pat) { /* read the first training pattern */ if (err->code >= 0) error(E_FREAD, fn_tab); error(err->code, fn_tab, 1, err->s, err->fld, err->exp); } else { /* if a pattern could be read */ if ((valcnt != incnt) && (valcnt != incnt +1)) error(E_PATSIZE,valcnt);/* check the pattern size */ } /* (target value may be missing) */ do { /* pattern read loop */ pred = reg_exec(reg,pat); /* execute the regression polynomial */ if (valcnt > incnt) { /* and sum the squared errors */ diff = pred -pat[incnt]; sse += diff *diff; } if (out) { /* if to write an output table */ for (i = 0; i < valcnt; i++) { if (i > 0) fputc(seps[1], out); fprintf(out, res.format, pat[i]); } /* print the pattern elements */ fputc(seps[0], out); /* print a separating blank */ fputc(seps[1], out); /* and a field separator */ fprintf(out, res.format, pred); fputc(seps[2], out); /* print the prediction value */ } /* and terminate the record */ if (((++patcnt & 0x03ff) == 0) && verb) fprintf(stderr, "%8d\b\b\b\b\b\b\b\b", patcnt); } while (vec_read(pat, valcnt, tscan, in) == 0); if (err->code < 0) /* check for an error */ error(err->code, fn_tab, patcnt+1, err->s, err->fld, err->exp); if (ts_delim(tscan) != TS_EOF) /* check for end of file */ error(E_VALUE, fn_tab, patcnt+1, "\"\"", 1); if (out && (out != stdout)){/* if not written to stdout, */ k = fclose(out); out = NULL; /* close the output file */ if (k != 0) error(E_FWRITE, fn_out); } /* check for successful writing */ if (in != stdin) { /* if not read from standard input, */ fclose(in); in = NULL; } /* close the input file */ fprintf(stderr, "[%d pattern(s)] done", patcnt); fprintf(stderr, " [%.2fs].\n", SEC_SINCE(t)); if (valcnt > incnt) { /* if desired outputs are present */ fprintf(stderr, "sse : %g\n", sse); if (patcnt > 0) { /* if there was at least one pattern */ fprintf(stderr, "mse : %g\n", sse /patcnt); fprintf(stderr, "rmse: %g\n", sqrt(sse /patcnt)); } /* print some error measures */ } } /* for the test pattern set */ else { /* if table version */ res.att = as_att(attset, reg_trgid(reg)); res.type = att_type(res.att); /* get the target attribute */ if (res.type != AT_NOM) /* no confidence column */ res.n_conf = NULL; /* for numeric targets */ else if (att_valcnt(res.att) < 2) error(E_TARGET, att_name(res.att)); /* --- read table header --- */ t = clock(); /* start the timer */ for (i = as_attcnt(attset); --i >= 0; ) att_setmark(as_att(attset, i), 1); att_setmark(res.att, 0); /* mark all atts. except the target */ as_chars(attset, recseps, fldseps, blanks, "", comment); in = io_hdr(attset, fn_hdr, fn_tab, inflags|AS_MARKED, 1); if (!in) error(1); /* read the table header */ /* --- process tuples --- */ if ((att_getmark(res.att) < 0) /* either target must be present */ && (k <= 2)) /* or an output file must be written */ error(E_TARGET, att_name(res.att)); if (k > 2) { /* if to write an output table */ if ((outflags & AS_ALIGN) /* if to align output file */ && (in != stdin)) { /* and not to read from stdin */ i = AS_INST | (inflags & ~(AS_ATT|AS_DFLT)); while (as_read(attset, in, i) == 0); fclose(in); /* determine the column widths */ fprintf(stderr, "done.\n"); in = io_hdr(attset, fn_hdr, fn_tab, inflags|AS_MARKED, 1); if (!in) error(1); /* reread the table header */ } /* (necessary because of first tuple) */ if (fn_out && *fn_out) /* if a proper file name is given, */ out = fopen(fn_out,"w");/* open output file for writing */ else { /* if no proper file name is given, */ out = stdout; fn_out = "<stdout>"; } /* write to stdout */ if (!out) error(E_FOPEN, fn_out); k = AS_MARKED|AS_INFO1|AS_RDORD|outflags; if (outflags & AS_ATT) /* if to write table header */ as_write(attset, out, k, infout); k = AS_INST|(k & ~AS_ATT);/* write the attribute names */ } /* to the output file */ f = AS_INST | (inflags & ~(AS_ATT|AS_DFLT)); i = ((inflags & AS_DFLT) && !(inflags & AS_ATT)) ? 0 : as_read(attset, in, f); inst = att_inst(res.att); /* get the instance of the target */ while (i == 0) { /* record read loop */ tplcnt++; /* count tuple and sum its weight */ tplwgt += wgt = as_getwgt(attset); pred = reg_execx(reg, NULL); /* execute regression polynomial */ if (res.type == AT_NOM) { /* if the target att. is nominal */ if (pred >= thresh) { res.pred.i = 1; res.conf = 1.0 -pred; } else { res.pred.i = 0; res.conf = pred; } if (res.conf > 1.0) res.conf = 1.0; else if (res.conf < 0.0) res.conf = 0.0; if ((inst->i > NV_NOM) && (res.pred.i != inst->i)) errcnt += wgt; } /* count classification errors */ else { /* if the target att. is metric */ if (res.type == AT_INT){/* if it is integer-valued */ res.pred.i = (int)(pred +0.5); diff = (inst->i > NV_INT) ? res.pred.i -inst->i : 0; } else { /* if it is real-valued */ res.pred.f = (float)pred; diff = (inst->f > NV_REAL) ? res.pred.f -inst->f : 0; } /* compute diff. to the true value */ errcnt += wgt *diff *diff; } /* sum the squared errors */ if (out && (as_write(attset, out, k, infout) != 0)) error(E_FWRITE,fn_out); /* write tuple to output file */ if (verb && ((tplcnt & 0x03ff) == 0)) fprintf(stderr, "%8d\b\b\b\b\b\b\b\b", tplcnt); i = as_read(attset,in,f); /* try to read the next record */ } if (i < 0) { /* if an error occurred, */ err = as_err(attset); /* get the error information */ tplcnt += (inflags & (AS_ATT|AS_DFLT)) ? 1 : 2; io_error(i, fn_tab, tplcnt, err->s, err->fld, err->exp); error(1); /* print an error message */ } /* and abort the program */ if (in != stdin) fclose(in);/* close the table file and */ in = NULL; /* clear the file variable */ if (out && (out != stdout)) { /* if an output file exists, */ i = fclose(out); out = NULL; /* close the output file */ if (i) error(E_FWRITE, fn_out); } /* print a success message */ fprintf(stderr, "[%d/%g tuple(s)] done", tplcnt, tplwgt); fprintf(stderr, " [%.2fs].\n", SEC_SINCE(t)); if (att_getmark(res.att) >= 0) { if (res.type != AT_NOM) { /* if the target att. is metric */ fprintf(stderr, "sse: %g", errcnt); if (tplwgt > 0) { /* if there was at least one tuple */ errcnt /= tplwgt; /* compute mean squared error */ fprintf(stderr, ", mse: %g", errcnt); fprintf(stderr, ", rmse: %g", sqrt(errcnt)); } /* print some error measures */ fputc('\n', stderr); } else { /* if the target att. is nominal */ fprintf(stderr, "%g error(s) (%.2f%%)\n", errcnt, (tplwgt > 0) ? 100*(errcnt/tplwgt) : 0); } /* print an error indicator if */ } /* the target attribute is present */ } /* if (matinp) .. else .. */ /* --- clean up --- */ #ifndef NDEBUG if (!matinp) /* if table version */ reg_deletex(reg, 1); /* delete the regression polynomial */ else { /* if matrix version */ reg_delete(reg); /* delete the regression polynomial */ ts_delete(tscan); /* and the table scanner */ } #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -