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

📄 c45.cpp

📁 计算机人工智能方面的决策树方法 c4.5
💻 CPP
字号:
/*************************************************************************/
/*                                                                       */
/*      Main routine, c4.5                                               */
/*      ------------------                                               */
/*                                                                       */
/*************************************************************************/

/*#include <stdlib.h>*/
#include "defns.h"
#include "c45types.h"
#include "extern.h"


    /*  External data, described in extern.i  */

int           MaxAtt, MaxClass, MaxDiscrVal = 2;

ItemNo          MaxItem;

Description     *Item;

DiscrValue      *MaxAttVal;

char            *SpecialStatus;

String          *ClassName,
		*AttName,
		**AttValName,
		FileName = "DF";

int           VERBOSITY = 0,  //bigger then more debug info printed
		TRIALS    = 10;

Boolean         GAINRATIO  = true,
		SUBSET     = false,
		BATCH      = true,
		UNSEENS    = false,
		PROBTHRESH = false;

ItemNo          MINOBJS   = 2,
		WINDOW    = 0,
		INCREMENT = 0;

double           CF = 0.25;

Tree            *Pruned;

Boolean         AllKnown = true;

FILE *fpScreen;

void main(int Argc, char *Argv[])
/*  ----  */
{
    int o;
    extern char *optarg;
    extern int optind;
    Boolean FirstTime=true;
    int Best, BestTree();
	
	/*
    clrscr();
	Edit By HGP (1999.3.15.20:01)
	*/
	if ((fpScreen=fopen("Screen.txt","wt"))==NULL)
	{
		printf("Can't open file Screen.txt for write!\n");
		return ;
	}

    PrintHeader("decision tree generator");

    /*  Process options  */

    while ( (o = getopt(Argc, Argv, "f:bupv:t:w:i:gsm:c:")) != EOF )
    {
	if ( FirstTime )
	{
	    fprintf(fpScreen,"\n    Options:\n");
		printf("\n    Options:\n");
	    FirstTime = false;
	}

	switch (o)
	{
	case 'f':   FileName = optarg;
		    fprintf(fpScreen,"\tFile stem <%s>\n", FileName);
			printf("\tFile stem <%s>\n", FileName);
		    break;
	case 'b':   BATCH = true;
		    fprintf(fpScreen,"\tWindowing disabled (now the default)\n");
			printf("\tWindowing disabled (now the default)\n");
		    break;
	case 'u':   UNSEENS = true;
		    fprintf(fpScreen,"\tTrees evaluated on unseen cases\n");
			printf("\tTrees evaluated on unseen cases\n");
		    break;
	case 'p':   PROBTHRESH = true;
		    fprintf(fpScreen,"\tProbability thresholds used\n");
			printf("\tProbability thresholds used\n");
		    break;
	case 'v':   VERBOSITY = atoi(optarg);
		    fprintf(fpScreen,"\tVerbosity level %d\n", VERBOSITY);
			printf("\tVerbosity level %d\n", VERBOSITY);
		    break;
	case 't':   TRIALS = atoi(optarg);
		    fprintf(fpScreen,"\tWindowing enabled with %d trials\n", TRIALS);
			printf("\tWindowing enabled with %d trials\n", TRIALS);
		    BATCH = false;
		    break;
	case 'w':   WINDOW = atoi(optarg);
		    fprintf(fpScreen,"\tInitial window size of %d items\n", WINDOW);
			printf("\tInitial window size of %d items\n", WINDOW);
		    BATCH = false;
		    break;
	case 'i':   INCREMENT = atoi(optarg);
		    fprintf(fpScreen,"\tMaximum window increment of %d items\n",
			   INCREMENT);
			printf("\tMaximum window increment of %d items\n",
			   INCREMENT);
		    BATCH = false;
		    break;
	case 'g':   GAINRATIO = false;
		    fprintf(fpScreen,"\tGain criterion used\n");
			printf("\tGain criterion used\n");
		    break;
	case 's':   SUBSET = true;
		    fprintf(fpScreen,"\tTests on discrete attribute groups\n");
			printf("\tTests on discrete attribute groups\n");
		    break;
	case 'm':   MINOBJS = atoi(optarg);
		    fprintf(fpScreen,"\tSensible test requires 2 branches with >=%d cases\n",
			    MINOBJS);
			printf("\tSensible test requires 2 branches with >=%d cases\n",
			    MINOBJS);
		    break;
	case 'c':   CF = atof(optarg);
		    fprintf(fpScreen,"\tPruning confidence level %g%%\n", CF);
			printf("\tPruning confidence level %g%%\n", CF);
		    CF /= 100;
		    break;
	case '?':   
			fprintf(fpScreen,"unrecognised option\n");
			printf("unrecognised option\n");
		    exit(1);
	}
    }

    /*  Initialise  */

    GetNames();
    GetData(".dat");
    fprintf(fpScreen,"\nRead %d cases (%d attributes) from %s.dat\n",
	   MaxItem+1, MaxAtt+1, FileName);

   printf("\nRead %d cases (%d attributes) from %s.dat\n",
	   MaxItem+1, MaxAtt+1, FileName);

    /*  Build decision trees  */

    if ( BATCH )
    {
	TRIALS = 1;
	OneTree();
	Best = 0;
    }
    else
    {
	Best = BestTree();
    }

    /*  Soften thresholds in best tree  */

    if ( PROBTHRESH )
    {
		fprintf(fpScreen,"Softening thresholds");
		printf("Softening thresholds");
		if ( ! BATCH ) 
		{
			fprintf(fpScreen," for best tree from trial %d", Best);
			printf(" for best tree from trial %d", Best);
			fprintf(fpScreen,"\n");
			printf("\n");
			SoftenThresh(Pruned[Best]);
			fprintf(fpScreen,"\n");
			printf("\n");
			PrintTree(Pruned[Best]);
		}
    }

    /*  Save best tree  */

    if ( BATCH || TRIALS == 1 )
    {
		fprintf(fpScreen,"\nTree saved\n");
		printf("\nTree saved\n");
    }
    else
    {
		fprintf(fpScreen,"\nBest tree from trial %d saved\n", Best);
		printf("\nBest tree from trial %d saved\n", Best);
    }
   
	SaveTree(Pruned[Best], ".tre");
	

    /*  Evaluation  */

    fprintf(fpScreen,"\n\nEvaluation on training data (%d items):\n", MaxItem+1);
	printf("\n\nEvaluation on training data (%d items):\n", MaxItem+1);
    Evaluate(false, Best);

    if ( UNSEENS )
    {   
		GetData(".tes");
		fprintf(fpScreen,"\nEvaluation on test data (%d items):\n", MaxItem+1);
		printf("\nEvaluation on test data (%d items):\n", MaxItem+1);
		Evaluate(true, Best);
    }
	fclose(fpScreen);
}

⌨️ 快捷键说明

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