⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cli.c

📁 聚类算法全集以及内附数据集
💻 C
📖 第 1 页 / 共 3 页
字号:
  #ifdef MATVERSION  if ((k != 2) && (k != 3))     /* check the number */    error(E_ARGCNT);            /* of arguments */  #else  if (k != 3) error(E_ARGCNT);  /* check the number of arguments */  if (fn_hdr && (strcmp(fn_hdr, "-") == 0))    fn_hdr = "";                /* convert "-" to "" */  i = (!fn_dom || !*fn_dom) ? 1 : 0;  if  (!fn_pat || !*fn_pat) i++;  if  ( fn_hdr && !*fn_hdr) i++;/* check assignments of stdin: */  if (i > 1) error(E_STDIN);    /* stdin must not be used twice */  if (fn_hdr)                   /* set the header file flag */    flags = AS_ATT | (flags & ~AS_DFLT);  #endif  if (rfnps[0]  <= 0) error(E_RFNPAR, rfnps[0]);  if (rfnps[1]  <  0) error(E_RFNPAR, rfnps[1]);  if (radius    <= 0) error(E_RADIUS, radius);  inimode = code(initab, ininame);  /* code the initialization mode */  if (inimode   <  0) error(E_MODE,   ininame);  inimode |= method;                /* add unit centers/origin flags */  nrmmode = code(nrmtab, nrmname);  /* code the normalization mode */  if (nrmmode   <  0) error(E_MODE,   nrmname);  if (nrmps[0]  <= 0) error(E_NRMPAR, nrmps[0]);  if (nrmps[1]  <  0) error(E_NRMPAR, nrmps[1]);  i       = code(updtab, updname);  /* code the update method */  if (i         <  0) error(E_METHOD, updname);  method |= i;                      /* combine method and modifier */  i       = code(modtab, modname);  /* code the update modifier */  if (i         <  0) error(E_MODIFY, modname);  method |= i;                      /* combine method and modifier */  if (method & CLS_ORIGIN) conly = 0;  if (msexp     <  0) error(E_MSEXP,  msexp);  if (moment    <  0) error(E_MOMENT, moment);  if (minchg    <  0) error(E_UPDPAR, minchg);  if (maxchg    <= 0) error(E_UPDPAR, maxchg);  if (growth    <= 0) error(E_UPDPAR, growth);  if (shrink    <= 0) error(E_UPDPAR, shrink);  if (lrates[0] <  0) error(E_LRATE,  lrates[0]);  if (lrates[1] <  0) error(E_LRATE,  lrates[1]);  if (lrates[2] <  0) error(E_LRATE,  lrates[2]);  if (epochs    <  0) error(E_EPOCHS, epochs);  dseed(seed);                  /* init. the random number generator */  #ifdef MATVERSION  /* --- read cluster description --- */  if (k > 2) {                  /* if an input file is given */    if ((!fn_in || !*fn_in) && (!fn_pat || !*fn_pat))      error(E_STDIN);           /* check assignments of std. input */    scan = sc_create(fn_in);    /* create a scanner */    if (!scan) error((!fn_in || !*fn_in) ? E_NOMEM : E_FOPEN, fn_in);    fprintf(stderr, "\nreading %s ... ", sc_fname(scan));    if (sc_nexter(scan) < 0)    /* start scanning (get first token) */      error(E_PARSE, sc_fname(scan));    clset = cls_parse(scan);    /* parse the cluster descriptions */    if (!clset || !sc_eof(scan)) error(E_PARSE, sc_fname(scan));    sc_delete(scan); scan = NULL;    /* delete the scanner */    if (owrite) {               /* if to overwrite the parameters */      cls_type (clset, type, -1);      cls_radfn(clset, (gauss) ? rf_gauss : rf_cauchy, rfnps);      cls_norm (clset, nrmmode, nrmps, noise);    }                           /* set the cluster parameters */    attcnt = cls_incnt(clset);  /* get the number of attributes */    clscnt = cls_clscnt(clset); /* and the number of clusters */    fprintf(stderr, "[%d cluster(s)] done.", clscnt);  }                             /* print a success message */  #else  /* --- read attribute set (and cluster set) --- */  attset = as_create("domains", att_delete);  if (!attset) error(E_NOMEM);  /* create an attribute set */  scan = sc_create(fn_dom);     /* and a scanner */  if (!scan) error((!fn_dom || !*fn_dom) ? E_NOMEM : E_FOPEN, fn_dom);  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 */    error(E_PARSE, sc_fname(scan));  if (trgname) {                /* if a target attribute is given */    k = as_attid(attset, trgname);    if (k < 0) error(E_UNKTRG, trgname);    att_setmark(as_att(attset, k), -1);  }                             /* mark the target attribute */  if ((sc_token(scan) == T_ID)  /* if there is a cluster set */  &&  (strcmp(sc_value(scan), "clset")) == 0) {    clset = cls_parsex(scan, attset, 1); /* parse the cluster set */    if (!clset || !sc_eof(scan)) error(E_PARSE, sc_fname(scan));    fprintf(stderr, "[%d attribute(s), ", as_attcnt(attset));    fprintf(stderr, "%d cluster(s)] done.\n", cls_clscnt(clset));    if (owrite) {               /* if to overwrite the parameters */      cls_type (clset, type, -1);      cls_radfn(clset, (gauss) ? rf_gauss : rf_cauchy, rfnps);      cls_norm (clset, nrmmode, nrmps, noise);    } }                         /* set the cluster parameters */  else {                        /* if there is no cluster set */    if (!sc_eof(scan)) error(E_PARSE, sc_fname(scan));    fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(attset));  }                             /* print a success message */  sc_delete(scan); scan = NULL; /* delete the scanner */  #endif  #ifdef MATVERSION  /* --- read training patterns --- */  if (fn_pat && *fn_pat)        /* if a file name is given, */    in = fopen(fn_pat, "r");    /* open the file for reading */  else {                        /* if no file name is given, */    in = stdin; fn_pat = "<stdin>"; }      /* use std. input */  fprintf(stderr, "\nreading %s ... ", fn_pat);  if (!in) error(E_FOPEN, fn_pat);  tfscan = tfs_create();        /* create a table file scanner and */  if (!tfscan) error(E_NOMEM);  /* set the separator characters */  if (blanks)  tfs_chars(tfscan, TFS_BLANK,  blanks);  if (fldseps) tfs_chars(tfscan, TFS_FLDSEP, fldseps);  if (recseps) tfs_chars(tfscan, TFS_RECSEP, recseps);  matrix = mat_readx(tfscan, in, 0, attcnt);  if (!matrix) {                /* read the training patterns */    err = tfs_err(tfscan);      /* on error get the error info. */    error(err->code, fn_pat, err->rec, err->s, err->fld, err->exp);  }                             /* abort with an error message */  patcnt = mat_rowcnt(matrix);  /* get the number of data points */  attcnt = mat_colcnt(matrix);  /* and their dimensionality */  if (exclude >= attcnt)        /* check the number of columns */    error(E_FLDCNT, fn_pat, 1, attcnt, exclude +1);  if (exclude > 0) {            /* if not to use all columns */    submat = mat_create(patcnt, attcnt -= exclude);    if (!submat) error(E_NOMEM);/* create a new matrix */    mat_sub(submat, matrix, 0, 0);    mat_delete(matrix); matrix = submat;  }                             /* cut out columns to use */  if (tfs_delim(tfscan) != TFS_EOF)     /* check for end of file */    error(E_VALUE, fn_pat, patcnt+1, "\"\"", 1);  if (in != stdin) {            /* if not read from standard input, */    fclose(in); in = NULL; }    /* close the input file */  if (patcnt <= 0) error(E_PATCNT);  fprintf(stderr, "[%d pattern(s)] done.\n", patcnt);  #else  /* --- read table --- */  as_chars(attset, blanks, fldseps, recseps, "");  table = io_tabin(attset, fn_hdr, fn_pat, flags, "table", 1);  if (!table) error(1);         /* read the table file */  #endif  /* --- create cluster set --- */  fprintf(stderr, "inducing clusters ... ");  if (!clset) {                 /* if no cluster set was read */    #ifdef MATVERSION    clset = (CLSET*)cls_create(mat_colcnt(matrix), clscnt);    #else    clset = (CLSET*)cls_createx(attset, 1, clscnt);    #endif    if (!clset) error(E_NOMEM); /* create a new cluster set */    cls_type(clset, type, -1);  /* and set the cluster type */    #ifdef MATVERSION    for (i = patcnt; --i >= 0;) /* determine the ranges of values */      cls_reg(clset, mat_row(matrix, i), 1);    if (irnorm)                 /* if to normalize the data, */      cls_reg(clset, NULL, 0);  /* compute the scaling factors */    #else    for (i = tab_tplcnt(table); --i >= 0; ) /* determine the */      cls_regx(clset, tab_tpl(table, i));   /* ranges of values */    if (irnorm)                 /* if to normalize the input ranges, */      cls_regx(clset, NULL);    /* compute the scaling factors */    #endif    if ((inimode & CLS_MODE) != CLS_POINTS)      cls_init(clset, inimode, range, drand, NULL);    else {                      /* if to select random data points */      #ifdef MATVERSION      mat_shuffle(matrix, drand);  /* shuffle the data points */      for (i = clscnt; --i >= 0; ) {        cls_init(clset, inimode, range, drand,                 mat_row(matrix, i % patcnt));      }                         /* use the first clscnt tuples */      #else      tab_shuffle(table, 0, INT_MAX, drand);      k = tab_tplcnt(table);    /* shuffle the data tuples */      for (i = clscnt; --i >= 0; ) {        cls_valuex(clset, tab_tpl(table, i % k));        cls_init(clset, inimode, range, drand, NULL);      }                         /* use the first clscnt tuples */      #endif                    /* (i.e. their corresp. data vectors) */    }                           /* as the initial cluster centers */    cls_type (clset, CLS_CENTER, radius *radius);    cls_radfn(clset, (gauss) ? rf_gauss : rf_cauchy, rfnps);    cls_norm (clset, nrmmode, nrmps, noise);    owrite = -1;                /* set the cluster parameters */  }                             /* and prepare overwriting */  /* --- do clustering --- */  cls_method (clset, method);   /* set the parameter update method, */  cls_msexp  (clset, msexp);    /* the membership exponent, */  cls_lrate  (clset, lrates, decays); /* the learning rates, */  cls_moment (clset, moment);         /* the momentum coefficient, */  cls_factors(clset, growth, shrink); /* growth and shrink factors, */  cls_limits (clset, minchg, maxchg); /* min. and max. change, */  cls_regular(clset, regps);    /* and the regularization parameters */  if (update <= 0) shuffle = 0; /* suppress unnecessary shuffling */  k   = update;                 /* initialize the pattern counter */  chg = trmchg +trmchg;         /* and the change value */  for (n = 0; n < epochs; n++){ /* compute a maximum number of epochs */    if ((n & 0x00ff) == 0)      /* print the current number of epochs */      fprintf(stderr, "%8d\b\b\b\b\b\b\b\b", n);    if ((owrite < 0)            /* if the cluster set was created, */    &&  (n == conly))           /* set the real cluster type */      cls_type(clset, type,-1); /* after 'conly' epochs */    chg = 0;                    /* clear the maximal change */    #ifdef MATVERSION    if (shuffle || (n == 0))    /* shuffle the training patterns */      mat_shuffle(matrix, drand);    for (i = patcnt; --i >= 0;){/* traverse the training patterns */      cls_aggr(clset, mat_row(matrix, i), 1);      if ((update > 0) && (--k <= 0)) {        k = update; t = cls_update(clset, (n < conly));        if (t > chg) chg = t;   /* aggregate the data vectors, */      }                         /* update in regular intervals, */    }                           /* and determine the maximal change */    #else    if (shuffle || (n == 0))    /* shuffle the training patterns */      tab_shuffle(table, 0, INT_MAX, drand);    for (i = tab_tplcnt(table); --i >= 0; ) {      tpl = tab_tpl(table, i);  /* traverse the tuples and */      cls_valuex(clset, tpl);   /* set the corresponding data vector */      cls_aggr(clset, NULL, tpl_getwgt(tpl));       if ((update > 0) && (--k <= 0)) {        k = update; t = cls_update(clset, (n < conly));        if (t > chg) chg = t;   /* aggregate the data vectors, */      }                         /* update in regular intervals, */    }                           /* and determine the maximal change */    #endif    if (update <= 0)            /* if no number of patterns is given, */      chg = cls_update(clset, (n < conly));  /* update once per epoch */    if ((n > conly) && (chg <= trmchg))      break;                    /* check the termination criterion */  }                             /* print a success message */  fprintf(stderr, "[%d epoch(s)] done.\n", n);  /* --- write cluster set --- */  if (fn_out && *fn_out)        /* if an output file name is given, */    out = fopen(fn_out, "w");   /* open the output file */  else {                        /* if no output file name is given, */    out = stdout; fn_out = "<stdout>"; }    /* write to std. output */  fprintf(stderr, "writing %s ... ", fn_out);  if (!out) error(E_FOPEN, fn_out);  #ifndef MATVERSION  if (as_desc(attset, out, AS_TITLE|AS_IVALS, maxlen) != 0)    error(E_FWRITE, fn_out);    /* describe attribute domains */  fprintf(out, "\n");           /* leave one line empty */  #endif  if (cls_desc(clset, out, dmode, maxlen) != 0)    error(E_FWRITE, fn_out);    /* describe the cluster set */  if (out != stdout) {          /* if not written to standard output, */    i = fclose(out); out = NULL;/* close the output file */    if (i != 0) error(E_FWRITE, fn_out);  }                             /* check for a write error and */  fprintf(stderr, "done [%.2fs].\n", clock() /(double)CLOCKS_PER_SEC);  /* --- clean up --- */  #ifndef NDEBUG  #ifdef MATVERSION  mat_delete(matrix);           /* delete the training patterns */  tfs_delete(tfscan);           /* and the table file scanner */  #else  tab_delete(table, 1);         /* delete the training patterns */  #endif  cls_delete(clset);            /* delete the cluster set */  #endif  #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 + -