📄 classify.c
字号:
for (j = 0; j < i; j++) if (*stat_recs[i]->class == *stat_recs[j]->class) ERROR_EXIT("Duplicate classes in statistics file.") TRYALLOC(float *, num_clas, means, "mean-vector pointers.") TRYALLOC(float **, num_clas, invcovars, "inverse-covariance-matrix pointers.") TRYALLOC(long, num_clas, count, "count.") for (i = 0; i < num_clas; i++) { means[i] = stat_recs[i]->mean; invcovars[i] = stat_recs[i]->invcovar; count[i] = 0; }/* * interpret options & check consistency */ statsize = *(long *) get_genhd("statsize", stat_ih); if (!f_flag) statfield = get_genhd("statfield", stat_ih); DEBUG(1)(stderr, "statsize = %ld. statfield = \"%s\".\n", statsize, statfield); if (!d_flag) { feasize = get_fea_siz(statfield, infea_ih, (short *) NULL, (long **) NULL); if(!f_flag && !e_flag && feasize <= 0) d_flag = YES; } if (d_flag) /* will use derived-field machinery to get features */ { if ((fields = get_fea_deriv(statfield, stat_ih->variable.refhd)) == NULL) ERROR_EXIT1( "%s is not a derived field in reference header.", statfield);/*!*//* check sum of sizes of source fields against statsize */ if (debug_level >= 1) { Fprintf(stderr, "Using derived-field mechanism. Fields:\n"); for (i = 0; fields[i] != NULL; i++) Fprintf(stderr, "\t\"%s\"\n", fields[i]); } if (hist_name != NULL) { Fprintf(hist_strm, "Source fields of derived field %s:\n", statfield); for (i = 0; fields[i] != NULL; i++) Fprintf(hist_strm, "\t%s\n", fields[i]); } } else /* will get features from field named in statfield */ { if (feasize <= 0) ERROR_EXIT1("No field %s in input file.", statfield); if (e_flag) { for (i = 0; i < num_elems; i++) if (elements[i] >= feasize) ERROR_EXIT2("No element %ld in field %s.", elements[i], statfield); if (statsize != num_elems) ERROR_EXIT2("%ld input features--should be %ld.\n", num_elems, statsize); if (hist_name != NULL) Fprintf(hist_strm, "Using field %s[%s].\n", statfield, e_arg); } else { if (statsize != feasize) ERROR_EXIT2("%ld input features--should be %ld.\n", feasize, statsize); if (hist_name != NULL) Fprintf(hist_strm, "Using field %s.\n", statfield); } DEBUG(1)(stderr, "Not using derived-field mechanism.\n"); }/* * make and write output-file header */ outfea_oh = copy_header(infea_ih); if (add_comment(outfea_oh, cmd_line) == 0) Fprintf(stderr, "classify: Warning - no space for command line in comment.\n"); Strcpy(outfea_oh->common.prog, ProgName); Strcpy(outfea_oh->common.vers, Version); Strcpy(outfea_oh->common.progdate, Date); add_source_file(outfea_oh, stat_name, stat_ih); add_source_file(outfea_oh, infea_name, infea_ih); TRYALLOC(char *, num_clas+1, enums, "enums array for output header") for (i = 0; i < num_clas; i++) enums[i] = fea_decode(*stat_recs[i]->class, "class", stat_ih); enums[num_clas] = (char *) NULL; if (add_fea_fld(clasfield, 1L, 0, (long *) NULL, CODED, enums, outfea_oh) != 0) ERROR_EXIT1("Can't add field %s to output header", clasfield) if (add_fea_fld(postfield, (long) num_clas, 1, (long *) NULL, FLOAT, (char **) NULL, outfea_oh) != 0) ERROR_EXIT1("Can't add field %s to output header", postfield) write_header(outfea_oh, outfea_strm);/* * allocate data records and feature vector */ infea_rec = allo_fea_rec(infea_ih); outfea_rec = allo_fea_rec(outfea_oh); posteriors = (float *) get_fea_ptr(outfea_rec, postfield, outfea_oh); class = (short *) get_fea_ptr(outfea_rec, clasfield, outfea_oh); TRYALLOC(float, statsize, feavec, "feature vector")/* * main processing loop---classify records */ while (get_fea_rec(infea_rec, infea_ih, infea_strm) != EOF) {/* * assemble feature vector */ if (d_flag) { DEBUG(1)(stderr, "Calling get_deriv_vec.\n"); feavec = (float *) get_deriv_vec(fields, infea_rec, infea_ih, FLOAT, &length, (char *) feavec); if (debug_level >= 2) pr_farray("Feature vector", (int) statsize, feavec); } else { fea_ptr = get_fea_ptr(infea_rec, statfield, infea_ih); DEBUG(1)(stderr, "Getting features.\n"); switch(get_fea_type(statfield, infea_ih)) { case CHAR: { char *p = (char *) fea_ptr; for (i = 0; i < statsize; i++) feavec[i] = p[e_flag ? elements[i] : i]; } break; case SHORT: { short *p = (short *) fea_ptr; for (i = 0; i < statsize; i++) feavec[i] = p[e_flag ? elements[i] : i]; } break; case LONG: { long *p = (long *) fea_ptr; for (i = 0; i < statsize; i++) feavec[i] = p[e_flag ? elements[i] : i]; } break; case FLOAT: { float *p = (float *) fea_ptr; for (i = 0; i < statsize; i++) feavec[i] = p[e_flag ? elements[i] : i]; } break; case DOUBLE: { double *p = (double *) fea_ptr; for (i = 0; i < statsize; i++) feavec[i] = p[e_flag ? elements[i] : i]; } break; } if (debug_level >= 2) pr_farray("Feature vector", (int) statsize, feavec); }/* * classify */ /* The following works because the strings in enums are in the */ /* same order as the vectors and matrices in means and */ /* invcovars (that of the records in the statistics file). */ *class = classify(feavec, (int) statsize, num_clas, means, invcovars, (float *) NULL, posteriors); DEBUG(1)(stderr, "Class number %d.\n"); if (debug_level >= 2) pr_farray("Posteriors", num_clas, posteriors); count[*class]++;/* * copy data from input record */ if (infea_ih->common.tag) outfea_rec->tag = infea_rec->tag; copy_fea_rec(infea_rec, infea_ih, outfea_rec, outfea_oh, (char **) NULL, (short **) NULL); DEBUG(1)(stderr, "Copying data from input record.\n");/* * output record */ put_fea_rec(outfea_rec, outfea_oh, outfea_strm); }/* * end of main processing loop * * final history output---summary statistics */ if (hist_name != NULL) { Fprintf(hist_strm, "\n%-20s%10s%15s\n%-20s%10s%15s\n\n", "", "Number of", "Relative", "Class", "records", "frequency"); nrec = 0; for (i = 0; i < num_clas; i++) nrec += count[i]; for (i = 0; i < num_clas; i++) Fprintf(hist_strm, "%-20s%10ld%15g\n", enums[i], count[i], count[i] / (double) nrec); Fprintf(hist_strm, "%-20s%10s%15s\n%-20s%10ld%15g\n\n", "", "---------", "---------", "Total", nrec, 1.0); } exit(0); /*NOTREACHED*/ }/* * for debug printout of floating arrays */void pr_farray(text, n, arr) char *text; int n; float *arr;{ int i; Fprintf(stderr, "%s - %d points:\n", text, n); for (i = 0; i < n; i++) { Fprintf(stderr, "%f ", arr[i]); if (i%5 == 4 || i == n - 1) Fprintf(stderr, "\n"); }}intnum_enums(fld, hdr) char *fld; struct header *hdr;{ int i, n; char **p; i = lin_search2(hdr->hd.fea->names, fld); spsassert(i != -1, "num_enums: field not found."); p = hdr->hd.fea->enums[i]; n = 0; while (*p++ != NULL) n++; return n;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -