📄 bci.c
字号:
printf("tabfile table file to read " "(field names in first record)\n"); printf("bcfile file to write Bayes classifier to\n"); return 0; /* print a usage message */ } /* and abort the program */ /* --- 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 'F': full = 1; break; case 'c': optarg = &clsname; break; case 'w': balance = (*s) ? *s++ : 0; break; case 's': simp = (*s) ? *s++ : 0; break; case 'L': lcorr = strtod(s, &s); break; case 't': setup |= NBC_DWNULL; break; case 'm': setup |= NBC_MAXLLH; break; case 'p': desc |= NBC_REL; break; case 'l': maxlen = (int)strtol(s, &s, 0); break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'u': optarg = &nullchs; break; case 'C': optarg = &comment; break; case 'n': flags |= AS_WEIGHT; break; case 'd': flags |= 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_dom = s; break; case 1: fn_tab = s; break; case 2: fn_bc = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check the option argument */ if (k != 3) error(E_ARGCNT); /* and the number of arguments */ if (fn_hdr && (strcmp(fn_hdr, "-") == 0)) fn_hdr = ""; /* convert "-" to "" */ i = (!fn_dom || !*fn_dom) ? 1 : 0; if (!fn_tab || !*fn_tab) i++; if ( fn_hdr && !*fn_hdr) i++;/* check assignments of stdin: */ if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (simp == 'a') setup |= NBC_ADD; else if (simp == 'r') setup |= NBC_REMOVE; else if (simp != 0 ) /* check simplification mode */ error(E_SIMP, simp); /* (must be 'add' or 'remove') */ if ((balance != 0) && (balance != 'l') && (balance != 'b') && (balance != 's')) error(E_BALANCE, balance); /* check balancing mode */ if (fn_hdr) /* set the header file flag */ flags = AS_ATT | (flags & ~AS_DFLT); /* --- read attribute set --- */ t = clock(); /* start the timer */ scan = sc_create(fn_dom); /* create a scanner */ if (!scan) error((!fn_dom || !*fn_dom) ? E_NOMEM : E_FOPEN, fn_dom); attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM); /* create an attribute set */ fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if ((sc_nexter(scan) < 0) /* start scanning (get first token) */ || (as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0) /* parse attribute set and */ || !sc_eof(scan)) /* check for end of file */ error(E_PARSE, sc_fname(scan)); attcnt = as_attcnt(attset); /* print a success message */ fprintf(stderr, "[%d attribute(s)] done ", attcnt); fprintf(stderr, "[%.2fs].", SEC_SINCE(t)); /* --- filter attributes --- */ for (i = attcnt; --i >= 0; ) { att = as_att(attset, i); /* traverse the attributes */ k = att_getdir(att); /* and get their directions */ att_setmark(att, ((k == DIR_IN) || (k == DIR_OUT)) ? -1 : +1); } /* mark input and output attributes */ as_attcut(NULL, attset, AS_MARKED); attcnt = as_attcnt(attset); /* cut all other attributes and */ for (i = attcnt; --i >= 0; ) /* clear all attribute markers */ att_setmark(as_att(attset, i), 0); /* --- determine id of class column --- */ if (clsname) { /* if a class att. name is given */ for (i = attcnt; --i >= 0;) /* remove all attribute directions */ att_setdir(as_att(attset, i), DIR_IN); clsid = as_attid(attset, clsname); if (clsid < 0) error(E_CLASS, clsname, sc_fname(scan)); } else { /* if no target att. name is given */ for (clsid = -1, i = attcnt; --i >= 0; ) { if (att_getdir(as_att(attset, i)) != DIR_OUT) continue; if (clsid < 0) clsid = i; else error(E_MULTCLS); } /* find a (unique) output attribute */ if (clsid < 0) clsid = attcnt -1; } /* by default use the last attribute */ att = as_att(attset, clsid); /* get the class attribute and */ att_setdir(att, DIR_OUT); /* set the class attribute direction */ if (att_type(att) != AT_NOM) /* check the type of the class */ error(E_CTYPE, att_name(att)); /* (must be nominal) */ sc_delete(scan); scan = NULL; /* delete the scanner */ fprintf(stderr, "\n"); /* terminate the startup message */ /* --- read table header --- */ as_chars(attset, recseps, fldseps, blanks, nullchs, comment); in = io_hdr(attset, fn_hdr, fn_tab, flags, 1); if (!in) error(1); /* read the table header */ /* --- build naive Bayes classifier --- */ if (!full /* if to induce a naive Bayes class. */ && (balance /* and to balance class frequencies */ || simp)) { /* or to simplify the classifier */ table = io_bodyin(attset, in, fn_tab, flags, "table", 2); if (!table) error(1); /* read the table body */ t = clock(); /* start the timer */ fprintf(stderr, "reducing%s table ... ", (balance) ? " and balancing" : ""); tab_reduce(table); /* reduce table for speed up */ if (balance) { /* if the balance flag is set */ tab_balance(table, clsid, (balance == 'l') ? -2.0F : (balance == 'b') ? -1.0F : 0.0F, NULL); } /* balance the class frequencies */ tplwgt = tab_getwgt(table, 0, INT_MAX); fprintf(stderr, "[%d/%g tuple(s)] ", tab_tplcnt(table), tplwgt); fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)); t = clock(); /* start the timer */ fprintf(stderr, "building classifier ... "); nbc = nbc_induce(table, clsid, setup, lcorr); if (!nbc) error(E_NOMEM); /* induce a classifier and */ attcnt = nbc_mark(nbc); /* mark the selected attributes */ fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)); } else { /* if to build a normal classifier */ if (full) fbc = fbc_create(attset, clsid); else nbc = nbc_create(attset, clsid); if (!fbc && !nbc) /* create either a full or */ error(E_NOMEM); /* a naive Bayes classifier */ t = clock(); /* start the timer */ k = AS_INST | (flags & ~(AS_ATT|AS_DFLT)); i = ((flags & AS_DFLT) && !(flags & AS_ATT)) ? 0 : as_read(attset, in, k); while (i == 0) { /* record read loop */ if (((fbc) ? fbc_add(fbc, NULL) : nbc_add(nbc, NULL)) != 0) error(E_NOMEM); /* process tuple and count it */ tplcnt++; tplwgt += as_getwgt(attset); i = as_read(attset, in, k); } /* try to read the next record */ if (i < 0) { /* if an error occurred, */ err = as_err(attset); /* get the error information */ tplcnt += (flags & (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 input file */ in = NULL; /* and set up the classifier */ if (fbc) { fbc_setup(fbc, setup, lcorr); attcnt = fbc_mark(fbc); } else { nbc_setup(nbc, setup|NBC_ALL, lcorr); } fprintf(stderr, "[%d/%g tuple(s)] ", tplcnt, tplwgt); fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)); } /* print a success message */ /* --- describe created classifier --- */ t = clock(); /* start the timer */ if (fn_bc && *fn_bc) /* if an output file name is given, */ out = fopen(fn_bc, "w"); /* open output file for writing */ else { /* if no output file name is given, */ out = stdout; fn_bc = "<stdout>"; } /* write to std. output */ fprintf(stderr, "writing %s ... ", fn_bc); if (!out) error(E_FOPEN, fn_bc); k = (full || simp) /* print only the class and */ ? AS_MARKED : 0; /* the marked attributes */ if (as_desc(attset, out, k|AS_TITLE|AS_IVALS, maxlen) != 0) error(E_FWRITE, fn_bc); /* describe attribute domains */ fputc('\n', out); /* leave one line empty */ k = (simp) ? NBC_MARKED : 0; /* print only marked attributes */ if (((fbc) ? fbc_desc(fbc, out, desc |FBC_TITLE, maxlen) : nbc_desc(nbc, out, desc|k|NBC_TITLE, maxlen)) != 0) error(E_FWRITE, fn_bc); /* describe Bayes classifier */ if (maxlen <= 0) maxlen = 72; /* determine maximal line length */ fputs("\n/*", out); /* append additional information */ for (k = maxlen -2; --k >= 0; ) fputc('-', out); fprintf(out, "\n number of attributes: %d", attcnt); fprintf(out, "\n number of tuples : %g\n", tplwgt); for (k = maxlen -2; --k >= 0; ) fputc('-', out); fputs("*/\n", out); /* terminate additional information */ if (out != stdout) { /* if not written to stdout, */ k = fclose(out); out = NULL;/* close the output file */ if (k) error(E_FWRITE, fn_bc); } /* print a success message */ fprintf(stderr, "[%d+1 attribute(s)] ", attcnt-1); fprintf(stderr, "done [%.2fs].\n", SEC_SINCE(t)); /* --- clean up --- */ #ifndef NDEBUG if (table) tab_delete(table, 0); /* delete table, */ if (nbc) nbc_delete(nbc, 1); /* naive Bayes classifier, */ if (fbc) fbc_delete(fbc, 1); /* full Bayes classifier, */ #endif /* and underlying attribute set */ #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -