




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗三 決策樹算法實驗一、實驗目的: 熟悉和掌握決策樹的分類原理、實質和過程;掌握典型的學習算法 和實現技術。二、實驗原理: 決策樹學習和分類. 三、實驗條件:四、實驗內容:1 根據現實生活中的原型自己創建一個簡單的決策樹。2 要求用這個決策樹能解決實際分類決策問題。五、實驗步驟:1、驗證性實驗:(1)算法偽代碼 算法Decision_Tree(data,AttributeName) 輸入由離散值屬性描述的訓練樣本集data; 候選屬性集合AttributeName。 輸出一棵決策樹。 (1) 創建節點N; (2) If samples 都在同一類C中then (3) 返回N作為葉節點,以類C
2、標記; (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的分支; (10令Sv為 samples中 test_attribute=v 的樣本集合;/一個劃分塊 (11)If Sv為空 then (12)加上一個葉
3、節點,以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:INNER CITY-“1”;TOWN-“2”; RURAL-“3”; SUBURBAN-“4” Income:50002萬-“1”;2萬4萬-“2”;4萬以上-“3”
4、Married Children Car Mortgage Pep:以上五個條件,若為“是”標記為“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
5、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
6、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)%輸入為訓練集,為離散后的數字,如記錄1:1 1 3 2 1;%前面為屬性列,最后一列為類標if nargin<1 error('請輸入數據集');else if isstr(DataSet) DataSet AttributValue=readdata2
7、(DataSet); else AttributValue=; endendif nargin<2 AttributName=;end Attributs=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);
8、 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)/2 RulesMatrix(i,rule(j-1)*2+1)=rule(j*2); end RulesMatrix(i,size(DataSet,2)=rule(length(rule); e
9、ndendfunction 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; end end Tree.Attribut=i; Tree.Child=; return; end if(length(Att
10、ributs)=0) %當條件屬性個數為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.Attribut=mostlabel; Tree.Child=; return; end for i=1:length(Attributs) Sa(i) ValRecord=ComputEn
11、tropy(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).va
12、l(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 e
13、nd 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) %防止下標越界 cla
14、ssnum(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=
15、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) %防止參數下標越界 clnum(DataSet(i,attribut)=0; valnum=DataSet(i,attribut); Valueexamnum(DataSet(i,attribut)=0; RecordVal(DataSet(i,attribut
16、).matrix=; %將編號保留下來,以方便后面按值分割數據集 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(
17、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)/Valu
18、eexamnum(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 en
19、d 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) %非葉子節點 if isempty(AttributName) disp(blank 'Attribut ' int2st
20、r(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.Attributlen
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論