【机器学习实战学习笔记】基于概率论的分类方法——朴素贝叶斯

基本思想

朴素贝叶斯的基本思想就是选择高概率对应的类别,即如果有两类,
若p1(x,y)>p2(x,y),则分类类别为1
若p1(x,y)<p2(x,y),则分类类别为2

特点

优点:在数据较少的情况下仍然有效,可以处理多类别问题
缺点:对于输入数据的准备方式比较敏感
适用数据类型:标称型数据

一般过程

(1)收集数据:可以使用任何方法,这里使用RSS源
(2)准备数据:需要数值型或者布尔型数据
(3)分析数据:有大量特征时,绘制特征作用不大,此时可以使用直方图
(4)训练算法:计算不同的独立特征的条件概率
(5)测试算法:计算错误率
(6)使用算法:一个常见的朴素贝叶斯应用是文档分类(见示例1),可以在任意的分类场景中使用朴素贝叶斯分类器,不一定是文本

示例1

文档的自动分类
(1)从文本中构建词向量

def loadDataSet():
    postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                   ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                   ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
                   ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                   ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],
                   ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0,1,0,1,0,1]
    return postingList,classVec

def createVocabList(dataset):
    vocabSet = set([])
    for doc in dataset:
        vocabSet = vocabSet | set(doc)
    print(vocabSet)
    return list(vocabSet)

def setOfWords2Vec(vocabList,inputSet):
    returnVec = [0] * len(vocabList)
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] = 1
        else:
            print("The word %s is not in the vocabulary list" % word)
    print(returnVec)
    return returnVec

if __name__ == '__main__':
    postingList,classVec = loadDataSet()
    vocabList = createVocabList(postingList)
    returnVec = setOfWords2Vec(vocabList,postingList[0])

在这里插入图片描述
(2)从词向量中计算概率(***)
伪代码如下:

计算每个类别中的文档数目
对每篇训练文档:
	对每个类别:
		如果词条出现在文档中,则增加该词条的计数值
		增加所有词条的计数值
对每个类别:
	对每个词条:
		将该词条的数目除以总词条数目得到条件概率
返回每个类别的条件概率

贝叶斯公式如下:
p ( c i ∣ ω ) = p ( ω ∣ c i ) p ( c i ) p ( ω ) p(c_{i}|\omega)= \frac{p(\omega|c_{i})p(c_{i})}{p(\omega)} p(ciω)=p(ω)p(ωci)p(ci)
用代码实现:

"""
Function:
    the naive bayes
Parameters:
    trainingMatrix: the training document which has transformed to the vector
    trainCategory: the training category,1 means abusive documents
Output/Return:
    p0Vec: the probability of a word to be not abusive
    p1Vec: the probability of a word to be abusive
    pAbusive: the probability of the abusive document
"""
def trainNB0(trainMatrix,trainCategory):
    num_training = len(trainMatrix) # the number of the training examples
    num_words = len(trainMatrix[0]) # the number of the words in the document
    pAbusive = sum(trainCategory) / float(num_training) # the probabiliry of the abusive documents
    p0Num = zeros(num_words)
    p1Num = zeros(num_words)
    p0Denom = 0.0
    p1Denom = 0.0
    for i in range(num_training):
        if trainCategory[i] == 1: # if it is the abusive document
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p0Vec = p0Num / p0Denom  # calculate the probability
    p1Vec = p1Num / p1Denom # calculate the probability
    return p0Vec,p1Vec,pAbusive

但需注意,在利用贝叶斯分类器对文档进行分类时,要计算多个概率的乘积以获得文档属于某个类别的概率。如果其中一个概率值为0,那么最终的概率乘积也为0,为了避免这种影响,可以将所有词的出现数初始化为1,分母初始化为2
此外,还会出现下溢出的问题,这是由于太多很小的数相乘所导致的,因此对乘积采用自然对数,以此来避免下溢出或者浮点数舍入导致的错误。

(3)测试过程

持续更新。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值