文档介绍:数据挖掘算法实验报告
1) 实验题目
基于决策树的分类算法,,采用如下的数 据建立分类决策树。
age
income
student
credit rating
buys computer
income); credit_rating_entropy(data, credit_rating); cout〈<〃info (D) =/z<<origin<<endl;
C0Ut«/z用 age 作为分裂属性时:〃 <〈〃\n〃<<〃嫡 info (age) (D)=〃<〈age<<〃\t\t〃<<〃信息增益为:,,«origin-age«endl;
cout〈<〃用 income 作为分裂属性时:〃嫡 info(income)(D)二〃<<income<<〃\t\t〃<<〃 信 息 增 益 为:/z<<origin-income<<endl;
cout〈<〃用 student 作为分裂属性时:〃嫡 info (student) (D)=) :,,<<student<<,,\t\t,z<<,, 信 息 增 益 为://<<origin-student<<endl;
cout〈<〃 用 credit_rating 作为分裂属性时:〃〈〃嫡 info (credit_rating) (D) =,,<<credit_rating<<,,\tz/<<,, 信 息 增 益 为:/z<<origin-credit_rating<<endl;
return 0;
}
double calculate(double a, double b)
{
if (a==0)
return 0;
else
return (a/b)*loglO (b/a)/loglO (2);
}
void origin_entropy(Data data[], double &entropy)
(
int i;
double yes=0, no=0;
for ( i=0;i<SIZE;i++)
{
if (strcmp (data[i]. buys_computer, 〃yes〃)=0)
yes++;
else
no++;
}
entropy=calculate(yes, SIZE)+calculate(no, SIZE);
}
void age_entropy(Data data[], double feentropy)
(
double youth[3]={0};
double middle[3]={0};
double old[3] = {0};
for (int i=0;i<SIZE;i++)
{
if (strcmp (data[i]. age, 〃〈=30〃)=0)
(
(strcmp(data[i]. buys_computer, 〃yes〃)=0)?youth[0] ++:youth[1] ++ ;youth[2]++;
}
else if (strcmp (data[i]. age, zz31.. . 40〃)二二0)
(
(strcmp(data[i]. buys_computer, 〃yes〃)=0)?middle[0] ++:middle[1] ++