




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、機器學習課內實驗報告(1) ID算法實現決策樹2015 - 2016學年 第 2 學期專業:智能科學與技術班級:智能1301班學號:06133029姓名:張爭輝一、 實驗目的:理解ID3算法的基本原理,并且編程實現。二、 實驗要求:使用C/C+/MATLAB實現ID3算法。輸入:若干行,每行 5 個字符串,表示Outlook Temperature Humidity Wind Play ball如上表。輸出:決策樹。實驗結果如下:輸入: Sunny Hot High Weak No Sunny Hot High Strong No Overcast Hot High Weak Yes Rain
2、 Mild High Weak Yes Rain Cool Normal Weak Yes Rain Cool Normal Strong No Overcast Cool Normal Strong Yes Sunny Mild High Weak No Sunny Cool Normal Weak Yes Rain Mild Normal Weak Yes Sunny Mild Normal Strong Yes Overcast Mild High Strong Yes Overcast Hot Normal Weak Yes Rain Mild High Strong No輸出:Out
3、look Rain Wind Strong No Weak Yes Overcast Yes Sunny Humidity Normal Yes High No 三、 具體實現:實現算法如下:#include <iostream>#include <fstream>#include <math.h>#include <string>using namespace std;#define ROW 14#define COL 5#define log2 0.69314718055typedef struct TNode char data15; ch
4、ar weight15; TNode * firstchild,*nextsibling;*tree;typedef struct LNode char OutLook15; char Temperature15; char Humidity15; char Wind15; char PlayTennis5; LNode *next;*link;typedef struct AttrNode char attributes15;/屬性 int attr_Num;/屬性的個數 AttrNode *next;*Attributes;char * ExamplesROWCOL = /"Ov
5、erCast","Cool","High","Strong","No", /"Rain","Hot","Normal","Strong","Yes", "Sunny","Hot","High","Weak","No", "Sunny","Hot","High&
6、quot;,"Strong","No", "OverCast","Hot","High","Weak","Yes", "Rain","Mild","High","Weak","Yes", "Rain","Cool","Normal","Weak","Yes",
7、 "Rain","Cool","Normal","Strong","No", "OverCast","Cool","Normal","Strong","Yes", "Sunny","Mild","High","Weak","No", "Sunny","Cool"
8、;,"Normal","Weak","Yes", "Rain","Mild","Normal","Weak","Yes", "Sunny","Mild","Normal","Strong","Yes", "OverCast","Mild","Normal","Stron
9、g","Yes", "OverCast","Hot","Normal","Weak","Yes", "Rain","Mild","High","Strong","No" ;char * Attributes_kind4 = "OutLook","Temperature","Humidity","Wi
10、nd"int Attr_kind4 = 3,3,2,2;char * OutLook_kind3 = "Sunny","OverCast","Rain"char * Temperature_kind3 = "Hot","Mild","Cool"char * Humidity_kind2 = "High","Normal"char * Wind_kind2 = "Weak","Strong"
11、;/*int i_Exampple145 = 0,0,0,0,1, 0,0,0,1,1, 1,0,0,1,0, 2,1,0,0,0, 2,2,1,0,0, 2,2,1,1,1, 1,2,1,1,0, 0,1,0,0,1, 0,2,1,0,0, 2,1,1,0,0, 0,1,1,1,0, 1,1,1,1,0, 1,1,1,0,0, 2,1,0,0,1 ;*/void treelists(tree T);void InitAttr(Attributes &attr_link,char * Attributes_kind,int Attr_kind);void InitLink(link &
12、amp;L,char * ExamplesCOL);void ID3(tree &T,link L,link Target_Attr,Attributes attr);void PN_Num(link L,int &positve,int &negative);double Gain(int positive,int negative,char * atrribute,link L,Attributes attr_L);void main() link LL,p; Attributes attr_L,q; tree T; T = new TNode; T->fir
13、stchild = T->nextsibling = NULL; strcpy(T->weight,""); strcpy(T->data,""); attr_L = new AttrNode; attr_L->next = NULL; LL = new LNode; LL->next = NULL; /成功建立兩個鏈表 InitLink(LL,Examples); InitAttr(attr_L,Attributes_kind,Attr_kind); ID3(T,LL,NULL,attr_L); cout<<&
14、quot;決策樹以廣義表形式輸出如下:"<<endl; treelists(T);/以廣義表的形式輸出樹/cout<<Gain(9,5,"OutLook",LL,attr_L)<<endl; cout<<endl;/以廣義表的形式輸出樹void treelists(tree T) tree p; if(!T) return; cout<<""<<T->weight<<"" cout<<T->data; p = T-&g
15、t;firstchild; if (p) cout<<"(" while (p) treelists(p); p = p->nextsibling; if (p)cout<<',' cout<<")" void InitAttr(Attributes &attr_link,char * Attributes_kind,int Attr_kind) Attributes p; for (int i =0;i < 4;i+) p = new AttrNode; p->next =
16、NULL; strcpy(p->attributes,Attributes_kindi); p->attr_Num = Attr_kindi; p->next = attr_link->next; attr_link->next = p; void InitLink(link &LL,char * ExamplesCOL) link p; for (int i = 0;i < ROW;i+) p = new LNode; p->next = NULL; strcpy(p->OutLook,Examplesi0); strcpy(p->
17、;Temperature,Examplesi1); strcpy(p->Humidity,Examplesi2); strcpy(p->Wind,Examplesi3); strcpy(p->PlayTennis,Examplesi4); p->next = LL->next; LL->next = p; void PN_Num(link L,int &positve,int &negative) positve = 0; negative = 0; link p; p = L->next; while (p) if (strcmp(p
18、->PlayTennis,"No") = 0) negative+; else if(strcmp(p->PlayTennis,"Yes") = 0) positve+; p = p->next; /計算信息增益/link L: 樣本集合S/attr_L:屬性集合double Gain(int positive,int negative,char * atrribute,link L,Attributes attr_L) int atrr_kinds;/每個屬性中的值的個數 Attributes p = attr_L->next;
19、 link q = L->next; int attr_th = 0;/第幾個屬性 while (p) if (strcmp(p->attributes,atrribute) = 0) atrr_kinds = p->attr_Num; break; p = p->next; attr_th+; double entropy,gain=0; double p1 = 1.0*positive/(positive + negative); double p2 = 1.0*negative/(positive + negative); entropy = -p1*log(p1
20、)/log2 - p2*log(p2)/log2;/集合熵 gain = entropy; /獲取每個屬性值在訓練樣本中出現的個數 /獲取每個屬性值所對應的正例和反例的個數 /聲明一個3*atrr_kinds的數組 int * kinds= new int * 3; for (int j =0;j < 3;j+) kindsj = new intatrr_kinds;/保存每個屬性值在訓練樣本中出現的個數 /初始化 for (int j = 0;j< 3;j+) for (int i =0;i < atrr_kinds;i+) kindsji = 0; while (q) i
21、f (strcmp("OutLook",atrribute) = 0) for (int i = 0;i < atrr_kinds;i+) if(strcmp(q->OutLook,OutLook_kindi) = 0) kinds0i+; if(strcmp(q->PlayTennis,"Yes") = 0) kinds1i+; else kinds2i+; else if (strcmp("Temperature",atrribute) = 0) for (int i = 0;i < atrr_kinds;
22、i+) if(strcmp(q->Temperature,Temperature_kindi) = 0) kinds0i+; if(strcmp(q->PlayTennis,"Yes") = 0) kinds1i+; else kinds2i+; else if (strcmp("Humidity",atrribute) = 0) for (int i = 0;i < atrr_kinds;i+) if(strcmp(q->Humidity,Humidity_kindi) = 0) kinds0i+; if(strcmp(q-&g
23、t;PlayTennis,"Yes") = 0) kinds1i+;/ else kinds2i+; else if (strcmp("Wind",atrribute) = 0) for (int i = 0;i < atrr_kinds;i+) if(strcmp(q->Wind,Wind_kindi) = 0) kinds0i+; if(strcmp(q->PlayTennis,"Yes") = 0) kinds1i+; else kinds2i+; q = q->next; /計算信息增益 double
24、* gain_kind = new doubleatrr_kinds; int positive_kind = 0,negative_kind = 0; for (int j = 0;j < atrr_kinds;j+) if (kinds0j != 0 && kinds1j != 0 && kinds2j != 0) p1 = 1.0*kinds1j/kinds0j; p2 = 1.0*kinds2j/kinds0j; gain_kindj = -p1*log(p1)/log2-p2*log(p2)/log2; gain = gain - (1.0*ki
25、nds0j/(positive + negative)*gain_kindj; else gain_kindj = 0; return gain;/在ID3算法中的訓練樣本子集合與屬性子集合的鏈表需要進行清空void FreeLink(link &Link) link p,q; p = Link->next; Link->next = NULL; while (p) q = p; p = p->next; free(q); void ID3(tree &T,link L,link Target_Attr,Attributes attr) Attributes
26、p,max,attr_child,p1; link q,link_child,q1; tree r,tree_p; int positive =0,negative =0; PN_Num(L,positive,negative); /初始化兩個子集合 attr_child = new AttrNode; attr_child->next = NULL; link_child = new LNode; link_child->next = NULL; if (positive = 0)/全是反例 strcpy(T->data,"No"); return; e
27、lse if( negative = 0)/全是正例 strcpy(T->data,"Yes"); return; p = attr->next; /屬性鏈表 double gain,g = 0; /*/ /* 建立屬性子集合與訓練樣本子集合有兩個方案: 一:在原來鏈表的基礎上進行刪除; 二:另外申請空間進行存儲子集合; 采用第二種方法雖然浪費了空間,但也省了很多事情,避免了變量之間的應用混亂 */ /*/ if(p) while (p) gain = Gain(positive,negative,p->attributes,L,attr); cout&l
28、t;<p->attributes<<" "<<gain<<endl; if(gain > g) g = gain; max = p;/尋找信息增益最大的屬性 p = p->next; strcpy(T->data,max->attributes);/增加決策樹的節點 cout<<"信息增益最大的屬性:max->attributes = "<<max->attributes<<endl<<endl; /下面開始建立決策樹 /創
29、建屬性子集合 p = attr->next; while (p) if (strcmp(p->attributes,max->attributes) != 0) p1 = new AttrNode; strcpy(p1->attributes,p->attributes); p1->attr_Num = p->attr_Num; p1->next = NULL; p1->next = attr_child->next; attr_child->next = p1; p = p->next; /需要區分出是哪一種屬性 /建立
30、每一層的第一個節點 if (strcmp("OutLook",max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,OutLook_kind0); T->firstchild = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q) if (strcmp(q->OutLook,OutLook_kind0) =
31、 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = link_child->next; link_child->next = q1;
32、 q = q->next; else if (strcmp("Temperature",max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,Temperature_kind0); T->firstchild = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q) if (strcmp(q->Temp
33、erature,Temperature_kind0) = 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = link_child->nex
34、t; link_child->next = q1; q = q->next; else if (strcmp("Humidity",max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,Humidity_kind0); T->firstchild = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q)
35、 if (strcmp(q->Humidity,Humidity_kind0) = 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = li
36、nk_child->next; link_child->next = q1; q = q->next; else if (strcmp("Wind",max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,Wind_kind0); T->firstchild = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; w
37、hile (q) if (strcmp(q->Wind,Wind_kind0) = 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = li
38、nk_child->next; link_child->next = q1; q = q->next; int p = 0,n = 0; PN_Num(link_child,p,n); if (p != 0 && n != 0) ID3(T->firstchild,link_child,Target_Attr,attr_child); FreeLink(link_child); else if(p = 0) strcpy(T->firstchild->data,"No"); FreeLink(link_child); /s
39、trcpy(T->firstchild->data,q1->PlayTennis);/-此處應該需要修改-:) else if(n = 0) strcpy(T->firstchild->data,"Yes"); FreeLink(link_child); /建立每一層上的其他節點 tree_p = T->firstchild; for (int i = 1;i < max->attr_Num;i+) /需要區分出是哪一種屬性 if (strcmp("OutLook",max->attributes)
40、= 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,OutLook_kindi); tree_p->nextsibling = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q) if (strcmp(q->OutLook,OutLook_kindi) = 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook
41、); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = link_child->next; link_child->next = q1; q = q->next; else if (strcmp("Temperature"
42、;,max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,Temperature_kindi); tree_p->nextsibling = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q) if (strcmp(q->Temperature,Temperature_kindi) = 0) q1 = new LNode; s
43、trcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1->Temperature,q->Temperature); strcpy(q1->Wind,q->Wind); strcpy(q1->PlayTennis,q->PlayTennis); q1->next = NULL; q1->next = link_child->next; link_child->next = q1; q = q->next; else
44、 if (strcmp("Humidity",max->attributes) = 0) r = new TNode; r->firstchild = r->nextsibling = NULL; strcpy(r->weight,Humidity_kindi); tree_p->nextsibling = r; /獲取與屬性值相關的訓練樣例Example(vi),建立一個新的訓練樣本鏈表link_child q = L->next; while (q) if (strcmp(q->Humidity,Humidity_kindi) = 0) q1 = new LNode; strcpy(q1->OutLook,q->OutLook); strcpy(q1->Humidity,q->Humidity); strcpy(q1-&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 國際貿易代理基礎知識考核試卷
- 珠寶首飾表面處理技術考核試卷
- 玻璃制品耐候性測試與優化考核試卷
- 稻谷種植農業氣象服務需求與供給考核試卷
- 新材料新技術引領可持續發展的新方向考核試卷
- 果蔬汁飲料的企業文化與品牌建設考核試卷
- 紡織企業成本分析與控制考核試卷
- 勞務派遣企業招聘渠道分析與優化考核試卷
- 濟南大學《模特經紀管理》2023-2024學年第二學期期末試卷
- 江西服裝學院《嬰幼兒護理與急救》2023-2024學年第二學期期末試卷
- 《水泥窯爐富氧(全氧)燃燒技術規范》
- 第37章 主要病原性真菌課件
- 2023年小型水庫雨水情測報和大壩安全監測設施項目-實施方案
- 2024年福建省高中生物學業水平考試生物試卷試題(含答案詳解)
- 工程施工材料采購方案工程材料采購方案
- DZ∕T 0214-2020 礦產地質勘查規范 銅、鉛、鋅、銀、鎳、鉬(正式版)
- 蘇教版高中化學實驗一覽表
- MOOC 隧道工程-中南大學 中國大學慕課答案
- 學校校服采購應急預案范文
- 第一單元 《分數乘法》整單元(教案)西師大版六年級上冊數學
- (2024年)面神經炎課件完整版
評論
0/150
提交評論