本文介绍朴素贝叶斯算法如何对文本进行分类。比如,每个用户的购物评论就是一篇文本,识别出这篇文本属于正向评论还是负面评论 就是分类的过程,而类别就是:{正面评论,负面评论}。正面评论为Positive,用标识符'+'表示;负面评论为Negative,用标识符'-'表示。
一,分类目标
寻找文本的某些特征,然后根据这些特征将文本归为某个类。
The goal of classification is to take a single observation, extract some useful
features, and thereby classify the observation into one of a set of discrete classes.
使用监督式机器学习方法对文本进行分类:首先假设已经有分好类的N篇文档:(d1,c1)、(d2,c2)、(d3,c3)……(dn,cn)
di表示第i篇文档,ci表示第i个类别。目标是:寻找一个分类器,这个分类器能够:当丢给它一篇新文档d,它就输出d (最有可能)属于哪个类别c
二,分类器的介绍
①Generative classifier
朴素贝叶斯分类器属于Generative classifier。
②Discriminative classifier
逻辑回归属于Discriminative classifier。
Generative classifiers like naive Bayes build a model of each class. Given an observation,they return the class most likely to have generated the observation.
Discriminative classifiers like logistic regression instead learn what features from the input are most useful to discriminate between the different possible classes.
三,词袋模型(Bag Of Words)
前面提到,文本分类需要寻找文本的特征。而词袋模型就是表示文本特征的一种方式。给定一篇文档,它会有很多特征,比如文档中每个单词出现的次数、某些单词出现的位置、单词的长度、单词出现的频率……而词袋模型只考虑一篇文档中单词出现的频率(次数),用每个单词出现的频率作为文档的特征(或者说用单词出现的频率来代表该文档)。词袋模型的示意图如下:
We represent a t