欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    朴树贝叶斯算法及截图(共12页).docx

    • 资源ID:48459200       资源大小:267.19KB        全文页数:12页
    • 资源格式: DOCX        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    朴树贝叶斯算法及截图(共12页).docx

    精选优质文档-倾情为你奉上/ naiveBayesClassify.cpp : 定义控制台应用程序的入口点。/#include "stdafx.h"#include<iostream>#include<iomanip>#include<string>#include<map>#include<vector>using namespace std; /save the training datatypedef vector<vector<string>> strDVect;/save all possible condition probabilitystypedef map<string,map<string,map<string,double>>> thriMap;/service for thriMaptypedef map<string,map<string,double>> doubleMap;typedef map<string,double> strMap;/save the kind of aim attribute valuetypedef map<string,int> aimMap;/kind of every attribute including aim attribute valuetypedef map<string,int> attMap;/const for denominator and numeratordouble K = 1;/sampletypedef vector<string> sampleVect;/check the three dimensions map and return 4 statesint 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 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 value,second key notes refence attribute name,/third 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; for(i=1; i < m; i+)   /save all possible statistics  for(j = 0; j < n-1; j+)     switch(existRecord(prob,datasin-1,datas0j,datasij)      case 1:    /must clear    strMapIns.clear();    doubleMapIns.clear();    strMapIns.insert(make_pair(datasij,1);    doubleMapIns.insert(make_pair(datas0j,strMapIns);    prob.insert(make_pair(datasin-1,doubleMapIns);    break;   case 2:    strMapIns.clear();    strMapIns.insert(make_pair(datasij,1);    prob.find(datasin-1)->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 aim attribute value  if(aimNum.find(datasin-1) != aimNum.end()     aimNum.find(datasin-1)->second+;    else     aimNum.insert(make_pair(datasin-1,1);   void getConditionProb(thriMap &prob,aimMap aimNum,attMap attKind)  /save all possible condition probabilitys 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+)       /add const K,L to  denominator and numerator    /the part of "attKind.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 sampleIns,int records)  /save all the probability double 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 attribute。  pp =(double)(iterAim->second + K)/(-attKind.end()->second*K+ records);  /获得极大使然假设  double h_ml = 1;  for(int i=0; i < sampleIns.size(); i+)     if(existRecord(prob,iterAim->first,datas0i,sampleIns.at(i) != 4)       cout << "there is no value of attrubute " << datas0i << endl;    h_ml *= K;      else       h_ml *= prob.find(iterAim->first)->second.find(datas0i)->second.find(sampleIns.at(i)->second;       h_map = pp*h_ml;  if(h_map > max)     max = h_map;   classKind = iterAim->first;    sum += h_map;  /normalize max = 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 attributes m = 15; n = 5; strDVect datas(m); for(i = 0; i < m; i+)   datasi.resize(n);  /first row save attributes datas00 = "age" datas01 = "inco" datas02 = "student" datas03 = "credit_rating" datas04 = "class:buys_computer"       datas10 = "<=30" datas11 = "high" datas12 = "no" datas13 = "fair" datas14 = "no" datas20 = "<=30" datas21 = "high" datas22 = "no" datas23 = "excellent" datas24 = "no" datas30 = "31.40" datas31 = "high" datas32 = "no" datas33 = "fair" datas34 = "yes" datas40 = ">40" datas41 = "medi" datas42 = "no" datas43 = "fair" datas44 = "yes" datas50 = ">40" datas51 = "low" datas52 = "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 = "<=30" datas91 = "low" datas92 = "yes" datas93 = "fair" datas94 = "yes" datas100 = ">40" datas101 = "medi" datas102 = "yes" datas103 = "fair" datas104 = "yes" datas110 = "<=30" datas111 = "medi" datas112 = "yes" datas113 = "excellent" datas114 = "yes" datas120 = "31.40" datas121 = "medi" datas122 = "no" datas123 = "excellent" datas124 = "yes" datas130 = "31.40" datas131 = "high" datas132 = "yes" datas133 = "fair" datas134 = "yes" datas140 = ">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.insert(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 K K = (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;   专心-专注-专业

    注意事项

    本文(朴树贝叶斯算法及截图(共12页).docx)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开