一、实验内容
已知:流感训练数据集,预定义两个类别;
求:用ID3算法建立流感的属性描述决策树
流感训练数据集
二、实验设计(原理分析及流程)
ID3算法的实现,第一步先从三个属性中计算出针对决策属性(患流感与否)的条件熵,找出最小值的一个,然后根据该节点的分类属性再进行分支,取其分类属性进行条件熵的计算,最后得出决策树。
三、代码:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#define arrlen 7
typedef struct Info
{
int num;// from 0 to 7
bool headache;// 1 means having
bool courbature;
int temperature; // 0: normal , 1: high , 2: very high
bool flu; // 1 means having
} info;
double Entropy012 ( const int * a, const int * b, int rownum )
{
int i;
double ty00, ty01, ty10, ty11, ty20, ty21, ty0, ty1, ty2, entropy, entropy0, entropy1, entropy2;
ty00 = ty01 = ty10 = ty11 = ty20 = ty21 = 0.0;
for ( i = 0; i < rownum; i++ )
{
if ( a[i] == 0 && b[i] == 0 )
ty00++;