《朴树贝叶斯算法及截图.docx》由会员分享,可在线阅读,更多相关《朴树贝叶斯算法及截图.docx(12页珍藏版)》请在三一办公上搜索。
1、精选优质文档-倾情为你奉上/ naiveBayesClassify.cpp : 定义控制台应用程序的入口点。/#include stdafx.h#include#include#include#include#includeusing namespace std;/save the training datatypedef vectorvector strDVect;/save all possible condition probabilitystypedef mapstring,mapstring,map thriMap;/service for thriMaptypedef mapstr
2、ing,map doubleMap;typedef map strMap;/save the kind of aim attribute valuetypedef map aimMap;/kind of every attribute including aim attribute valuetypedef map attMap;/const for denominator and numeratordouble K = 1;/sampletypedef vector sampleVect;/check the three dimensions map and return 4 statesi
3、nt existRecord(thriMap prob,string aimValue,string attName,string attValue)thriMap:const_iterator iterThri = prob.find(aimValue);if(iterThri = prob.end()return 1;doubleMap:const_iterator iterDouble = iterThri-second.find(attName);if(iterDouble = iterThri-second.end()return 2;strMap:const_iterator it
4、erSingle = iterDouble-second.find(attValue);if(iterSingle = iterDouble-second.end()return 3;return 4;void show(thriMap prob)thriMap:iterator iterThri = prob.begin();for(;iterThri != prob.end(); iterThri+)doubleMap:iterator iterDouble = iterThri-second.begin();for(;iterDouble != iterThri-second.end()
5、; iterDouble+)strMap:iterator iterSingle = iterDouble-second.begin();for(;iterSingle != iterDouble-second.end(); iterSingle+)cout first first first second endl;/get all possible statistics/three dimensions hash prob, first key notes aim attribute value,second key notes refence attribute name,/third
6、key notes reference attribute value, the value notes the refence attribute value numbervoid getConditionStat(strDVect datas,thriMap &prob,aimMap &aimNum)int i,j,k,m,n;m = datas.size();n = datas0.size(); doubleMap doubleMapIns;strMap strMapIns;thriMap:iterator iterThri;doubleMap:iterator iterDouble;f
7、or(i=1; i m; i+)/save all possible statisticsfor(j = 0; j second.insert(make_pair(datas0j,strMapIns);break;case 3:prob.find(datasin-1)-second.find(datas0j)-second.insert(make_pair(datasij,1);break;case 4:prob.find(datasin-1)-second.find(datas0j)-second.find(datasij)-second+;break;/save the kind of a
8、im attribute valueif(aimNum.find(datasin-1) != aimNum.end()aimNum.find(datasin-1)-second+;elseaimNum.insert(make_pair(datasin-1,1);void getConditionProb(thriMap &prob,aimMap aimNum,attMap attKind) /save all possible condition probabilitysthriMap:iterator iterThri = prob.begin();for(;iterThri != prob
9、.end(); iterThri+)doubleMap:iterator iterDouble = iterThri-second.begin();for(;iterDouble != iterThri-second.end(); iterDouble+)strMap:iterator iterSingle = iterDouble-second.begin();for(;iterSingle != iterDouble-second.end(); iterSingle+)/add const K,L to denominator and numerator/the part of attKi
10、nd.find(iterDouble-first)-second*K add the weight of kind of refence attribute。iterSingle-second = (iterSingle-second + K)/ (attKind.find(iterDouble-first)-second*K + aimNum.find(iterThri-first)-second);void getClassification(strDVect datas,thriMap prob,aimMap aimNum,attMap attKind,sampleVect sample
11、Ins,int records)/save all the probabilitydouble sum = 0;double max = 0;double pp;string classKind;/获得极大后验假设double h_map;aimMap:const_iterator iterAim = aimNum.begin();for(;iterAim != aimNum.end(); iterAim+)/get prior probability/the part of (-attKind.end()-second*K add the weight of the kind of aim
12、attribute。pp =(double)(iterAim-second + K)/(-attKind.end()-second*K+ records);/获得极大使然假设double h_ml = 1;for(int i=0; i first,datas0i,sampleIns.at(i) != 4)cout there is no value of attrubute datas0i first)-second.find(datas0i)-second.find(sampleIns.at(i)-second;h_map = pp*h_ml;if(h_map max)max = h_map
13、;classKind = iterAim-first;sum += h_map;/normalizemax = max / sum;cout the the most class is classKind , and the probability is max endl;int _tmain(int argc, _TCHAR* argv)int i,j,m,n;/m:number of training data,n: number of attributesm = 15; n = 5;strDVect datas(m);for(i = 0; i m; i+)datasi.resize(n)
14、;/first row save attributesdatas00 = age;datas01 = inco;datas02 = student;datas03 = credit_rating;datas04 = class:buys_computer; datas10 = =30;datas11 = high;datas12 = no;datas13 = fair;datas14 = no;datas20 = 40;datas41 = medi;datas42 = no;datas43 = fair;datas44 = yes;datas50 = 40;datas51 = low;data
15、s52 = yes;datas53 = fair;datas54 = yes;datas60 = 40;datas61 = low;datas62 = yes;datas63 = excellent;datas64 = no;datas70 = 31.40;datas71 = low;datas72 = yes;datas73 = excellent;datas74 = yes;datas80 = =30;datas81 = medi;datas82 = no;datas83 = fair;datas84 = no;datas90 = 40;datas101 = medi;datas102 =
16、 yes;datas103 = fair;datas104 = yes;datas110 = 40;datas141 = medi;datas142 = no;datas143 = excellent;datas144 = no;for(i =0; i m; i+)for(j = 0; j n; j+)cout.width(15);cout setiosflags(ios:left) datasij;cout endl; thriMap prob;aimMap aimNum;attMap attKind;attKind.insert(make_pair(age,3);attKind.inser
17、t(make_pair(inco,3);attKind.insert(make_pair(student,2);attKind.insert(make_pair(credit_rating,2);attKind.insert(make_pair(class:buys_computer,2); getConditionStat(datas,prob,aimNum);/init KK = (double)1/(m-1);getConditionProb(prob,aimNum,attKind);show(prob); sampleVect sampleIns;sampleIns.push_back(31.40); sampleIns.push_back(high);sampleIns.push_back(no);sampleIns.push_back(fair);getClassification(datas,prob,aimNum,attKind,sampleIns,m-1);return 0; 专心-专注-专业