openCv学习笔记(十三)—贝叶斯分类器的实现

来源:互联网 发布:网易邮箱 数据库下载 编辑:程序博客网 时间:2024/05/22 22:09

第一个是用c语言做的关于文本的分类,主要是对待分类文本所有单词在模板中概率的后验计算。算法比较简单,从网上下的(没记下地址,若不愿意公开,请留言,自当处理),稍作了一点修改。。,等有时间可以实现垃圾邮件的分类,利用斯坦福机器学习公开课中方法,统计高频词,利用朴素贝叶斯。等有时间和大家分享。

[cpp] view plaincopyprint?
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <direct.h> //_getcwd(), _chdir()
  4. #include <stdlib.h> //_MAX_PATH, system()
  5. #include <io.h> //_finddata_t, _findfirst(), _findnext(), _findclose()
  6. #include<iostream>
  7. using namespace std;
  8. //#include<fstream>
  9. char vocabulary[1000][20];/*声明公有二维数组,用来存储分割好的单词*/
  10. /*=================将要分类的文本分割成单词存储在二维数组vocabulary中================*/
  11. //@输入参数:要分类的文本
  12. //@输出参数:该文本中总单词数
  13. int SplitToWord(char text[])
  14. {
  15. int i=0;
  16. char seps[]=", .\n";/*定义单词的分隔符*/
  17. char *substring;
  18. /******利用分隔符将文本内容分割成单词并存储******/
  19. substring=strtok(text,seps);
  20. while(substring!=NULL)
  21. {
  22. strcpy(vocabulary[i],substring);//将单词存储到vocabulary数组中
  23. substring=strtok(NULL,seps);
  24. i++;
  25. }
  26. return i; //返回一共多少个单词
  27. }
  28. /*===============================计算该目录下的文件数================================*/
  29. //@输入参数:无
  30. //@输出参数:该目录下.txt文件数
  31. int CountDirectory()
  32. {
  33. int count=0; //txt文件计数器
  34. long hFile;
  35. _finddata_t fileinfo;
  36. /********查找.txt文件,记录文件数**********/
  37. if ((hFile=_findfirst("*.txt",&fileinfo))!=-1L)
  38. {
  39. do
  40. {
  41. count++;
  42. } while (_findnext(hFile,&fileinfo) == 0);
  43. }
  44. return count;
  45. }
  46. /*===================================计算某类别中∏P(ai|vj)===================================*/
  47. //@输入参数:分类文本中单词数
  48. //@输出参数:该类别下∏P(ai|vj)
  49. float CalculateWordProbability(int wordCount)
  50. {
  51. int countSame; //分类文本中的某单词在所有训练样本中出现次数
  52. int countAll=0; //训练样本中总单词数
  53. char token;
  54. FILE *fp;
  55. float wordProbability=1; //为后面联乘做准备
  56. int i,j;
  57. long hFile;
  58. _finddata_t fileinfo;
  59. for(j=0;j<wordCount;j++) //对于分类样本中的每一个单词
  60. {
  61. countSame=0;
  62. countAll=0;
  63. if((hFile=_findfirst("*.txt",&fileinfo))!=-1L)//对于该类别下每一个.txt文本
  64. {
  65. do
  66. {
  67. if((fp=fopen(fileinfo.name,"r"))==NULL)//是否能打开该文本
  68. {
  69. printf("Sorry!Cannot open the file!\n");
  70. exit(0);
  71. }
  72. /********存储此.txt文件中每个单词并与分类文本的单词作比较*******/
  73. while((token = fgetc(fp)) != EOF)
  74. {
  75. char keyword[1024];
  76. i = 0;
  77. keyword[0] = token; // 将每个词第一个字符赋给数组第一个元素
  78. while ((keyword[++i] = fgetc(fp)) !=' ' && keyword[i] != '\t' && keyword[i] != EOF && keyword[i] !='\n'); // 开始读字符,直到遇到空白符,说明找到一个词
  79. keyword[i] = '\0';// 加结束符
  80. countAll++;
  81. if (strcmp(keyword,vocabulary[j]) == 0)//比较两个单词是否相同
  82. countSame++;
  83. }
  84. fclose(fp);
  85. }while (_findnext(hFile,&fileinfo) == 0);
  86. }
  87. wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300;//计算∏P(wj|vi),为了扩大效果而*380
  88. }
  89. return wordProbability;
  90. }
  91. /*============================计算每个类别的最终概率输出结果===============================*/
  92. //@输入参数:分类文本中单词数
  93. void CalculateProbability(int wordCount,int num)
  94. {
  95. /*********将类别表存储在二维数组中*********/
  96. FILE *fp;
  97. char classList[10][20]; //类别列表
  98. char ch; //临时读取字符使用
  99. int index=0; //classList的行标
  100. int className_c=0; //classList的列标
  101. if((fp=fopen("ClassList.txt","r"))==NULL)
  102. {
  103. printf("Failed to open the file: ClassList.txt.\n");
  104. }
  105. ch = fgetc(fp);
  106. while(ch!=EOF)
  107. {
  108. if(ch!='\n')
  109. {
  110. classList[index][className_c]=ch;
  111. className_c++;
  112. }
  113. else
  114. {
  115. classList[index][className_c]='\0';
  116. index++;
  117. className_c=0;
  118. }
  119. ch = fgetc(fp);
  120. }
  121. /********计算总文本数和每个类别下的文本数、∏P(ai|vj)********/
  122. int txtCount[10]; //每个类别下的训练文本数
  123. int countAll=0; //训练集中总文本数
  124. float wordProbability[10];//每个类别的单词概率,即∏P(ai|vj)
  125. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\1"))//更改当前绝对路径
  126. printf("系统找不到指定路径!\n");
  127. else
  128. {
  129. txtCount[0]=CountDirectory(); //获取该类别下.txt文件数
  130. countAll+=txtCount[0];
  131. wordProbability[0]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  132. }
  133. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\2"))//更改当前绝对路径
  134. printf("系统找不到指定路径!\n");
  135. else
  136. {
  137. txtCount[1]=CountDirectory(); //获取该类别下.txt文件数
  138. countAll+=txtCount[1];
  139. wordProbability[1]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  140. }
  141. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\3"))//更改当前绝对路径
  142. printf("系统找不到指定路径!\n");
  143. else
  144. {
  145. txtCount[2]=CountDirectory(); //获取该类别下.txt文件数
  146. countAll+=txtCount[2];
  147. wordProbability[2]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  148. }
  149. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\4"))//更改当前绝对路径
  150. printf("系统找不到指定路径!\n");
  151. else
  152. {
  153. txtCount[3]=CountDirectory(); //获取该类别下.txt文件数
  154. countAll+=txtCount[3];
  155. wordProbability[3]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  156. }
  157. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\5"))//更改当前绝对路径
  158. printf("系统找不到指定路径!\n");
  159. else
  160. {
  161. txtCount[4]=CountDirectory(); //获取该类别下.txt文件数
  162. countAll+=txtCount[4];
  163. wordProbability[4]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  164. }
  165. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\6"))//更改当前绝对路径
  166. printf("系统找不到指定路径!\n");
  167. else
  168. {
  169. txtCount[5]=CountDirectory(); //获取该类别下.txt文件数
  170. countAll+=txtCount[5];
  171. wordProbability[5]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  172. }
  173. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\7"))//更改当前绝对路径
  174. printf("系统找不到指定路径!\n");
  175. else
  176. {
  177. txtCount[6]=CountDirectory(); //获取该类别下.txt文件数
  178. countAll+=txtCount[6];
  179. wordProbability[6]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  180. }
  181. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\8"))//更改当前绝对路径
  182. printf("系统找不到指定路径!\n");
  183. else
  184. {
  185. txtCount[7]=CountDirectory(); //获取该类别下.txt文件数
  186. countAll+=txtCount[7];
  187. wordProbability[7]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  188. }
  189. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\9"))//更改当前绝对路径
  190. printf("系统找不到指定路径!\n");
  191. else
  192. {
  193. txtCount[8]=CountDirectory(); //获取该类别下.txt文件数
  194. countAll+=txtCount[8];
  195. wordProbability[8]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  196. }
  197. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\10"))//更改当前绝对路径
  198. printf("系统找不到指定路径!\n");
  199. else
  200. {
  201. txtCount[9]=CountDirectory(); //获取该类别下.txt文件数
  202. countAll+=txtCount[9];
  203. wordProbability[9]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)
  204. }
  205. /*******计算先验概率和最终概率并输出分类结果*******/
  206. float max=0;
  207. int classNo=0;
  208. float priorProbability[10];
  209. float finalProbability[10];
  210. for(int i=0;i<num;i++)
  211. {
  212. priorProbability[i]=(float)txtCount[i]/(float)countAll;//先验概率
  213. finalProbability[i]=priorProbability[i]*wordProbability[i];//最终概率
  214. if(finalProbability[i]>max) //找到最大概率并记录
  215. {
  216. max=finalProbability[i];
  217. classNo=i;
  218. }
  219. printf("该文本为类别%s的概率为:%.5e\n",classList[i],finalProbability[i]);//输出每个类别的最终概率
  220. }
  221. printf("\n经分析,该文本最有可能为%s类文本!\n",classList[classNo]);//输出最后分类结果
  222. }
  223. /*===================调用文本分割函数和计算最终概率函数======================*/
  224. //@输入参数:分类文本
  225. void NaiveBayesClassifier(char text[],int num)
  226. {
  227. int vocabularyCount;//分类样本中单词数
  228. vocabularyCount=SplitToWord(text); //对要分类的文本进行单词分割,结果存储在vocabulary数组中,返回分类样本中单词数
  229. CalculateProbability(vocabularyCount,num); //计算最终概率
  230. }
  231. /*===================程序入口====================*/
  232. int main()
  233. {
  234. FILE *fp;
  235. if((fp=fopen("text.txt","r"))==NULL)
  236. {
  237. printf("Failed to open the file: ClassList.txt.\n");
  238. }
  239. char ch = fgetc(fp);
  240. int i=0;
  241. while(ch!=EOF)
  242. {
  243. ch = fgetc(fp);
  244. i++;
  245. }
  246. char *text=newchar(i+1);
  247. fseek(fp,0,SEEK_SET);//
  248. ch = fgetc(fp);
  249. int j=0;
  250. while(ch!=EOF)
  251. {
  252. ch = fgetc(fp);
  253. cout<<ch;
  254. text[j]=ch;
  255. j++;
  256. }
  257. // char text[]=new char(i);;
  258. int num = 2;
  259. NaiveBayesClassifier(text,num); /*调用朴素贝叶斯分类函数,返回最终分类结果*/
  260. return 1;
  261. }


这一个是openCv里的贝叶斯训练器的使用,摘自:http://blog.csdn.net/carson2005/article/details/6854024

[cpp] view plaincopyprint?
  1. //openCV中贝叶斯分类器的API函数用法举例
  2. //运行环境:winXP + VS2008 + openCV2.3.0
  3. //摘自:http://blog.csdn.net/carson2005/article/details/6854024
  4. //#include <stdafx.h>
  5. /*
  6. openCV中与贝叶斯分类器相关的API函数有以下几个:
  7. (1)CvNormalBayesClassifier::CvNormalBayesClassifier();
  8. 该函数为默认构造函数;
  9. (2)CvNormalBayesClassifier::CvNormalBayesClassifier(const Mat& trainData, const Mat& response, const Mat& varIdx=Mat(),const Mat& sampleIdx=Mat());
  10. 该函数实际是在默认构造函数内部调用train()函数进行分类器训练;
  11. (3)bool CvNormalBayesClassifier::train(const Mat& trainData, const Mat& response, const Mat& varIdx=Mat(),const Mat& sampleIdx=Mat());
  12. 该函数进行贝叶斯分类器的训练,输入向量必须为行向量;变量response必须为整数,但其在初始化时类型可被设置为CV_32FC1;
  13. 所有特征向量必须完整,不允许训练样本集的某一个向量存在数据缺失;
  14. (4)float CvNormalBayesClassifier::predict(const Mat& samples, Mat* result=0);
  15. 该函数根据用户输入的测试样本的特征向量,返回其所属的类别;注意,如果输入是很多个测试样本的特征向量组成的矩阵,返回值为result矩阵;
  16. */
  17. #include "opencv.hpp"
  18. #include <iostream>
  19. using namespace cv;
  20. using namespace std;
  21. //10个样本特征向量维数为12的训练样本集,第一列为该样本的类别标签
  22. double inputArr[10][13] =
  23. {
  24. 1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1,
  25. -1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,
  26. 1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,
  27. -1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,
  28. -1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,
  29. -1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,
  30. 1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,
  31. 1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,
  32. 1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,
  33. 1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1
  34. };
  35. //一个测试样本的特征向量
  36. double testArr[]=
  37. {
  38. 0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1
  39. };
  40. int main()
  41. {
  42. Mat trainData(10, 12, CV_32FC1);//构建训练样本的特征向量
  43. for (int i=0; i<10; i++)
  44. {
  45. for (int j=0; j<12; j++)
  46. {
  47. trainData.at<float>(i, j) = inputArr[i][j+1];
  48. }
  49. }
  50. Mat trainResponse(10, 1, CV_32FC1);//构建训练样本的类别标签
  51. for (int i=0; i<10; i++)
  52. {
  53. trainResponse.at<float>(i, 0) = inputArr[i][0];
  54. }
  55. CvNormalBayesClassifier nbc;
  56. bool trainFlag = nbc.train(trainData, trainResponse);//进行贝叶斯分类器训练
  57. if (trainFlag)
  58. {
  59. cout<<"train over..."<<endl;
  60. nbc.save("d:/normalBayes.txt");
  61. }
  62. else
  63. {
  64. cout<<"train error..."<<endl;
  65. system("pause");
  66. exit(-1);
  67. }
  68. CvNormalBayesClassifier testNbc;
  69. testNbc.load("d:/normalBayes.txt");
  70. Mat testSample(1, 12, CV_32FC1);//构建测试样本
  71. for (int i=0; i<12; i++)
  72. {
  73. testSample.at<float>(0, i) = testArr[i];
  74. }
  75. float flag = testNbc.predict(testSample);//进行测试
  76. cout<<"flag = "<<flag<<endl;
  77. system("pause");
  78. return 0;
  79. }
原创粉丝点击