📄 bcload.c
字号:
else if (attinfo.v_att < 0) /* note the second numeric attribute */ attinfo.v_att = attcnt; /* for the vertical direction */ names [attcnt] = att_name(att); attmap[attcnt++] = k; /* note the attribute identifier */ if (type == AT_INT) { /* if attribute is integer valued */ i = att_valmin(att)->i; /* get and set minimum value */ rng->min = (i <= INT_MIN +1) ? -DBL_MAX : (double)i; i = att_valmax(att)->i; /* get and set maximum value */ rng->max = (i >= INT_MAX) ? DBL_MAX : (double)i; } else { /* if attribute is real valued */ f = att_valmin(att)->f; /* get and set minimum value */ rng->min = (f <= -FLT_MAX) ? -DBL_MAX : (double)f; f = att_valmax(att)->f; /* get and set maximum value */ rng->max = (f >= FLT_MAX) ? DBL_MAX : (double)f; } /* (set initial range of values ) */ for (i = clscnt; --i >= 0;){/* traverse the normal distributions */ if (nbc) { /* if naive Bayes classifier */ exp = nbc_exp(nbc, i, k); dev = sqrt(nbc_var(nbc, i, k)); } else if (fbc) { /* if full Bayes classifier */ mvn = fbc_mvnorm(fbc, i); exp = mvn_exp(mvn, k); dev = sqrt(mvn_var(mvn, k)); } else { /* if cluster set */ r = am_off(cls_attmap(clset), k); exp = cls_center(clset, i)[r]; mat = cls_covmat(clset, i); dev = sqrt((mat) ? mat_get(mat, r, r) : cls_isovar(clset, i)); } /* get exp. value and std. deviation */ t = exp -3 *dev; if (t < rng->min) rng->min = t; t = exp +3 *dev; if (t > rng->max) rng->max = t; } /* adapt the value range */ if (rng->min >= rng->max) { rng->min = 0; rng->max = 1; } rng++; /* check and adapt final range */ } /* and go to the next attribute */ if (attinfo.v_att < 0) attinfo.v_att = attinfo.h_att; attinfo.h_rng = ranges[attinfo.h_att]; attinfo.v_rng = ranges[attinfo.v_att]; return 0; /* get ranges of selected attributes */} /* bc_load() */ /* and return `ok' *//*--------------------------------------------------------------------*/int bc_data (const char *fname){ /* --- load data table */ int i, r, f; /* loop variable, buffers */ FILE *file; /* input file */ TFSCAN *tfs; /* table file scanner */ ATT *att; /* class attribute */ if (!attset) return E_DATA; /* check for an attribute set */ for (i = as_attcnt(attset); --i >= 0; ) att_setmark(as_att(attset, i), 1); if (nbc || fbc) { /* mark all attributes and if class. */ att = as_att(attset, (nbc) ? nbc_clsid(nbc) : fbc_clsid(fbc)); att_setmark(att, 0); /* get the class attribute, */ att_inst(att)->i = UV_SYM; /* mark it as optional, and */ } /* set its value to unknown */ /* With these preparations and the flag AS_MARKED in the call to */ /* as_read the class is read only if it is present in the data. */ /* Otherwise the class is set to unknown in all tuples. */ file = fopen(fname, "r"); /* open table file for input */ if (!file) return E_FOPEN; /* and set the format characters */ as_chars(attset, fmtinfo.blanks, fmtinfo.fldseps, fmtinfo.recseps, fmtinfo.uvchars); recno = 1; /* initialize the record number */ if (fmtinfo.first == FR_COMMENT) { tfs = as_tfscan(attset); /* if to skip a comment line, */ do { /* loop to skip fields */ r = tfs_getfld(tfs, file, buf, sizeof(buf)-1); if (r < 0) { fclose(file); return E_FREAD; } } while (r > TFS_REC); /* ignore all fields up to */ recno++; /* the end of the first record */ } f = ((fmtinfo.first == FR_ATTS) /* build read flags */ ? AS_ATT : AS_DFLT)|AS_MARKED|AS_NOXATT; if (as_read(attset, file, f) != 0) { fclose(file); /* read the first record */ return as_err(attset)->code;/* of the data file */ } /* (attribute names/first tuple) */ if (table) /* if a table exists, delete it, */ tab_delete(table, 0); /* then create a new table */ table = tab_create("table", attset, tpl_delete); if (!table) { fclose(file); return E_NOMEM; } r = (f & AS_ATT) ? as_read(attset, file, AS_INST) : 0; if (f & AS_ATT) recno++; /* read the first data record */ while (r == 0) { /* record read loop */ if (tab_tpladd(table, NULL) != 0) { r = E_NOMEM; break; } /* add read tuple to the table */ recno++; /* count the processed record */ r = as_read(attset, file, AS_INST); } /* read the next tuple */ fclose(file); /* close the input file and */ if (r < 0) { /* check for a read error */ tab_delete(table, 0); table = NULL; return r; } return 0; /* return `ok' */} /* bc_data() *//*--------------------------------------------------------------------*/int bc_bvnorm (void){ /* --- init. normal distributions */ int i, k; /* loop variables */ int x, y; /* attribute identifiers */ int clscnt; /* number of classes */ MVNORM *mvn; /* to traverse the normal distribs. */ double *ctr; /* to traverse the cluster centers */ MATRIX *cov; /* to traverse the covariance mats. */ double t0, t1, t2; /* temporary buffers */ x = attmap[attinfo.h_att]; /* get the attribute identifiers */ y = attmap[attinfo.v_att]; /* of the selected attributes */ if (nbc) { /* if naive Bayes classifier, */ clscnt = nbc_clscnt(nbc); /* get the number of classes */ for (i = k = 0; i < clscnt; i++) { k |= bvn_init(bvnorm +i, nbc_prior(nbc, i), nbc_exp(nbc, i, x), nbc_exp(nbc, i, y), nbc_var(nbc, i, x), nbc_var(nbc, i, y), 0); } } /* create bivariate normal dists. */ else if (fbc) { /* if full Bayes classifier, */ clscnt = fbc_clscnt(fbc); /* get the number of classes */ for (i = k = 0; i < clscnt; i++) { mvn = fbc_mvnorm(fbc, i); /* traverse the normal distributions */ k |= bvn_init(bvnorm +i, fbc_prior(fbc, i), mvn_exp(mvn, x), mvn_exp(mvn, y), mvn_var(mvn, x), mvn_var(mvn, y), (x != y) ? mvn_cov(mvn, x, y) : 0); } } /* create bivariate normal dists. */ else { /* if cluster set */ x = am_off(cls_attmap(clset), x); /* get the rows/columns */ y = am_off(cls_attmap(clset), y); /* associated with the atts. */ clscnt = cls_clscnt(clset); /* get the number of classes */ for (i = k = 0; i < clscnt; i++) { ctr = cls_center(clset, i); /* traverse the clusters */ cov = cls_covmat(clset, i); /* and get their parameters */ if (!cov) { /* if only isotropic variance */ t0 = t1 = cls_isovar(clset, i); t2 = 0; } else { /* if direction dependent variance */ t0 = mat_get(cov, x, x); t1 = mat_get(cov, y, y); t2 = (x != y) ? mat_get(cov, x, y) : 0; } /* get variances and covariance */ k |= bvn_init(bvnorm +i, cls_weight(clset, i), ctr[x], ctr[y], t0, t1, t2); } /* create a bivariate normal dist. */ } /* for each of the clusters */ return (k != 0) ? -1 : clscnt;/* return the initialization status */} /* bc_bvnorm() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -