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

    朴素贝叶斯算法C--代码有截图(共7页).docx

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

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

    朴素贝叶斯算法C--代码有截图(共7页).docx

    精选优质文档-倾情为你奉上#include <stdio.h>#include <stdafx.h>#include <string.h>#include <direct.h> /_getcwd(), _chdir()#include <stdlib.h> /_MAX_PATH, system()#include <io.h> /_finddata_t, _findfirst(), _findnext(), _findclose()char vocabulary100020;/*声明公有二维数组,用来存储分割好的单词*/*=将要分类的文本分割成单词存储在二维数组vocabulary中=*/输入参数:要分类的文本/输出参数:该文本中总单词数int SplitToWord(char text)int i=0;char seps=", .n" /*定义单词的分隔符*/ char *substring; /*利用分隔符将文本内容分割成单词并存储*/substring=strtok(text,seps); while(substring!=NULL) strcpy(vocabularyi,substring);/将单词存储到vocabulary数组中 substring=strtok(NULL,seps); i+;return i; /返回一共多少个单词/*=计算该目录下的文件数=*/输入参数:无/输出参数:该目录下.txt文件数int CountDirectory()int count=0; /txt文件计数器long hFile; _finddata_t fileinfo;/*查找.txt文件,记录文件数*/ if (hFile=_findfirst("*.txt",&fileinfo)!=-1L) do count+; while (_findnext(hFile,&fileinfo) = 0);return count;/*=计算某类别中P(ai|vj)=*/输入参数:分类文本中单词数/输出参数:该类别下P(ai|vj)float CalculateWordProbability(int wordCount)int countSame; /分类文本中的某单词在所有训练样本中出现次数int countAll=0; /训练样本中总单词数char token;FILE *fp;float wordProbability=1; /为后面联乘做准备int i,j;long hFile; _finddata_t fileinfo;for(j=0;j<wordCount;j+) /对于分类样本中的每一个单词 countSame=0; countAll=0; if(hFile=_findfirst("*.txt",&fileinfo)!=-1L) /对于该类别下每一个.txt文本 do if(fp=fopen(fileinfo.name,"r")=NULL) /是否能打开该文本 printf("Sorry!Cannot open the file!n"); exit(0); /*存储此.txt文件中每个单词并与分类文本的单词作比较*/ while(token = fgetc(fp) != EOF) char keyword1024; i = 0; keyword0 = token; / 将每个词第一个字符赋给数组第一个元素 while (keyword+i = fgetc(fp) != ' ' && keywordi != 't' && keywordi != EOF && keywordi != 'n'); / 开始读字符,直到遇到空白符,说明找到一个词 keywordi = '0'/ 加结束符 countAll+; if (strcmp(keyword,vocabularyj) = 0) /比较两个单词是否相同 countSame+; fclose(fp); while (_findnext(hFile,&fileinfo) = 0); wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300; /计算P(wj|vi),为了扩大效果而*380return wordProbability; /*=计算每个类别的最终概率输出结果=*/输入参数:分类文本中单词数 void CalculateProbability(int wordCount)/*将类别表存储在二维数组中*/FILE *fp;char classList1020; /类别列表 char ch; /临时读取字符使用 int index=0; /classList的行标 int className_c=0; /classList的列标if(fp=fopen("ClassList.txt","r")=NULL) printf("Failed to open the file: ClassList.txt.n"); ch = fgetc(fp); while(ch!=EOF) if(ch!='n') classListindexclassName_c=ch; className_c+; else classListindexclassName_c='0' index+; className_c=0; ch = fgetc(fp);/*计算总文本数和每个类别下的文本数、P(ai|vj)*/int txtCount10; /每个类别下的训练文本数int countAll=0; /训练集中总文本数float wordProbability10; /每个类别的单词概率,即P(ai|vj)if(_chdir("c:SogouCSample1") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount0=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount0; wordProbability0=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample2") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount1=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount1; wordProbability1=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample3") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount2=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount2; wordProbability2=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample4") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount3=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount3; wordProbability3=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample5") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount4=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount4; wordProbability4=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample6") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount5=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount5; wordProbability5=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("c:SogouCSample7") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount6=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount6; wordProbability6=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("C:SogouCSample8") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount7=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount7; wordProbability7=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("C:SogouCSample9") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount8=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount8; wordProbability8=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir("C:SogouCSample10") /更改当前绝对路径 printf("系统找不到指定路径!n");else txtCount9=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount9; wordProbability9=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)/*计算先验概率和最终概率并输出分类结果*/float max=0;int classNo=0;float priorProbability10;float finalProbability10;for(int i=0;i<10;i+) priorProbabilityi=(float)txtCounti/(float)countAll; /先验概率 finalProbabilityi=priorProbabilityi*wordProbabilityi; /最终概率 if(finalProbabilityi>max) /找到最大概率并记录 max=finalProbabilityi; classNo=i; printf("该文本为类别%s的概率为:%.5en",classListi,finalProbabilityi); /输出每个类别的最终概率printf("n经分析,该文本最有可能为%s类文本!n",classListclassNo); /输出最后分类结果/*=调用文本分割函数和计算最终概率函数=*/输入参数:分类文本void NaiveBayesClassifier(char text)int vocabularyCount;/分类样本中单词数vocabularyCount=SplitToWord(text); /对要分类的文本进行单词分割,结果存储在vocabulary数组中,返回分类样本中单词数CalculateProbability(vocabularyCount); /计算最终概率/*=程序入口=*/int main()char text="Microsoft offered 44.6 billion dollars to buy Yahoo.February 1st network reported the Associated Press news, Microsoft offered 44.6 billion dollars in cash and stock to buy Yahoo search site.Microsoft offered to pay 31 dollars per share for Yahoo.Microsoft's acquisition offer on Jan. 31 premium of 62% than Yahoo's closing price of 19.18 dollars.Microsoft said that Yahoo shareholders can choose cash or stock transactions. Microsoft and Yahoo have sought cooperation in late 2006 and early 2007.The last two years, Yahoo has been in a dilemma: the market share decline,poor operating performance,stock prices tumbled sharply.Trying to make a difference for Microsoft in the Internet market, the acquisition of Yahoo is a shortcut, because the two sides have very strong complementarity."/*要分类的文本内容*/ NaiveBayesClassifier(text); /*调用朴素贝叶斯分类函数,返回最终分类结果*/ printf("n");return 1; 专心-专注-专业

    注意事项

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

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




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

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

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

    收起
    展开