ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:18.79KB ,
资源ID:3720305      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3720305.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(ID3算法源程序.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

ID3算法源程序.docx

1、ID3算法源程序ID3 算法的源程序(C语言) 2005-3-25PROTO.HENTROPYNEG negentropy ( REAL *, UINT, NODE*, UINT );void print_tree ( NODE* , CHAR* );void free_tree ( NODE* );NODE* ID3 ( MATRIX * , NODE* , UINT , UINT );void err_exit ( CHAR* , UINT );MATRIX *build_matrix ( UINT, UINT );void free_matrix ( MATRIX * );void re

2、ad_matrix ( CHAR *, MATRIX * );void file_size ( CHAR * , UINT * , UINT * );CHAR *read_tags ( CHAR * , UINT );void free_tags ( CHAR *, UINT);ID3.htypedef unsigned intUINT;typedef unsigned long ULONG;typedefchar CHAR;typedef unsigned char BOOL;typedef doubleREAL;typedef struct node UINT idx; /* ID cod

3、e for attribute */ REAL threshold; /* Numerical threshold for attribute test */ struct node *on; /* Address of on node */ struct node *off; /* Address of off node */ struct node *parent; /* Addess of parent node */ NODE;typedef struct ne_struct REAL ne;UINT status; NEGENTROPY;typedef struct matrix U

4、INT width; UINT height; REAL *data; MATRIX;enum UINT INACTIVE, OFF, ON ;#define LN_2 0.693147180559945309417#define entropy(x) (x 0 ? x * log(x) / LN_2 : 0.0)/* FILE: id3.c* Author: Andrew Colin* DISCLAIMER: No liability is assumed by the author for any use made* of this program.* DISTRIBUTION: Any

5、use may be made of this program, as long as the* clear acknowledgment is made to the author in code and runtime* executables*/#include #include #include #include #include #include #include #include #include id3.h#include proto.h/*-*/MATRIX *build_matrix (UINT width, UINT height)MATRIX *_matrix;UINT

6、i;_matrix = (MATRIX*) malloc (sizeof (MATRIX);if (!_matrix)err_exit (_FILE_, _LINE_);_matrix-width= width;_matrix-height = height;_matrix-data = (REAL*) malloc (height * sizeof (REAL*);if (_matrix-data = NULL)err_exit(_FILE_, _LINE_);for (i=0; idatai = (REAL*) malloc (width * sizeof(REAL);if (_matri

7、x-datai = NULL)err_exit(_FILE_, _LINE_);return _matrix;/*-*/* Standard error handler function*/void err_exit (CHAR* file, UINT line)printf(n Fatal error in file %s, line %u, file, line);exit(0);/*-*/void file_size (CHAR *file_name, UINT *width, UINT *height)/* Given the name of a file of numeric dat

8、a, this routine counts* the numbers of rows and columns. Its assumed that the number* of entries is the same in each row, and an error is flagged if this* is not the case.*/FILE *f;UINT buf_size = 0xFF, _width = 0;CHAR *buffer, *ptr;*width = *height = 0;buffer = (CHAR*) malloc (buf_size * sizeof (CH

9、AR);if (buffer = NULL)err_exit (_FILE_, _LINE_);/* Open price file - abort if filename invalid */f = fopen(file_name, r);if (f = NULL)printf(n File not found : %sn, file_name);err_exit (_FILE_, _LINE_);/* Get number of entries in first row */if (fgets(buffer, buf_size, f) != NULL)+*height;ptr = strt

10、ok (buffer, ,);while (ptr != NULL)+*width;ptr = strtok (NULL, ,);/* Count numbers of subsequent rows */while (!feof(f)if (fgets(buffer, buf_size, f) != NULL)if (strlen(buffer) strlen(n)/* if line is more than a NL char */+*height;_width = 0;ptr = strtok (buffer, ,);while (ptr != NULL)+_width;ptr = s

11、trtok (NULL, ,);if (*width != _width)printf(n Number of entries in file %s did not agree, file_name);err_exit (_FILE_, _LINE_);free (buffer);/*-*/void free_matrix (MATRIX *_matrix)UINT i;for (i=0; iheight; i+)free (_matrix-datai);free (_matrix-data);free (_matrix);/*-*/void free_tags ( CHAR* varname

12、, UINT width)UINT i;for (i=0; ion);free_tree (node-off);free(node);/*-*/NODE* ID3 ( MATRIX *matrix, NODE* parent, UINT target, UINT state)/* Routine to build a decision tree, based on Quinlans ID3 algorithm. */NEGENTROPY negentropy_struct;NODE *node;UINT n_vars = matrix-width, n_samples = matrix-hei

13、ght, i, j, split;REAL *data = matrix-data;REAL best_threshold, min_negentropy, _negentropy;/* Allocate memory for this node */node = (NODE*) malloc (sizeof(NODE);if (!node)err_exit (_FILE_, _LINE_);/* Set up links in decision tree */node-parent = parent;/* Set address of parent node */if (parent !=

14、NULL) /* parent to child; not relevant for root node */* Pass address of this node to the parent node */if (state = ON)parent-on = node;elseif (state = OFF)parent-off = node;/* * Select attribute with lowest negentropy for splitting. Scan through * ALL attributes (except the target) and ALL data sam

15、ples. This is * pretty inefficient for data sets with repeated values, but will do * for illustrative purposes */min_negentropy = 1.0;for (i=0; in_vars; i+)for (j=0; jidx = i;node-threshold = dataji;/* .and calculate the negentropy of this partition */negentropy_struct = negentropy (data, n_samples,

16、 node, target);_negentropy = negentropy_struct.ne;/* If this negentropy is lower than any other, retain the index and threshold for future use */if (_negentropy min_negentropy) min_negentropy = _negentropy;split = i;best_threshold = dataji; /*if (i != target)*/ /*for (j=0; jn_samples; j+)*/ /*for (i

17、=0; iidx = split;node-threshold = best_threshold;/* * If the negentropy routine found itself at an end-of-branch * for the decision tree, the status flag in negentropy_struct * is set to ON or OFF and the node labelled accordingly. Otherwise, * ID3 continues to call itself until all end-of-branch no

18、des are * found. */if(negentropy_struct.status != INACTIVE) node-on = node-off = NULL;node-idx = negentropy_struct.status;elsenode-on= ID3 (matrix, node, target, ON);node-off = ID3 (matrix, node, target, OFF);return node;/*-*/void main (int argv, char *argc)MATRIX *matrix;NODE *node;UINT target, n_v

19、ars, n_samples;CHAR data_file13, tag_file13;/* Longest file name in DOS */CHAR *tag_names;/* Set up file names */if (argv != 2)printf(nUsage: id3 datafile);exit(0);elseprintf(nWelcome to ID3);printf(nLast compiled on %s, %s, _DATE_, _TIME_);printf(n);strcpy(data_file, argc1);strcpy(tag_file,argc1);s

20、trcat(data_file, .dat);strcat(tag_file,.tag);/* Read dimensions of data file */file_size (data_file, &n_vars, &n_samples);/* Read labels for columns of data */tag_names = read_tags (tag_file, n_vars);/* Allocate storage for data. */matrix = build_matrix (n_vars, n_samples);/* .and read it from disk

21、*/read_matrix (data_file, matrix);/* Classification target is last column */target = n_vars - 1;/* Return root of decision tree - ID3 continues to call itself recursively */node = ID3 ( matrix, NULL, target, 0 );print_tree(node, tag_names);printf(n);free_tags (tag_names, n_vars);free_matrix(matrix);free_tree (node);/*-*/NEGENTROPY negentropy ( REAL *data,UINT n_samples,NODE *local,UINT target)/* * Calculates the entropy of classif

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1