




已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)三 決策樹(shù)算法實(shí)驗(yàn)一、實(shí)驗(yàn)?zāi)康模?熟悉和掌握決策樹(shù)的分類原理、實(shí)質(zhì)和過(guò)程;掌握典型的學(xué)習(xí)算法 和實(shí)現(xiàn)技術(shù)。二、實(shí)驗(yàn)原理: 決策樹(shù)學(xué)習(xí)和分類. 三、實(shí)驗(yàn)條件:四、實(shí)驗(yàn)內(nèi)容:1 根據(jù)現(xiàn)實(shí)生活中的原型自己創(chuàng)建一個(gè)簡(jiǎn)單的決策樹(shù)。2 要求用這個(gè)決策樹(shù)能解決實(shí)際分類決策問(wèn)題。五、實(shí)驗(yàn)步驟:1、驗(yàn)證性實(shí)驗(yàn):(1)算法偽代碼 算法Decision_Tree(data,AttributeName) 輸入由離散值屬性描述的訓(xùn)練樣本集data; 候選屬性集合AttributeName。 輸出一棵決策樹(shù)。 (1) 創(chuàng)建節(jié)點(diǎn)N; (2) If samples 都在同一類C中then (3) 返回N作為葉節(jié)點(diǎn),以類C標(biāo)記; (4) If attribute_list為空then (5) 返回N作為葉節(jié)點(diǎn),以samples 中最普遍的類標(biāo)記;/多數(shù)表決 (6) 選擇attribute_list 中具有最高信息增益的屬性test_attribute; (7) 以test_attribute 標(biāo)記節(jié)點(diǎn)N; (8) For each test_attribute 的已知值v /劃分 samples ;(9) 由節(jié)點(diǎn)N分出一個(gè)對(duì)應(yīng)test_attribute=v的分支; (10令Sv為 samples中 test_attribute=v 的樣本集合;/一個(gè)劃分塊 (11)If Sv為空 then (12)加上一個(gè)葉節(jié)點(diǎn),以samples中最普遍的類標(biāo)記; (13)Else 加入一個(gè)由Decision_Tree(Sv,attribute_list-test_attribute)返回節(jié)點(diǎn)值。(2)實(shí)驗(yàn)數(shù)據(jù)預(yù)處理Age:30歲以下標(biāo)記為“1”;30歲以上50歲以下標(biāo)記為“2”;50歲以上標(biāo)記為“3”。Sex:FEMAL-“1”;MALE-“2”Region:INNERCITY-“1”;TOWN-“2”;RURAL-“3”;SUBURBAN-“4”Income:50002萬(wàn)-“1”;2萬(wàn)4萬(wàn)-“2”;4萬(wàn)以上-“3”MarriedChildrenCarMortgagePep:以上五個(gè)條件,若為“是”標(biāo)記為“1”,若為“否”標(biāo)記為“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語(yǔ)句:Tree RulesMatrix= DecisionTree(DataSet, AttributName);六、實(shí)驗(yàn)結(jié)果:實(shí)驗(yàn)程序:function Tree RulesMatrix=DecisionTree(DataSet,AttributName)%輸入為訓(xùn)練集,為離散后的數(shù)字,如記錄1:1 1 3 2 1;%前面為屬性列,最后一列為類標(biāo)if nargin1 error(請(qǐng)輸入數(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) %計(jì)算信息熵 if(attribut=0) clnum=0; for i=1:size(DataSet,1) if(DataSet(i,size(DataSet,2)clnum) %防止下標(biāo)越界 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ù)下標(biāo)越界 clnum(DataSet(i,attribut)=0; valnum=DataSet(i,attribut); Valueexamnum(DataSet(i,attribut)=0; RecordVal(DataSet(i,attribut).matrix=; %將編號(hào)保留下來(lái),以方便后面按值分割數(shù)據(jù)集 end if(DataSet(i,size(DataSet,2)clnum(DataSet(i,attribut) %防止下標(biāo)越界 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é)點(diǎn) 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 大學(xué)物理思維邏輯試題及答案
- 建筑施工安全課程設(shè)置試題及答案
- 家具行業(yè)設(shè)計(jì)理念與用戶體驗(yàn)的相互作用研究試題及答案
- 安全事故處理流程試題及答案
- 增強(qiáng)商務(wù)演示技能的試題及答案
- 小學(xué)教師應(yīng)如何利用網(wǎng)絡(luò)資源支持教育教學(xué)反思與改進(jìn)策略試題及答案
- 幼兒園數(shù)字與圖形探索樂(lè)趣題試題及答案
- 創(chuàng)意數(shù)學(xué)學(xué)問(wèn)試題及答案
- 一年級(jí)試卷及答案語(yǔ)文
- 大學(xué)化學(xué)課程中的跨學(xué)科融合試題及答案
- 房地產(chǎn)廣告效果的評(píng)測(cè)與分析
- 2025年北京市石景山區(qū)九年級(jí)初三一模語(yǔ)文試卷(含答案)
- 華大新高考聯(lián)盟2025屆高三4月教學(xué)質(zhì)量測(cè)評(píng)歷史+答案
- T-CASEI 015-2023叉車使用安全管理規(guī)范
- 首次透析患者健康教育
- 山洪災(zāi)害防御知識(shí)課件
- 血吸蟲(chóng)防急感課件
- 弱電基礎(chǔ)知識(shí)單選題100道及答案
- 殯葬法律法規(guī)試題及答案
- 帶貨主播職業(yè)發(fā)展路徑與技能提升指南
- 2024福建省能源石化集團(tuán)有限責(zé)任公司秋季社會(huì)招聘120人筆試參考題庫(kù)附帶答案詳解
評(píng)論
0/150
提交評(píng)論