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

📄 test-rules-main.c

📁 Ripper 分类算法
💻 C
字号:
/****************************************************************************** test-rules-main.c - test a ruleset of a dataset******************************************************************************/#include <stdio.h>#include <math.h>#include "ripper.h"#include "mdb.h"/*****************************************************************************/char *Program="test-rules";char *Help_str[] = {    "syntax: test-rules [options] stem",    "   test the ruleset stem.hyp on stem.test",    "   and change the counts associated with each rule",     "",    "options are:",    "  -v#: set trace level to #",    "  -s: read data from stdin (default: from stem.test)",    "  -u: output updated ruleset with new counts",    NULL};/*****************************************************************************/int main(argc,argv)int argc;char *argv[];{    char *stem;    concept_t *hyp;    int o,i,j;    FILE *fp;    BOOL use_stdin,update;    vec_t *data;     example_t *exi;    rule_t *rj;    BOOL covered;    double **mat;    double tot;    update = use_stdin = FALSE;    set_trace_level(SUMM);    while ((o=getopt(argc,argv,"v:stuh"))!=EOF) {	switch (o) {	  case 'v':	    set_trace_level(atoi(optarg)); 	    break;	  case 'u':	    update = TRUE;	    break;	  case 's':	    use_stdin = TRUE;	    break;	  case 'h':	  case '?':	  default: 	    give_help();	    if (o=='h') exit(0);	    else fatal("option not implemented");	}    }    if (optind<argc) {	stem = argv[optind++];    } else {	give_help();	fatal("no file stem specified");    }    if (optind<argc) {	warning("not all arguments were used: %s ...",argv[optind]);    }        /* load the hypothesis */    ld_names(add_ext(stem,".names"));    if ((fp=fopen(add_ext(stem,".hyp"),"r"))==NULL) {	fatal("can't open concept file for read");    }    hyp = ld_concept(fp);    fclose(fp);    trace (SUMM) {	printf("Initial hypothesis:\n");	for (i=0; i<vmax(hyp->rules); i++) {	    printf("%2d. ",i+1);	    print_rule(vref(rule_t,hyp->rules,i));	    printf("\n");	}	printf("%2d. ",vmax(hyp->rules)+1);	print_atom(hyp->def); 	printf(" :- true (%g/%g).\n",hyp->nposx,hyp->nnegx); /* wts */	fflush(stdout);    }     /* load the data */    data = use_stdin? ld_data(NULL) : ld_data(add_ext(stem,".test"));    if (data==NULL || vmax(data)==0) fatal("no data!");    /* allocate the confusion matrix */    mat = newmem(vmax(hyp->rules)+1,double *);    for (i=0; i<vmax(hyp->rules)+1; i++) {	mat[i] = newmem(vmax(Classes),double);	for (j=0; j<vmax(Classes); j++) mat[i][j] = 0.0;    }    /* fill up the confusion matrix */    for (i=0; i<vmax(data); i++) {	exi = vref(example_t,data,i);	covered = FALSE;	for (j=0; !covered && j<vmax(hyp->rules); j++) {	    rj = vref(rule_t,hyp->rules,j);	    if (form_covers(rj->antec,exi->inst)) {		covered = TRUE;		mat[j][exi->lab.nom->index] ++ ; /* could be wt */	    }	}	if (!covered) {	    /* update default rule counts */	    mat[vmax(hyp->rules)][exi->lab.nom->index] ++; /* could be wt */	}    }    /* print matrix */    trace (SUMM) {	/* header */	printf("\nrule %7.7s ","conseq");	for (i=0; i<vmax(Classes); i++) {	    printf("%7.7s ", vref(atom_t,Classes,i)->nom->name);	}	printf("%7.7s\n","total");	/* contents */	for (j=0; j<vmax(hyp->rules)+1; j++) {	    if (j<vmax(hyp->rules)) {		rj  = vref(rule_t,hyp->rules,j);		printf(" %3d %7.7s ",j+1,rj->conseq->name);	    } else {		printf(" %3d %7.7s ",j+1,hyp->def->nom->name);	    }	    tot = 0.0;	    for (i=0; i<vmax(Classes); i++) {		tot += mat[j][i]; 		printf("%7.0f ",mat[j][i]);	    }	    printf("%7.0f\n",tot);	}	fflush(stdout);    } /* trace SUMM */    if (update) {	if ((fp=fopen(add_ext(stem,".hyp"),"w"))==NULL) {	    fatal("can't open concept file for write");	}	/* compute new counts */	for (j=0; j<vmax(hyp->rules); j++) {	    rj = vref(rule_t,hyp->rules,j); 	    rj->nposx = rj->nnegx = 0;	    for (i=0; i<vmax(Classes); i++) {		if (i==rj->conseq->index) {		    rj->nposx += mat[j][i];		} else {		    rj->nnegx += mat[j][i];		}	    }	}		j = vmax(hyp->rules);	hyp->nposx = hyp->nnegx = 0;	for (i=0; i<vmax(Classes); i++) {	    if (i==hyp->def->nom->index) {		hyp->nposx += mat[j][i];	    } else {		hyp->nnegx += mat[j][i];	    }	}	trace (SUMM) {	    printf("\nUpdated hypothesis:\n");	    print_concept(hyp);	    fflush(stdout);	}	fshow_concept(fp,hyp);    } /* if update */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -