// naiveBayesClassify.cpp : 定义控制台应用程序的入口点。////#include “stdafx.h“#include#include#include#include#include#include //#includeusing namespace std; //save the training datatypedef vector> strDVect;//save all possible condition probabilitystypedef map>> thriMap;//service for thriMaptypedef 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 statesint existRecord(thriMap probstring aimValuestring attNamestring 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 iterSingle = 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(); iterDouble++) { strMap::iterator iterSingle = iterDouble->second.begin(); for(;iterSingle != iterDouble->second.end(); iterSingle++) { cout << iterThri->first << “ “ << iterDouble->first << “ “ << iterSingle->first << “ “ << iterSingle->second << endl; } } }}//get all possible statistics//three dimensions hash prob first key notes aim attribute valuesecond key notes refence attribute name//third key notes reference attribute value the value notes the refence attribute value numbervoid getConditionStat(strDVect datasthriMap &probaimMap &aimNum){ int ijkmn; m = datas.size(); n = datas[0].size(); doubleMap doubleMapIns; strMap strMapIns; thriMap::iterator iterThri; doubleMap::iterator iterDouble; for(i=1; i < m; i++) { //save all possible statistics for(j = 0; j < n-1; j++) { switch(existRecord(probdatas[i][n-1]datas[0][j]datas[i][j])) { case 1: //must clear strMapIns.clear(); doubleMapIns.clear(); strMapIns.insert(make_pair(datas[i][j]1)); doubleMapIns.insert(make_pair(datas[0][j]strMapIns)); prob.insert(make_pair(datas[i][n-1]doubleMapIns)); break; case 2: strMapIns.clear(); strMapIns.insert(make_pair(datas[i][j]1)); prob.find(datas[i][n-1])->second.insert(make_pair(datas[0][j]strMapIns)); break; case 3: prob.find(datas[i][n-1])->second.find(datas[0][j])->second.insert(make_pair
共有 条评论
评论
共有 条评论