📄 bcload.c
字号:
/*---------------------------------------------------------------------- File : bcload.c Contents: routines for loading Bayes classifiers and data (common to the X11 and the Windows program) Author : Christian Borgelt History : 18.02.2000 file created from file xbcview.c 08.06.2000 bug in function bc_load removed (ranges) 13.12.2000 bug concerning DBL_MIN removed 12.04.2003 clustering result loading added 17.08.2004 radial basis function networks added----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#ifndef SC_SCAN#define SC_SCAN#endif#include "scan.h"#include "bcload.h"/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*//* --- classifier and attribute information --- */ATTSET *attset = NULL; /* attribute set */const char **names = NULL; /* attribute names */RANGE *ranges = NULL; /* ranges of attribute values */int *attmap = NULL; /* map attributes to attset ids. */NBC *nbc = NULL; /* naive Bayes classifier */FBC *fbc = NULL; /* full Bayes classifier */CLSET *clset = NULL; /* cluster set */RBFNET *rbf = NULL; /* radial basis function network */BVNORM *bvnorm = NULL; /* bivariate normal distributions */int attcnt = 0; /* number of numeric attributes */ATTINFO attinfo = /* attribute information */ { 0, { 0.0, 1.0 }, 0, { 0.0, 1.0 } };/* --- data information --- */TABLE *table = NULL; /* data table */FMTINFO fmtinfo = /* data format information */ { FR_ATTS, " \\t\\r", " \\t", "\\n", "?" };int recno = 0; /* record number (for error messages) */ static char buf[256]; /* read buffer *//*---------------------------------------------------------------------- Functions----------------------------------------------------------------------*/void bc_clean (void){ /* --- clean up after a load error */ if (bvnorm) { free(bvnorm); bvnorm = NULL; } if (ranges) { free(ranges); ranges = NULL; } if (attmap) { free(attmap); attmap = NULL; } if (names) { free((void*)names); names = NULL; } if (table) { tab_delete(table, 0); table = NULL; } if (rbf) { rbf_deletex(rbf, 0); clset = NULL; rbf = NULL; } if (fbc) { fbc_delete(fbc, 0); fbc = NULL; } if (nbc) { nbc_delete(nbc, 0); nbc = NULL; } if (clset) { cls_delete(clset); clset = NULL; } if (attset) { as_delete(attset); attset = NULL; } attcnt = 0; /* free memory and clear variables */} /* bc_clean() *//*--------------------------------------------------------------------*/int bc_load (const char *fname){ /* --- load classifier/clusters */ int i, k, n, r; /* loop variables and counters */ int clscnt; /* number of classes/clusters */ int type; /* attribute type */ SCAN *scan; /* scanner */ ATT *att; /* to traverse the attributes */ RANGE *rng; /* to traverse attribute value ranges */ MVNORM *mvn; /* to traverse the normal distribs. */ MATRIX *mat; /* to traverse the matrices */ float f; /* buffer for a min/max value */ double exp, dev, t; /* expected value and std. deviation */ /* --- load domain definitions --- */ scan = sc_create(fname); /* start scanning the file */ if (!scan) return E_FOPEN; /* and check for an error */ sc_errfile(scan, NULL, 1); /* set error reporting parameters */ bc_clean(); /* clean up existing variables */ attset = as_create("domains", att_delete); if (!attset /* create and attribute set */ || (sc_nexter(scan) < 0) /* and parse it */ || (as_parse(attset, scan, AT_ALL) != 0)) { sc_delete(scan); return E_PARSE; } n = as_attcnt(attset); /* get the number of attributes */ /* --- load Bayes classifier/cluster set/RBF network --- */ k = sc_token(scan); /* check the next token */ if ((k == T_ID) && (strcmp(sc_value(scan), "nbc") == 0)) { nbc = nbc_parse(attset, scan); if (!nbc || !sc_eof(scan)){ /* parse a naive Bayes classifier */ sc_delete(scan); return E_PARSE; } sc_delete(scan); /* delete the scanner */ if (nbc_total(nbc) <= 0) /* check for a positive case total */ return E_CREATE; clscnt = nbc_clscnt(nbc); } /* get the number of classes */ else if ((k == T_ID) && (strcmp(sc_value(scan), "fbc") == 0)) { fbc = fbc_parse(attset, scan); if (!fbc || !sc_eof(scan)){ /* parse a full Bayes classifier */ sc_delete(scan); return E_PARSE; } sc_delete(scan); /* delete the scanner */ if (fbc_total(fbc) <= 0) /* check for a positive case total */ return E_CREATE; clscnt = fbc_clscnt(fbc); } /* get the number of classes */ else if ((k == T_ID) && (strcmp(sc_value(scan), "clset") == 0)) { clset = cls_parsex(scan, attset, 0); if (!clset || !sc_eof(scan)) { /* parse a cluster set */ sc_delete(scan); return E_PARSE; } sc_delete(scan); /* delete the scanner */ clscnt = cls_clscnt(clset); /* get the number of clusters */ cls_unscale(clset); } /* remove all attribute scaling */ else if ((k == T_ID) && (strcmp(sc_value(scan), "rbfnet") == 0)) { rbf = rbf_parsex(scan, attset, 0); if (!rbf || !sc_eof(scan)){ /* parse an RBF network */ sc_delete(scan); return E_PARSE; } sc_delete(scan); /* delete the scanner */ clset = rbf_clset(rbf); /* get the underlying cluster set */ clscnt = cls_clscnt(clset); /* get the number of clusters */ cls_unscale(clset); } /* remove all attribute scaling */ else { sc_error(scan, E_STREXP, "nbc"); sc_delete(scan); return E_PARSE; } /* --- create the attribute vectors --- */ for (k = attcnt = 0; k < n; k++) { type = att_type(as_att(attset, k)); if ((type == AT_INT) || (type == AT_FLT)) attcnt++; } /* count the numeric attributes */ if (attcnt < 1) return E_ATTCNT;/* and check for at least one */ bvnorm = (BVNORM*) malloc(clscnt *sizeof(BVNORM)); names = (const char**)malloc(attcnt *sizeof(const char*)); ranges = (RANGE*) malloc(attcnt *sizeof(RANGE)); attmap = (int*) malloc(attcnt *sizeof(int)); if (!bvnorm || !names || !ranges || !attmap) return E_NOMEM; /* --- build classifier and attribute information --- */ attinfo.h_att = attinfo.v_att = -1; rng = ranges; /* initialize attribute indices */ for (k = attcnt = 0; k < n; k++) { att = as_att(attset, k); /* traverse the attributes */ type = att_type(att); /* and get the attribute type */ if ((type != AT_INT) && (type != AT_FLT)) continue; /* skip non-numeric attributes */ if (attinfo.h_att < 0) /* note the first numeric attribute */ attinfo.h_att = attcnt; /* for the horizontal direction */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -