




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗數(shù)據(jù)挖掘決策樹算法實現(xiàn)實驗內容決策樹算法是非常常用的分類算法,是逼近離散目標函數(shù)的方法,學習得到的函數(shù)以決策樹 的形式表示。其基木思路是不斷選取產(chǎn)生信息增益最人的屬性來劃分樣例集和,構造決策樹。 信息增益定義為結點與其子結點的信息爛之差。信息癇是香農(nóng)提出的,用于描述信息不純度 (不穩(wěn)定性),其計算公式是entropy (s)二 -z a log p,z = 1pi子集合中不同性(而二元分類即正樣例和負樣例)的樣例的比例。這樣信息收益nj以定義為 樣本按照某丿肉性劃分吋造成嫡減少的期望,可以區(qū)分訓練樣本中正負樣本的能力,其計算公 式是:gain (s9a) = entropy (s) 工 -
2、'entropy (s、.)ver(j) i s i 7(a)是屬件a的值域s足樣本集介s、.是s屮在屈件a i值等jf的樣木集介實現(xiàn)該算法針對的樣例集侖如下outlook temperaturehumidinwindplavtennis1sunnyhothighweakno<-'sutmvhothighstrongno3overcasthothighweakves4rainyfmildhighweakv亡s3if5rainvcoolnomialweakves6rainvjcoolnomialstrongno a7overcastcoolnomialstrongy亡sa8
3、sunnymildhighweak門oq9sunnycoolnormalweakv亡s310rainvniildnomialweakyes111suimyjmildnomialweakvesa12overcastmildhighstrongy亡s313overcasthotnormalweakves<-*j14rainymildhighstrongnoq該農(nóng)記錄了在不同氣候條件下是否公打球的悄況,要求根據(jù)該表用程序輸出決策樹二算法實現(xiàn)c+代碼如下:#include <iostrcam> #include <string>#include <vector>
4、;#include <map>#include <algorithm>#include <cmath> using namespace std;#define maxlen 6輸入每行的數(shù)據(jù)個數(shù)vector <vector <string> > state; vector <string> item(maxlen); vector <string> attribute_row; string end(”end”);/輸入結束 string yes(nyesn);string no(nnon);string bl
5、ank(“”);map<string,vector < string > > map. int tree_size = 0;struct nodestring attribute;string arrived_value; vector<node *> childs;node()attribute = blank; arrived_value = blank;node * root;實例集對應一行實例集保存首行即屬性行數(shù)據(jù)attributevalues;/存儲屬性對應的所冇的值決策樹節(jié)點屬性值到達的屬性值所有的孩了根據(jù)數(shù)據(jù)實例計算屬性與值組成的mapvoid
6、 computemapfrom2dvector() unsigned int i,jjk;bool exited = false;vector<string> values;for(i = 1;i< maxleng-1; i+)按照列遍歷for (j = 1; j < statc.sizc(); j+)for (k = 0; k < values.size(); k+) if(!pare(statejlli) exited = true;if(!exited)values.push_back(stateji);exited = false;m
7、ap_attribute_valuesstateoi = values; values.erase(values.begin(), values.end();根據(jù)具體屈性和值來計算爛double computeentropy(vector <vector <string> > remain_state, string attribute, string value,bool ifparent)vcctor<int> count (2,0);unsigned int ij;bool done_flag = false;for(j = l;j< maxle
8、n; j+)if(done_flag) break;if(! attributc_row j .comparc(attributc) for(i = 1; i < remain_state.size(); i+)if(!ifparent&&!remain_pare(value) ii ifparent)/ifparent 記錄是否算父節(jié)點if(!remain_stateilmaxlen - pare(yes)count0+;else countll j+;donc_flag = true;)if(count0 = 0 ii count 1
9、= 0 ) return 0;double sum = count()l + countfl;doubleentropy=-count0/sum*log(count0/sum)/log(2.0)-countl /sum*log(count 1 /sum)/log(2.0);return entropy;計算按照屬性attribute劃分當前剩余實例的信息增益double computegain(vector <vector <string> > remain_state, string attribute)!unsigned int j,k,m;double paren
10、t_entropy = computeentropy(remain_state, attribute, blank, true);double children_entropy = 0;vcctor<string> values = map_attributc_valucsattributc;vector<double> ratio;vector<int> count_values;int tcmpint;for(m = 0; m < values.size(); m+)tempint = 0;for(k = l;k< maxlen 1; k+)
11、if(! attribute_row kj .compare(attribute) for(j = 1; j < remain_state.size(); j+)if(!remain_pare(valuesm) tempint+;) count_values.push_back(tempint);)for(j = 0; j < values.size(); j+)ratio.push_back(doublc)count_valucsj / (doublc)(rcmain_statc.sizc()-1); double temp_entropy;for(j =
12、0; j < values.size(); j+)temp_entropy = computeentropy(remain_state, attribute, valuesjl, false); childrcn_cntropy += ratioj * tcmp_cntropy;return (parent_entropy - children_entropy);int findattrinumbynamc(string attri)for(int i = 0; i < maxlen; i+)if(!pare(attri) return i;cerr«
13、;mcan,t find the numth of attributeu«endl;return 0;找出樣例屮占多數(shù)的正/負性string mostcommonlabel(vector <vector <string> > remain_state) int p = 0, n = 0;for(unsigned i = 0; i < remain_state.size(); i+)if(!remain_stateijmaxlen-pare(yes) p+; else n+;if(p >= n) return yes;else return no;判
14、斷樣例是否正負性都為labelbool allthesamelabel(vector <vector <string> > remain_state, string label) int count = 0;for(unsigned int i = 0; i < remain_state.size(); i+) if(!remain_stateilmaxlen-pare(label) count+;if(count = remain_state.size()-1) return true;else return false;node * buliddcc
15、isiontrccdfs(nodc * p, vector <vcctor <string> > rcmain_statc, vector <string> remain_attribute) /if(remain_state.size() > 0)/pri n t v( re mai n_state);/if (p = null)p = new node();if (allthesamelabel(remain_state, yes)p->attribute = yes;return p;if (ahthesamelabel(remain_st
16、ate5 no) p->attribute = no;return p;if(rcmain_attributc.sizc() = 0)string label = mostcommonlabcl(rcmain_statc);p->attribute = label;return p;double max_gain = 0, tcmp_gain;vector <string>:iterator max_it = remain_attribute.begin();vector <string>:iterator itl;for(itl = remain_attr
17、ibute.begin(); itl < remain_attribute.end(); itl+) temp_gain = computegain(remain_state, (*itl);if(tcmp_gain > max_gain) max_gain = temp_gain;max_it = itl;下面根據(jù)max"指向的屬性來劃分當前樣例,更浙樣例集和屬性集 vector <string> new_attribute;vector <vector <string> > new_state;for(vector <stri
18、ng>:itcrator it2 = rcmain_attributc.bcgin(); it2 < rcmain_d(); it2+)if(*it2).compare(*max_it) new_attribute.push_back(*it2);)p >auribute = *max_it;vector <string> values = map_attribute_values*max_il;int attribuc_num = findattrinumbynamc(*maxt); new_state.push_back(attribute_row);for(
19、vector <string>:iterator it3 = values.begin(); it3 < values.end(); it3+) for(unsigned int i = 1; i < remain_state.size(); i+) if(!remain_stateiattribue_pare(*it3) ncw_statc.push_back(rcmain_statci);node * new_node = new node();new node->arrived value = *it3;if(ncw_statc.sizc()
20、= 0)ncw_nodc->attributc=mostcommonlabel(remain_suite);elsebuliddecisiontreedfs(new_node, new_state, new_attribute); p->childs.push_back(ncw_nodc);new_state.erase(new_stcite.begin()+1 ,new_state.end();/return p;void input() string s; while(cin»s,pare(end) != 0)/-l 為輸入結束 item0 = s;for(int i
21、 = 1 ;i < maxlen; i+) cin»itcmi;state. pu sh_back(item); for(int j = 0;j< maxlen; j+)attribute_row.push_back(state0j);void printtree(node *p, int depth)for (int i = 0; i < depth; i+) cout« v;按照樹的深度先輸出 tabif(!p->arrivcd_valuc.cmpty()cout«p->arrived_value«endl;for (i
22、nt i = 0; i < depth+l; i+) cout« 't' 按照樹的深度先輸出 tabcout«p->attribute«endl;for (vector<node*>:iterator it = p->childs.begin(); it != p->childs.end(); it+) printtree(*it, depth + 1);void freetree(node *p)訐(p = null)return;for (vector<nodeitendor it = p->ch
23、ilds.begin(); it != p->childs.end(); it+) frcctrcc(*it);delete p;tree_size+;int main()input();vector <string> remain_attribute;string outlook(houtlooklf);string temperature(htemperatureh);string humidity("humiditym);string wind("wind,r);remain_attribute.push_back(outlook); remain_attribute.push_back(temperature);remain_attribute.push_back(humidity); remain_attribute.push_back(wind
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國古代文化與現(xiàn)代企業(yè)管理試題及答案
- 奧運知識科普課件下載網(wǎng)
- 家鄉(xiāng)班會課課件
- 主管護師考試技巧與攻略試題及答案
- 2025執(zhí)業(yè)醫(yī)師考試常見試題及答案
- 范文感恩老師演講稿
- 行政管理專業(yè)的語文能力考察試題及答案
- 行政管理及其發(fā)展方向試題及答案
- 行政管理經(jīng)濟法考點細分試題及答案
- 護理文化建設試題及答案分析
- 汛期建筑施工安全課件
- 2025年北京市西城區(qū)九年級初三一模英語試卷(含答案)
- 道路維護保養(yǎng)項目投標方案(技術方案)
- 廣東省深圳市2025年高三年級第二次調研考試數(shù)學試題(含答案)
- 山東省山東名??荚嚶?lián)盟2025年高三4月高考模擬考試物理試卷+答案
- 廚師技能測試題及答案
- 校園景觀園林綠化植物配置設計
- 2024船用電氣電子產(chǎn)品型式認可試驗指南
- 【9物二?!可钲谑?025年4月份九年級中考第二次模擬測試物理試卷(含答案)
- 2024年度云南省二級造價工程師之安裝工程建設工程計量與計價實務題庫檢測試卷A卷附答案
- 萬科施工組織設計
評論
0/150
提交評論