西电数据挖掘决策树算法_第1页
西电数据挖掘决策树算法_第2页
西电数据挖掘决策树算法_第3页
西电数据挖掘决策树算法_第4页
西电数据挖掘决策树算法_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据挖掘算法实验报告1) 实验题目基于决策树的分类算法,属性的选择采用ID3或C4.5策略,采用如下的数 据建立分类决策树。ageincomestudentcredit ratingbuys computer=30highnofairno40mediumnofairves40lowyesrairyes4Qlowvesexcellentno31. .40lowvesexcellentves=30mediumnofairno40mediumyesfairyes40mediumnoexcelentno2)算法基本思想的描述ID3选择具有最高信息熵增益的属性作为分裂属性,基于这种原则我们 首先可以算出

2、初始集合的熵,然后分别求出以各个属性为分裂属性时的熵, 然后将通过上面得到的数据算出以各个属性为分裂属性时的信心增益,选择 具有最大的信息增益属性作为我们的分裂属性。3) 编程实现算法#include #include #include using namespace std;#define SIZE 14struct Data(char age10;char income10;char student10;char credit_rating20;char buys_computer10;;Data data SIZE=(=30,high,气o,fair,气o,40,medium,no,fa

3、ir,yes,40,low,yes,fair,yes,40,low,yes,excellent,no,31.40,low,yes,excellent,yes,=30,medium,no,fair,no,40,medium,yes,fair,yes,40,medium,no,excellent,no;double calculate(double a,double b);void origin_entropy(Data data,double &entropy);void age_entropy(Data data,double &entropy);void income_entropy(Dat

4、a data,double &entropy);void student_entropy(Data data,double &entropy);void credit_rating_entropy(Data data,double &entropy);int main()(double origin=0,age=0,student=0,credit_rating=0,income=0;origin_entropy(data,origin);age_entropy(data,age);student_entropy(data,student);income_entropy(data,income

5、);credit_rating_entropy(data,credit_rating);coutinfo(D)=originendl;cout用 age 作为分裂属性时:侦熵 info(age)(D)=agett信息增益为:origin-ageendl;cout用 income 作为分裂属性时:n熵 info(income)(D)=incomett 信 息 增 益 为:origin-incomeendl;cout用 student 作为分裂属性时:n熵 info(student)(D)=):studenttt 信 息 增 益 为:origin-studentendl;cout用 credit_

6、rating 作为分裂属性时:n 熵 info(credit_rating)(D)=credit_ratingt 信息增益 为:origin-credit_ratingendl;return 0;double calculate(double a,double b)(if(a=0)return 0;elsereturn (a/b)*log10(b/a)/log10(2);void origin_entropy(Data data,double &entropy)(int i;double yes=0, no=0;for( i=0;iSIZE;i+)(if(strcmp(datai.buys_c

7、omputer,yes)=0)yes+;elseno+;entropy二calculate(yes,SIZE)+calculate(no,SIZE);void age_entropy(Data data,double &entropy)(double youth3=0;double middle3=0;double old3 = 0;for (int i=0;iSIZE;i+)(if(strcmp(datai.age,=30)=0)(strcmp(datai.buys_computer,yes)=0)?youth0+:youth1+ ;youth2+;else if(strcmp(datai.

8、age,31.40)=0)(strcmp(datai.buys_computer,yes)=0)?middle0+:middle1 +;middle2+;else(strcmp(datai.buys_computer,yes)=0)?old0+:old1+;old 2+;entropy二youth2/SIZE*(calculate(youth0,youth2)+calculate( youth1,youth2)+middle2/SIZE*(calculate(middle0,middle2 )+calculate(middle1,middle2)+old2/SIZE*(calculate(ol

9、d0 ,old2)+calculate(old1,old2);void income_entropy(Data data,double &entropy)(double high3 = 0;double medium3=0;double low3 = 0;for (int i=0;iSIZE;i+)(if(strcmp(datai.income,high)=0)(strcmp(datai.buys_computer,yes)=0)?high0+:high1+;h igh2+;else if(strcmp(datai.income,medium)=0)(strcmp(datai.buys_com

10、puter,yes)=0)?medium0+:medium1 +;medium2+;else(strcmp(datai.buys_computer,yes)=0)?low0+:low1+;low 2+;entropy=high2/SIZE*(calculate(high0,high2)+calculate(hig h1,high2)+medium2/SIZE*(calculate(medium0,medium2)+ca lculate(medium1,medium2)+low2/SIZE*(calculate(low0,low 2)+calculate(low1,low2);void stud

11、ent_entropy(Data data,double &entropy)(double yes3 = 0;double no3 = 0;for (int i=0;iSIZE;i+)(if(strcmp(datai.student,yes)(strcmp(datai.buys_computer,yes)?yes0+:yes1+;yes2+7else(strcmp(datai.buys_computer,yes)?no0+:no1+;no2+;entropy=yes2/SIZE*(calculate(yes0,yes2)+calculate(yes1,yes2)+no2/SIZE*(calculate(no0,no2)+calculate(no1,no 2);void credit_rating_entropy(Data data,double &entropy)(double fair3 = 0;double excellent3=0;for (int i=0;iSIZE;i+)(if(strcmp(datai.credit_rating,fair)(strcmp(datai.buys_computer,yes)?fair0+:fair1+;fair2+;else(strcmp(datai.buys_computer,yes)?excellent0+:exce

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论