siftrules.c.5

来自「vc的源代码程序的基于最小特征向量的元素的集合方法的程序源代码」· 5 代码 · 共 793 行 · 第 1/2 页

5
793
字号
	  	else		{		    Wrong[r]++;		}	    }	}    }    BaseBits = CodeWeight * RuleBits + ExceptionBits(SumCover, FPos, FNeg);    /*  From the Right and Wrong of each rule, calculate its Value  */    Verbosity(1)    {        printf("\t");    	InCount = -1;    }    ForEach(r, 1, NRules)    {	if ( Rule[r].Rhs == FocusClass )	{	    if ( RuleIn[r] )	    {		NewBits = ExceptionBits(SumCover-Right[r]-Wrong[r],					FPos-Wrong[r], FNeg+Right[r]) +			  CodeWeight *			      (RuleBits - Rule[r].Bits + LogItemNo[Selected]);		Value[r] = NewBits - BaseBits;	    }	    else	    {		NewBits = ExceptionBits(SumCover+Right[r]+Wrong[r],					FPos+Wrong[r], FNeg-Right[r]) +			  CodeWeight *			      (RuleBits + Rule[r].Bits - LogItemNo[Selected+1]);		Value[r] = BaseBits - NewBits;	    }	    Verbosity(1)	    {	        if ( RuleIn[r] )	        {		    if ( ++InCount && ! (InCount % 3) ) printf("\n\t\t");		    printf("%d[%d|%d=%.1f]  ", r, Right[r], Wrong[r], Value[r]);	        }	    }	}    }    Verbosity(1)    {	printf("\n\t\t%d rules, %d firings: F+=%d, F-=%d, %.1f bits (rules=%.1f)\n",		Selected, SumCover, FPos, FNeg, BaseBits, RuleBits);    }    return BaseBits;}/*************************************************************************//*								         *//*  Add rule r to the set of included rules and increase the number of	 *//*  rules covering each of the items that fire the rule		         *//*								         *//*************************************************************************/    AddRule(r)/*  -------  */    RuleNo r;{    ItemNo i;    RuleIn[r] = true;    ForEach(i, 0, MaxItem)    {	if ( Match[r][i] )	{	    Covered[i]++;	}    }    Verbosity(1) printf("%5d+  %6.1f", r, Value[r]);}/*************************************************************************//*								         *//*  Delete rule r from the included rules and decrease the number of	 *//*  rules covering each of the items covered by the rule	         *//*								         *//*************************************************************************/    DeleteRule(r)/*  ----------  */    RuleNo r;{    ItemNo i;    RuleIn[r] = false;    ForEach(i, 0, MaxItem)    {	if ( Match[r][i] )	{	    Covered[i]--;	}    }    Verbosity(1) printf("%5d-  %6.1f", r, -Value[r]);}/*************************************************************************//*								         *//*  Make an index of included rules in RuleIndex.  Select first those    *//*  classes whose rules have the fewest false positives.  Within a	 *//*  class, put rules with higher accuracy ahead.			 *//*								         *//*************************************************************************/    MakeIndex()/*  ---------  */{    ClassNo c, BestC, Pass;    RuleNo r, BestR, NewNRules = 0;    ItemNo i;    Boolean *Included;    Included = (Boolean *) calloc(MaxClass+1, sizeof(Boolean));    RuleIndex = (RuleNo *) calloc(NRules+1, sizeof(RuleNo));    Verbosity(1) printf("\nFalsePos  Class\n");    ForEach(i, 0, MaxItem)    {	Covered[i] = 0;    }    /*  Select the best class to put next  */    ForEach(Pass, 0, MaxClass)    {	ForEach(c, 0, MaxClass)	{	    if ( Included[c] ) continue;	    FalsePos[c] = 0;	    ForEach(i, 0, MaxItem)	    {		if ( Covered[i] || Class(Item[i]) == c ) continue;		ForEach(r, 1, NRules)		{		    if ( Rule[r].Rhs == c && RuleIn[r] && Match[r][i] )		    {			FalsePos[c]++;			break;		    }		}	    }	}	BestC = -1;	ForEach(c, 0, MaxClass)	{	    if ( ! Included[c] &&	         ( BestC < 0 || FalsePos[c] < FalsePos[BestC] ) )	    {		BestC = c;	    }	}	Included[BestC] = true;	Verbosity(1)	    printf("%5d     %s\n", FalsePos[BestC], ClassName[BestC]);	/*  Now grab the rules for this class  */	do	{	    BestR = 0;	    /*  Find the best rule to put next  */	    ForEach(r, 1, NRules)	    {		if ( RuleIn[r] && Rule[r].Rhs == BestC &&		     ( ! BestR || Rule[r].Error < Rule[BestR].Error ) )		{		    BestR = r;		}	    }	    if ( BestR )	    {		RuleIndex[++NewNRules] = BestR;		RuleIn[BestR] = false;		ForEach(i, 0, MaxItem)		{		    Covered[i] |= Match[BestR][i];		}	    }	} while ( BestR );    }    NRules = NewNRules;    cfree(Included);}/*************************************************************************//*								         *//*  Find the default class as the one with most items not covered by	 *//*  any rule.  Resolve ties in favour of more frequent classes.		 *//*  (Note: Covered has been set by MakeIndex.)				 *//*								         *//*************************************************************************/    FindDefault()/*  -----------  */{    ClassNo c;    ItemNo i;    RuleNo r;    /*  Determine uncovered items  */    ForEach(c, 0, MaxClass)    {	NoRule[c] = 0;    }    ForEach(i, 0, MaxItem)    {	if ( ! Covered[i] )	{	    NoRule[Class(Item[i])]++;	}    }    Verbosity(1)    {	printf("\nItems: Uncovered   Class\n");	ForEach(c, 0, MaxClass)	{	    printf("%5d %7d      %s\n", ClassFreq[c], NoRule[c], ClassName[c]);	}	printf("\n");    }    DefaultClass = 0;    ForEach(c, 1, MaxClass)    {	if ( NoRule[c] > NoRule[DefaultClass] ||	     NoRule[c] == NoRule[DefaultClass] &&	     ClassFreq[c] > ClassFreq[DefaultClass] )	{	    DefaultClass = c;	}    }}/*************************************************************************//*								         *//*  Given a rule and a case (ClearOutcomes performed), determine	 *//*  the strength with which we can conclude that the case belongs	 *//*  to the class specified by the rule's right-hand side.	         *//*								         *//*  If the case doesn't satisfy all the conditions of the rule,		 *//*  then this is 0.						         *//*								         *//*************************************************************************/float Strength(ThisRule, Case)/*    --------  */    PR ThisRule;    Description Case;{    short d;    Boolean Satisfies();    if ( ThisRule.Error > 0.7 ) return 0.0;    ForEach(d, 1, ThisRule.Size)    {	if ( ! Satisfies(Case, ThisRule.Lhs[d]) )	{	    return 0.0;	}    }    return ( 1 - ThisRule.Error );}/*************************************************************************//*									 *//*  Determine the number of bits to encode exceptions: FP false +ves	 *//*  out of Fires firings, and FN false negatives in the remaining items	 *//*									 *//*************************************************************************/#define nCrBits(E,N)  (LogFact[N] - (LogFact[E] + LogFact[N-(E)]))float ExceptionBits(Fires, FP, FN)/*    -------------  */    int Fires, FP, FN;{    return nCrBits(FP, Fires) +  nCrBits(FN, MaxItem+1-Fires);}/*************************************************************************//*									 *//*  Find encoding lengths for all rules					 *//*									 *//*************************************************************************/    FindRuleCodes()/*  -------------  */{    RuleNo r;    short d, NCond;    float Bits, CondBits();    ForEach(r, 1, NRules)    {	NCond = Rule[r].Size;	Bits = 0;	ForEach(d, 1, NCond)	{	    Bits += CondBits(Rule[r].Lhs[d]);	}	/*  Must encode the number of conditions, but credit the total	    encoding by the ways conditions can be reordered  */	Rule[r].Bits = Bits + LogItemNo[NCond] - LogFact[NCond];    }}/*************************************************************************//*									 *//*  Determine the number of bits required to encode a condition		 *//*									 *//*************************************************************************/float CondBits(C)/*    --------  */    Condition C;{    Test t;    Attribute a;    t = C->CondTest;    a = t->Tested;    switch ( t->NodeType )    {	case BrDiscr:		/* test of discrete attribute */	case ThreshContin:	/* test of continuous attribute */	    return AttTestBits/REDUNDANCY + BranchBits[a];	case BrSubset:		/* subset test on discrete attribute  */	    return AttTestBits/REDUNDANCY + MaxAttVal[a];    } }

⌨️ 快捷键说明

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