實驗三-決策樹算法實驗實驗報告.doc_第1頁
實驗三-決策樹算法實驗實驗報告.doc_第2頁
實驗三-決策樹算法實驗實驗報告.doc_第3頁
實驗三-決策樹算法實驗實驗報告.doc_第4頁
實驗三-決策樹算法實驗實驗報告.doc_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

實驗三 決策樹算法實驗一、實驗目的: 熟悉和掌握決策樹的分類原理、實質和過程;掌握典型的學習算法 和實現(xiàn)技術。二、實驗原理: 決策樹學習和分類. 三、實驗條件:四、實驗內容:1 根據(jù)現(xiàn)實生活中的原型自己創(chuàng)建一個簡單的決策樹。2 要求用這個決策樹能解決實際分類決策問題。五、實驗步驟:1、驗證性實驗:(1)算法偽代碼 算法Decision_Tree(data,AttributeName) 輸入由離散值屬性描述的訓練樣本集data; 候選屬性集合AttributeName。 輸出一棵決策樹。 (1) 創(chuàng)建節(jié)點N; (2) If samples 都在同一類C中then (3) 返回N作為葉節(jié)點,以類C標記; (4) If attribute_list為空then (5) 返回N作為葉節(jié)點,以samples 中最普遍的類標記;/多數(shù)表決 (6) 選擇attribute_list 中具有最高信息增益的屬性test_attribute; (7) 以test_attribute 標記節(jié)點N; (8) For each test_attribute 的已知值v /劃分 samples ;(9) 由節(jié)點N分出一個對應test_attribute=v的分支; (10令Sv為 samples中 test_attribute=v 的樣本集合;/一個劃分塊 (11)If Sv為空 then (12)加上一個葉節(jié)點,以samples中最普遍的類標記; (13)Else 加入一個由Decision_Tree(Sv,attribute_list-test_attribute)返回節(jié)點值。(2)實驗數(shù)據(jù)預處理Age:30歲以下標記為“1”;30歲以上50歲以下標記為“2”;50歲以上標記為“3”。Sex:FEMAL-“1”;MALE-“2”Region:INNERCITY-“1”;TOWN-“2”;RURAL-“3”;SUBURBAN-“4”Income:50002萬-“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 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 2 2 1 1 3 1 3 3 2 2 1 1 2(3)Matlab語句:Tree RulesMatrix= DecisionTree(DataSet, AttributName);六、實驗結果:實驗程序:function Tree RulesMatrix=DecisionTree(DataSet,AttributName)%輸入為訓練集,為離散后的數(shù)字,如記錄1:1 1 3 2 1;%前面為屬性列,最后一列為類標if nargin1 error(請輸入數(shù)據(jù)集);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=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; end end Tree.Child(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(DataSet,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=classnum(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) %防止參數(shù)下標越界 clnum(DataSet(i,attribut)=0; valnum=DataSet(i,attribut); Valueexamnum(DataSet(i,attribut)=0; RecordVal(DataSet(i,attribut).matrix=; %將編號保留下來,以方便后面按值分割數(shù)據(jù)集 end if(DataSet(i,size(DataSet,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(DataSet(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 Entropy=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=blank |_ int2str(value) _; else blank=blank |_ value _; end end if(length(Tree.Child)=0) %非葉子節(jié)點 if isempty(AttributName) disp(blank Attribut int2str(Tree.Attribut); else disp(blank Attribut AttributNameTree.Attribut); end if isempty(AttributValue) for j=1:length(Tree.Child)-1 showTree(Tree.Child(j).root,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,AttributValue,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(AttributValue)Tree.Attribut); end end endfunction Rules=getRule(Tree) if(length(Tree.Child)=0) Rules=; for i=1:length(Tree.Child) content=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論