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

📄 sample.c

📁 这是数据挖掘算法的cubist算法的具体实现.运行在dos下.
💻 C
字号:
/*************************************************************************//*									 *//*	Source code for use with Cubist Release 2.04			 *//*	--------------------------------------------			 *//*		   Copyright RuleQuest Research 2007			 *//*									 *//*	This code is provided "as is" without warranty of any kind,	 *//*	either express or implied.  All use is at your own risk.	 *//*									 *//*************************************************************************//*************************************************************************//*									 *//*	Sample program to demonstrate the use of Cubist models		 *//*	------------------------------------------------------		 *//*									 *//*	Compilation:							 *//*									 *//*	    Unix: use an ANSI C compiler such as gcc and include	 *//*		  the math library, e.g. gcc -O2 sample.c -lm		 *//*									 *//*	    Windows: compile as a console application with symbol	 *//*		  "WIN32" and "_CONSOLE" defined			 *//*									 *//*	The options for this program are:				 *//*									 *//*	    -f <filestem>   specify the application name		 *//*	    -p		    print the cubist model			 *//*	    -e		    show estimated error bounds for each case	 *//*									 *//*									 *//*	The program expects to find the following files:		 *//*									 *//*	    <filestem>.names (the application names file)		 *//*	    <filestem>.model (the model file generated by Cubist)	 *//*	    <filestem>.cases (with the same format as a .data file) 	 *//*	    <filestem>.data  (the training data, if the Cubist model	 *//*			      uses instances as well as rules)		 *//*									 *//*	Please note: the names file and data file (if required)		 *//*	must be exactly as they were when the model was generated.	 *//*									 *//*	For each case in <filestem.cases>, the program prints the	 *//*	target value and then the value predicted by the model.  If	 *//*	selected, error bounds of the form "+- E" are nominally set	 *//*	at 95%, so that the absolute error should be less than E for	 *//*	95% of cases.  These bounds are estimated heuristically, and	 *//*	a summary showing the actual percentage of cases within the	 *//*	printed bounds gives a more accurate assessment of the real	 *//*	percentage.							 *//*									 *//*	Revised March 2007						 *//*									 *//*************************************************************************/#include "defns.h"#include "global.c"#include "hooks.c"/*************************************************************************//*									 *//*	Main                                                             *//*									 *//*************************************************************************/int main(int Argc, char *Argv[])/*  ----  */{    FILE		*F;    Description		Case;    int			o, m, CaseNo=0, InBounds=0, Known=0;    float		Predicted, Given, Low, High;    extern String	OptArg, Option, PropVal;    RRuleSet		*CubistModel;    char		Msg[100];    Boolean		PreviewModel=false, ShowErrBounds=false;    /*  Process options  */    while ( (o = ProcessOption(Argc, Argv, "f+pe")) )    {	switch (o)	{	case 'f':   FileStem = OptArg;		    break;	case 'p':   PreviewModel = true;		    break;	case 'e':   ShowErrBounds = true;		    break;	case '?':   printf("    **Unrecognised option %s\n", Option);		    exit(1);	}    }    /*  Read information on attribute names and values  */    if ( ! (F = GetFile(".names", "r")) ) Error(0, Fn, "");    GetNames(F);    /*  Read the model file that defines the ruleset and sets values	for various global variables such as USEINSTANCES  */    CubistModel = GetCommittee(".model");    if ( PreviewModel )    {	/*  Display the rulesets  */	ForEach(m, 0, MEMBERS-1)	{	    if ( MEMBERS > 1 )	    {		sprintf(Msg, "Model %d:", m+1);	    }	    else	    {		sprintf(Msg, "Model:");	    }	    PrintRules(CubistModel[m], Msg);	}    }    if ( USEINSTANCES )    {	if ( ! (F = GetFile(".data", "r")) ) Error(0, Fn, "");	GetData(F, true, false);	/*  Prepare the file of instances and the kd-tree index  */	InitialiseInstances(CubistModel);	free(Item);	if ( PreviewModel ) printf("\nUsing instances (%d nearest neighbors)"				   " together with rules\n\n", NN);    }    else    if ( PreviewModel )    {	printf("\nUsing rules alone\n\n");    }    if ( ShowErrBounds )    {	/*  Estimate global error bounds for cases covered by no rules  */	High = 0.5 * (Ceiling + Floor + (Ceiling - Floor) / (1 + 2 * EXTRAP));	Low  = 0.5 * (Ceiling + Floor - (Ceiling - Floor) / (1 + 2 * EXTRAP));	GlobalErrLim = 0.95 * Max(GlobalMean - Low, High - GlobalMean);    }    /*  Now process the cases in file <filestem>.cases.	This has the same format as a .data file except that the	target value can be "?" to indicate that it is unknown.  */    printf("%-16s %15s %15s\n%32s %15s\n\n",	   "Case", "Given", "Predicted", "Value", "Value");    if ( ! (F = GetFile(".cases", "r")) ) Error(0, Fn, "");    while ( (Case = GetDescription(F, false)) )    {	ReplaceUnknowns(Case, Nil);	Given     = Class(Case);	Predicted = ( USEINSTANCES ? NNEstimate(CubistModel, Case, &GNNEnv) :				     PredictValue(CubistModel, Case) );	/*  Print either case label or number  */                          		if ( LabelAtt )                                         	{	    printf("%-15.15s ", (String) (IgnoredVals + SVal(Case,LabelAtt)));         	}	else	{                                                  	    printf("%4d\t\t", CaseNo+1);	}	if ( Class(Case) == UNKNOWN )	{	    printf(" %15s %15.*f",			"?", Precision+1, Predicted);	}	else	{	    printf(" %15.*f %15.*f",			Precision, Given, Precision+1, Predicted);	}	if ( ShowErrBounds )	{	    printf(" +- %.*f", Precision+1, ErrLim);	    if ( Class(Case) != UNKNOWN )	    {		Known++;		if ( fabs(Predicted - Given) <= ErrLim ) InBounds++;	    }	}	printf("\n");	FreeCase(Case);	CaseNo++;    }    if ( ShowErrBounds && Known > 0 )    {	printf("\n%d / %d (%g%%) cases within error bounds\n",	       InBounds, Known, rint((100.0 * InBounds) / Known));    }    /*  Close the case file and free allocated memory  */        fclose(F);        FreeCttee(CubistModel);    if ( USEINSTANCES )    {	FreeCases(Instance, MaxInstance);	FreeUnlessNil(Ref);	FreeUnlessNil(RSPredVal);	FreeUnlessNil(RSErrLim);	FreeUnlessNil(GNNEnv.AttMinD);	FreeIndex(KDTree);    }    FreeNamesData();    FreeUnlessNil(PropVal);    FreeUnlessNil(IgnoredVals);    return 0;}

⌨️ 快捷键说明

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