数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx

上传人:scccc 文档编号:11228988 上传时间:2021-07-15 格式:DOCX 页数:9 大小:52.65KB
返回 下载 相关 举报
数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx_第1页
第1页 / 共9页
数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx_第2页
第2页 / 共9页
数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx_第3页
第3页 / 共9页
数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx_第4页
第4页 / 共9页
数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx》由会员分享,可在线阅读,更多相关《数据仓库与数据挖掘-实验三 决策树算法实验报告范文.docx(9页珍藏版)》请在三一文库上搜索。

1、20123100160 曾志伟 通信7班实验三 决策树算法实验一、实验目的: 熟悉和掌握决策树的分类原理、实质和过程;掌握典型的学习算法 和实现技术。二、实验原理: 决策树学习和分类. 三、实验条件:四、实验内容:1 根据现实生活中的原型自己创建一个简单的决策树。2 要求用这个决策树能解决实际分类决策问题。四、实验内容:1 根据现实生活中的原型自己创建一个简单的决策树。2 要求用这个决策树能解决实际分类决策问题。五、实验步骤:1、验证性实验:(1)算法伪代码 算法Decision_Tree(data,AttributeName) 输入由离散值属性描述的训练样本集data; 候选属性集合Attr

2、ibuteName。 输出一棵决策树。 (1) 创建节点N; (2) If samples 都在同一类C中then (3) 返回N作为叶节点,以类C标记; (4) If attribute_list为空then (5) 返回N作为叶节点,以samples 中最普遍的类标记;/多数表决 (6) 选择attribute_list 中具有最高信息增益的属性test_attribute; (7) 以test_attribute 标记节点N; (8) For each test_attribute 的已知值v /划分 samples ;(9) 由节点N分出一个对应test_attribute=v的分支;

3、 (10令Sv为 samples中 test_attribute=v 的样本集合;/一个划分块 (11)If Sv为空 then (12)加上一个叶节点,以samples中最普遍的类标记; (13)Else 加入一个由Decision_Tree(Sv,attribute_list-test_attribute)返回节点值。(2)实验数据预处理Age:30岁以下标记为“1”;30岁以上50岁以下标记为“2”;50岁以上标记为“3”。Sex:FEMAL-“1”;MALE-“2”Region:INNERCITY-“1”;TOWN-“2”;RURAL-“3”;SUBURBAN-“4”Income:50

4、002万-“1”;2万4万-“2”;4万以上-“3”MarriedChildrenCarMortgagePep:以上五个条件,若为“是”标记为“1”,若为“否”标记为“2”。Age sex region income married children car mortgage pep 1 2 1 1 2 1 1 2 21 2 1 1 2 2 2 2 12 1 4 1 2 1 2 2 1 2 1 1 1 1 2 2 2 21 2 1 1 1 2 2 2 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 1 2 2 1 1 1 2 1 1 2 1 2 1 3 1 2 2 1 2

5、1 2 1 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 1 2 2 1 1 2 1 1 2 2 1 2 1 2 2 1 2 1 1 1 2 1 2 2 2 1 3 2 1 2 1 1 1 2 2 1 1 1 2 1 1 1 2 1 1 1 3 2 2 2 1 2 1 3 1 2 2 1 2 2 2 1 3 2 3 3 1 1 1 2 1 3 2 2 3 1 2 1 1 2 3 1 3 3 1 1 2 2 1 3 2 1 3 1 2 1 2 2 3 2 1 3 1 1 1 1 1 3 1 1 3 1 2 1 1 2 3 1 3 3 1 2 2 2 2 3 2 4 3 1

6、2 2 1 1 3 1 3 3 2 2 1 1 2(3)Matlab语句:Tree RulesMatrix= DecisionTree(DataSet, AttributName);2、设计性实验:(1).进入创建新图工作窗。(2).设置决策树分类算法和相应地环境参数。(3) 输入训练数据集。(4) 训练决策树以得出能够对新输入数据正确分类的模型。(5) 观察实际分类效果,记录,修改、调试再实验,直到满意。实验结果如下:建立决策树函数,程序如下:function Tree RulesMatrix=DecisionTree(DataSet, AttributName)%?1?1 1 3 2 1?

7、%?if nargin1 error(?);else if isstr(DataSet) DataSet AttributValue=readdata2(DataSet); else AttributValue=; endendif narginmostlabelnum) mostlabelnum=length(ValRecords(i).matrix); mostlabel=i; end end Tree.Attribut=mostlabel; Tree.Child=; return; end for i=1:length(Attributs) Sa(i) ValRecord=ComputE

8、ntropy(DataSet,i); Gains(i)=S-Sa(i); AtrributMatric(i).val=ValRecord; end maxval maxindex=max(Gains); Tree.Attribut=Attributs(maxindex); Attributs2=Attributs(1:maxindex-1) Attributs(maxindex+1:length(Attributs); for j=1:length(AtrributMatric(maxindex).val) DataSet2=DataSet(AtrributMatric(maxindex).v

9、al(j).matrix,1:maxindex-1) DataSet(AtrributMatric(maxindex).val(j).matrix,maxindex+1:size(DataSet,2); if(size(DataSet2,1)=0) mostlabelnum=0; mostlabel=0; for i=1:length(ValRecords) if(length(ValRecords(i).matrix)mostlabelnum) mostlabelnum=length(ValRecords(i).matrix); mostlabel=i; end end Tree.Child

10、(j).root.Attribut=mostlabel; Tree.Child(j).root.Child=; else Tree.Child(j).root=CreatTree(DataSet2,Attributs2); end end endfunction Entropy RecordVal=ComputEntropy(DataSet,attribut) %? if(attribut=0) clnum=0; for i=1:size(DataSet,1) if(DataSet(i,size(DataSet,2)clnum) %? classnum(DataSet(i,size(DataS

11、et,2)=0; clnum=DataSet(i,size(DataSet,2); RecordVal(DataSet(i,size(DataSet,2).matrix=; end classnum(DataSet(i,size(DataSet,2)=classnum(DataSet(i,size(DataSet,2)+1; RecordVal(DataSet(i,size(DataSet,2).matrix=RecordVal(DataSet(i,size(DataSet,2).matrix i; end Entropy=0; for j=1:length(classnum) P=class

12、num(j)/size(DataSet,1); if(P=0) Entropy=Entropy+(-P)*log2(P); end end else valnum=0; for i=1:size(DataSet,1) if(DataSet(i,attribut)valnum) %? clnum(DataSet(i,attribut)=0; valnum=DataSet(i,attribut); Valueexamnum(DataSet(i,attribut)=0; RecordVal(DataSet(i,attribut).matrix=; %? end if(DataSet(i,size(D

13、ataSet,2)clnum(DataSet(i,attribut) %? Value(DataSet(i,attribut).classnum(DataSet(i,size(DataSet,2)=0; clnum(DataSet(i,attribut)=DataSet(i,size(DataSet,2); end Value(DataSet(i,attribut).classnum(DataSet(i,size(DataSet,2)= Value(DataSet(i,attribut).classnum(DataSet(i,size(DataSet,2)+1; Valueexamnum(Da

14、taSet(i,attribut)= Valueexamnum(DataSet(i,attribut)+1; RecordVal(DataSet(i,attribut).matrix=RecordVal(DataSet(i,attribut).matrix i; end Entropy=0; for j=1:valnum Entropys=0; for k=1:length(Value(j).classnum) P=Value(j).classnum(k)/Valueexamnum(j); if(P=0) Entropys=Entropys+(-P)*log2(P); end end Entr

15、opy=Entropy+(Valueexamnum(j)/size(DataSet,1)*Entropys; end endendfunction showTree(Tree,level,value,branch,AttributValue,AttributName) blank=; for i=1:level-1 if(branch(i)=1) blank=blank |; else blank=blank ; end end blank=blank ; if(level=0) blank= (The Root):; else if isempty(AttributValue) blank=

16、blank |_ int2str(value) _; else blank=blank |_ value _; end end if(length(Tree.Child)=0) %? if isempty(AttributName) disp(blank Attribut int2str(Tree.Attribut); else disp(blank Attribut AttributName(Tree.Attribut); end if isempty(AttributValue) for j=1:length(Tree.Child)-1 showTree(Tree.Child(j).roo

17、t,level+1,j,branch 1,AttributValue,AttributName); end showTree(Tree.Child(length(Tree.Child).root,level+1,length(Tree.Child),branch(1:length(branch)-1) 0 1,AttributValue,AttributName); else for j=1:length(Tree.Child)-1 showTree(Tree.Child(j).root,level+1,AttributValueTree.Attributj,branch 1,Attribut

18、Value,AttributName); end showTree(Tree.Child(length(Tree.Child).root,level+1,AttributValueTree.Attributlength(Tree.Child),branch(1:length(branch)-1) 0 1,AttributValue,AttributName); end else if isempty(AttributValue) disp(blank leaf int2str(Tree.Attribut); else disp(blank leaf AttributValuelength(At

19、tributValue)Tree.Attribut); end end endfunction Rules=getRule(Tree) if(length(Tree.Child)=0) Rules=; for i=1:length(Tree.Child) content=getRule(Tree.Child(i).root); %disp(content); %disp(num2str(Tree.Attribut) , num2str(i) ,); for j=1:size(content,1) rule=cell2struct(content(j,1),str); content(j,1)=

20、num2str(Tree.Attribut) , num2str(i) , rule.str; end Rules=Rules;content; end else Rules=C num2str(Tree.Attribut); end end主程序:function main DataSet=1 2 1 1 2 1 1 2 21 2 1 1 2 2 2 2 12 1 4 1 2 1 2 2 1 2 1 1 1 1 2 2 2 21 2 1 1 1 2 2 2 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 1 2 2 1 1 1 2 1 1 2 1 2 1 3 1 2 2

21、1 2 1 2 1 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 1 2 2 1 1 2 1 1 2 2 1 2 1 2 2 1 2 1 1 1 2 1 2 2 2 1 3 2 1 2 1 1 1 2 2 1 1 1 2 1 1 1 2 1 1 1 3 2 2 2 1 2 1 3 1 2 2 1 2 2 2 1 3 2 3 3 1 1 1 2 1 3 2 2 3 1 2 1 1 2 3 1 3 3 1 1 2 2 1 3 2 1 3 1 2 1 2 2 3 2 1 3 1 1 1 1 1 3 1 1 3 1 2 1 1 2 ; AttributName= 龄 性 区 入 婚 孩 车 贷 pep ; Tree RulesMatrix= DecisionTree(DataSet, AttributName)运行结果如下:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1