好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

实验三决策树算法实验实验报告.docx

18页
  • 卖家[上传人]:人***
  • 文档编号:397207032
  • 上传时间:2022-11-07
  • 文档格式:DOCX
  • 文档大小:22.76KB
  • / 18 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 实验三 决策树算法实验一、实验目的: 熟悉和掌握决策树的分类原理、实质和过程;掌握典型的学习算法 和实现技术二、实验原理: 决策树学习和分类.三、实验条件:四、实验容:1 根据现实生活中的原型自己创建一个简单的决策树2 要求用这个决策树能解决实际分类决策问题五、实验步骤:1、验证性实验:(1)算法伪代码算法Decision_Tree(data,AttributeName)输入由离散值属性描述的训练样本集data;候选属性集合AttributeName输出一棵决策树 (1) 创建节点 N;(2) If samples都在同一类C中then (3)返回N作为叶节点,以类C标记; ⑷ 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的分支;(10 令 Sv 为 samples 中 test_attribute=v 的样本集合;//划分块 (11) If Sv 为空 then(12) 加上一个叶节点,以samples中最普遍的类标记;(13) Else 加入一^ 由 Decision_Tree(Svattribute_list-test_attribute)返回节点值。

      2)实验数据预处理Age:30岁以下标记为“1” 30岁以上50岁以下标记为“2” 50岁以上标记为“3” Sex: FEMAL-…“1” MALE-…“2”Region: INNER CITY “1”; TOWN “2”; RURAL “3”; SUBURBAN “4” Income: 5000~2 万 “1”; 2 万~4 万 “2”; 4 万以上 “3” Married Children Car2 12 12 22 21 11 22 12 12 21 11 11 22 12 22 12 12 12 11 22 12 21 11 21 22 12 11 21 22 12 12 12 12 22 12 21 13 21 11 13 13 23 23 13 23 2MortgagePep:以上五个条件,若为“是”标记为个,若为“否”标记为“2”Age sex regi on in come married chi Id re n car mortgage pep112 112 2112 2 24 12 121112 21112 2112 122 112 1112 113 12 2 12 2 2 1 21 2 2 2 22 2 11212 12 212 12 212 11112 1113 2 2 2 12 2 12 23 3 1112 3 12 13 3 11213 12 113 111-总结资料-3 113 12 13 2 4 3 13 3 2 2 1(3) Matlab 语句:[Tree RulesMatrix]= DecisionTree(DataSet, AttributName)六、实验结果:-总结资料-The Decision Tree:(The Root) :Altribut regionAttribut named1 Attribut AgeAttribut sexI.亡 af ]Leaf 2Leaf 2Attribut childrenAttribut LncoiieLeaf LL\Leaf 2Leaf 1leaf 2Att ritut carAttribut Affeleaf 2Leaf 】Leaf 】Attribut inconeleaf 2Attribut narriedLeaf ]Leaf 2leaf 2Attribut nortgag^leaf 2Attribut carLeaf ]L 2 .Attribut childrenleaf ]Leaf 2leaf 1总结资料-Tree =Attribut:3Child:[1x4 st ruc+]RulesMatrix 二1L1010001121010002201010002301111001301211002301311001301012002101020102201020101001020201002100002002210001002220002002300002003000012003000121003001221003002222004000001实验程序:function [Tree RulesMatrix]=DecisionTree(DataSet,AttributName) %输入为训练集,为离散后的数字,如记录1:1 1 3 2 1;%前面为属性列,最后一列为类标if nargin<1error('请输入数据集');elseif isstr(DataSet)[DataSet AttributValue]=readdata2(DataSet);elseAttributValue=[];endendif nargin<2AttributName=[];endAttributs=[1:size(DataSet,2)-1];Tree=CreatTree(DataSet,Attributs);disp([char(13) 'The Decision Tree:']); showTree(Tree,0,0,1,AttributValue,AttributName);Rules=getRule(Tree);RulesMatrix=zeros(size(Rules,1),size(DataSet,2));for i=1:size(Rules,1)rule=cell2struct(Rules(i,1),{'str'}); rule=str2num([rule.str([1:(find(rule.str=='C')-1)]) rule.str((find(rule.str=='C')+1):length(rule.str))]); for j=1:(length(rule)-1)/2RulesMatrix(i,rule((j-1)*2+1))=rule(j*2);endRulesMatrix(i,size(DataSet,2))=rule(length(rule));endendfunction Tree=CreatTree(DataSet,Attributs) %决策树程序 输入为:数据集,属性名列表%disp(Attributs);[S ValRecords]=ComputEntropy(DataSet,0);if(S==0) %当样例全为一类时退出,返回叶子节点类标for i=1:length(ValRecords)if(length(ValRecords(i).matrix)==size(DataSet,1))break;endendTree.Attribut=i;Tree.Child=[];return;endif(length(Attributs)==0) %当条件属性个数为0 时返回占多数的类标 mostlabelnum=0;mostlabel=0;for i=1:length(ValRecords)if(length(ValRecords(i).matrix)>mostlabelnum) mostlabelnum=length(ValRecords(i).matrix); mostlabel=i;endendTree.Attribut=mostlabel;Tree.Child=[];return;endfor i=1:length(Attributs)[Sa(i) ValRecord]=ComputEntropy(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).val(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;endendTree.Child(j).root.Attribut=mostlabel;Tree.Child(j).root.Child=[];else Tree.Child(j).root=CreatTree(DataSet2,Attributs2);endendendfunction [Entropy RecordVal]=ComputEntropy(DataSet,attribut) %计算信息熵if(attribut==0)clnum=0;for i=1:size(DataSet,1)if(DataSet(i,size(DataSet,2))>clnum) %防止下。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.