贝叶斯学习方法中有用性非常高的一种为朴素贝叶斯学习期,常被称为朴素贝叶斯分类器。

在某些领域中与神经网络和决策树学习相当。尽管朴素贝叶斯分类器忽略单词间的依赖关系。即如果全部单词是条件独立的,但朴素贝叶斯分类在实际应用中有非常出色的表现。

朴素贝叶斯文本分类算法伪代码:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

朴素贝叶斯文本分类算法流程:

通过计算训练集中每一个类别的概率与不同类别下每一个单词的概率,然后利用朴素贝叶斯公式计算新文档被分类为各个类别的概率。终于输出概率最大的类别。

C++源代码:

/*
Bayesian classifier for document classifiaction
15S103182
Ethan
2015.12.27
*/
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
int stringToInteger(string a){
stringstream ss;
ss<<a;
int b;
ss>>b;
return b;
}
vector<int> openClassificationFile(const char* dataset){
fstream file;
file.open(dataset,ios::in);
if(!file)
{
cout <<"Open File Failed!" <<endl;
vector<int> a;
return a;
}
vector<int> data;
int i=1;
while(!file.eof()){
string temp;
file>>temp;
data.push_back(stringToInteger(temp));
}
file.close();
return data;
}
vector<string> openFile(const char* dataset){
fstream file;
file.open(dataset,ios::in);
if(!file)
{
cout <<"Open File Failed!" <<endl;
vector<string> a;
return a;
}
vector<string> data;
int i=1;
while(!file.eof()){
string temp;
file>>temp;
data.push_back(temp);
}
file.close();
for(int i=0;i<data.size();i++) cout<<data[i]<<"\t";
cout<<endl;
cout<<"Open file successfully!"<<endl;
return data;
}
vector<vector<string> > openFiles(const vector<char*> files){
vector<vector<string> > docs;
for(int i=0;i<files.size();i++){
vector<string> t = openFile(files[i]);
docs.push_back(t);
}
return docs;
}
void bayesian(vector<vector<string> > docs,vector<int> c,vector<string> d){
map<string,int> wordFrequency;//每一个单词出现的个数
map<int,float> cWordProbability;//类别单词频率
map<int,int> cTotalFrequency;//类别单词个数
map<int,map<string,int> > cWordlTotalFrequency;//类别下单词个数
int totalWords=0;
for(int i=0;i<docs.size();i++){
totalWords += docs[i].size();
cWordProbability[c[i]] = cWordProbability[c[i]] + docs[i].size();
map<string,int> sn;
for(int j=0;j<docs[i].size();j++){
wordFrequency[docs[i][j]] = wordFrequency[docs[i][j]] + 1;
sn[docs[i][j]] = sn[docs[i][j]] + 1;
}
map<string,int>::iterator isn;
for(isn = sn.begin();isn!=sn.end();isn++){
cWordlTotalFrequency[c[i]][isn->first] = cWordlTotalFrequency[c[i]][isn->first] + isn->second;
}
}
int tw = wordFrequency.size();
map<int,float>::iterator icWordProbability;
for(icWordProbability=cWordProbability.begin();icWordProbability!=cWordProbability.end();icWordProbability++){
cTotalFrequency[icWordProbability->first] = icWordProbability->second;
cWordProbability[icWordProbability->first] = icWordProbability->second / totalWords;
}
cout<<"Word Frequency:"<<endl;
map<string,int>::iterator iwordFrequency;
for(iwordFrequency=wordFrequency.begin();iwordFrequency!=wordFrequency.end();iwordFrequency++){
cout<<setw(8)<<iwordFrequency->first<<"\tFrequency:"<<iwordFrequency->second<<endl;
}
cout<<"Conditional Probability:"<<endl;
map<string,int> dtw;//待分类文档词频
for(int i=0;i<d.size();i++) dtw[d[i]] = dtw[d[i]] + 1;
map<string,map<int,float> > cp;//单词类别概率
map<string,int>::iterator idtw;
for(idtw=dtw.begin();idtw!=dtw.end();idtw++){
map<int,float> cf;
for(int j=0;j<cTotalFrequency.size();j++){
float p=0;
p = (float)(cWordlTotalFrequency[j][idtw->first] +1) / (cTotalFrequency[j] + wordFrequency.size());
cf[j] = p;
cout<<"P("<<idtw->first<<"|"<<j<<") \t= "<<p<<endl;
}
cp[idtw->first] = cf;
}
cout<<"Classification Probability:"<<endl;
float mp = 0;
int classification=0;
for(int i=0;i<cTotalFrequency.size();i++){
float tcp=1;
for(int j=0;j<d.size();j++){
tcp = tcp * cp[d[j]][i];
}
tcp = tcp * cWordProbability[i];
cout<<"classification:"<<i<<"\t"<<"Probability:"<<tcp<<endl;
if(mp<tcp) {
mp = tcp;
classification = i;
}
}
cout<<"The new document classification is:"<<classification<<endl;
} int main(int argc, char** argv) {
vector<vector<string> > docs;
vector<int> c = openClassificationFile("classification.txt");
vector<char *> files;
files.push_back("1.txt");files.push_back("2.txt");files.push_back("3.txt");files.push_back("4.txt");files.push_back("5.txt");
cout<<"训练文档集:"<<endl;
docs = openFiles(files);
vector<string> d;
cout<<"待分类文档:"<<endl;
d = openFile("new.txt");
bayesian(docs,c,d);
return 0;
}

效果展示:

结论:

朴素贝叶斯分类器用于处理离散型的文本数据,可以有效对文本文档进行分类。在实验过程中,最困难的地方在于数据结构的设计。因为要统计每一个文档类别的频数和每一个文档类别下单词的概率,这个地方须要用到复杂映射与统计。在编码过程中经过不断的思考,终于通过多级映射的形式储存所需的数据,终于计算出新文档的类别。通过实验,成功将新的未分类文档输入样例分类为期待的文档类型。实验结果较为惬意。

Naive Bayesian文本分类器的更多相关文章

  1. 基于Bayes和KNN的newsgroup 18828文本分类器的Python实现

    向@yangliuy大牛学习NLP,这篇博客是数据挖掘-基于贝叶斯算法及KNN算法的newsgroup18828文本分类器的JAVA实现(上)的Python实现.入门为主,没有太多自己的东西. 1. ...

  2. 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)

    算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification) 0.写在前面的话 我个人一直很喜欢算法一类的东西,在我看来算法是人类智慧的精华,其中蕴含着无与伦比 ...

  3. 朴素贝叶斯(Naive Bayesian)

    简介 Naive Bayesian算法 也叫朴素贝叶斯算法(或者称为傻瓜式贝叶斯分类) 朴素(傻瓜):特征条件独立假设 贝叶斯:基于贝叶斯定理 这个算法确实十分朴素(傻瓜),属于监督学习,它是一个常用 ...

  4. 分类算法之朴素贝叶斯分类(Naive Bayesian classification)

    分类算法之朴素贝叶斯分类(Naive Bayesian classification) 0.写在前面的话 我个人一直很喜欢算法一类的东西,在我看来算法是人类智慧的精华,其中蕴含着无与伦比的美感.而每次 ...

  5. 朴素贝叶斯分类器(Naive Bayesian Classifier)

    本博客是基于对周志华教授所著的<机器学习>的"第7章 贝叶斯分类器"部分内容的学习笔记. 朴素贝叶斯分类器,顾名思义,是一种分类算法,且借助了贝叶斯定理.另外,它是一种 ...

  6. 后端程序员之路 18、朴素贝叶斯模型(Naive Bayesian Model,NBM)

    贝叶斯推断及其互联网应用(一):定理简介 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/08/bayesian_inference_part_one.ht ...

  7. [ML学习笔记] 朴素贝叶斯算法(Naive Bayesian)

    [ML学习笔记] 朴素贝叶斯算法(Naive Bayesian) 贝叶斯公式 \[P(A\mid B) = \frac{P(B\mid A)P(A)}{P(B)}\] 我们把P(A)称为"先 ...

  8. 一个使用fasttext训练的新闻文本分类器/模型

    fastext是什么? Facebook AI Research Lab 发布的一个用于快速进行文本分类和单词表示的库.优点是很快,可以进行分钟级训练,这意味着你可以在几分钟时间内就训练好一个分类模型 ...

  9. 基于KNN的newsgroup 18828文本分类器的Python实现

    还是同前一篇作为学习入门. 1. KNN算法描述: step1: 文本向量化表示,计算特征词的TF-IDF值 step2: 新文本到达后,根据特征词确定文本的向量 step3 : 在训练文本集中选出与 ...

随机推荐

  1. (手写)mybatis 核心配置文件和接口不在同一包下的解决方案

    smart-sh-mybatis项目 app.xml文件中此处配置为: <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 --> <!-- s ...

  2. [POJ 1006] Biorhythms C++解题

        Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 107569   Accepted: 33365 ...

  3. 微信小程序开发 -- 设置屏幕亮度

    wx.setScreenBrightness(OBJECT) 设置屏幕亮度. OBJECT参数说明: 参数 类型 必填 说明 value Number 是 屏幕亮度值,范围 0~1,0 最暗,1 最亮 ...

  4. Python循环语句 if while for

    流程控制: if 条件1: 缩进的代码块 (注意缩进4个空格) elif 条件2: 缩进的代码块 elif 条件3: 缩进的代码块 ...... else: 缩进的代码块 注意1:(相同的代码块儿,同 ...

  5. install chrome on ubuntu14.04

    summary chrome broswer can't found in ubuntu14.04 default source list.To install chrome ,you must ad ...

  6. linux下 export只能设定临时变量

    今天在调用ABBYY API的时候,需要传递APPID和APPPASSWD给系统环境才能够执行相应的python调用代码. 设置之后,因为写代码自己关掉了terminal,后面直接运行报错,访问权限不 ...

  7. 九度oj 题目1041:Simple Sorting

    题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...

  8. cf- 297 < a >--字符串操作技巧

    A. Vitaliy and Pie time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. java面试题之如何中断一个线程?

    方法一:调用interrupt方法,通知线程应该中断了: A.如果线程处于被阻塞状态,那么线程将立即退出被阻塞状态,并抛出了一个InterruptedException异常. B.如果线程处于正常活动 ...

  10. fullpage在vue单页面当中使用会出现的问题以及解决办法

    在 vue 单页面当中发现fullpage会报错,报错信息大概意思为,fullpage不允许初始化多次. 解决办法,在使用fullpage的组件跳转路由进入销毁组件之前的生命周期的时候对fullpag ...