朴素贝叶斯原理与应用(一)

一、原理

1 贝叶斯定理

朴素贝叶斯是贝叶斯决策理论的一部分。是贝叶斯最基本、最原始的理论方法。在这里插入图片描述
贝叶斯决策理论中最重要的定理是贝叶斯定理
  P ( A ∣ B ) = P ( B ∣ A ) P ( A ) P ( B ) \ P(A|B)= \frac{P(B|A)P(A)}{P(B)}  P(AB)=P(B)P(BA)P(A)
式中, P ( A ) 是 A 的 先 验 概 率 ; P(A)是A的先验概率; P(A)A
P ( A ∣ B ) 是 已 知 B 发 生 后 A 发 生 的 条 件 概 率 , 被 称 为 A 的 后 验 概 率 ; P(A|B)是已知B发生后A发生的条件概率,被称为A的后验概率; P(AB)BAA
P ( B ∣ A ) 是 已 知 A 发 生 后 B 发 生 的 条 件 概 率 , 被 称 为 B 的 后 验 概 率 ; P(B|A)是已知A发生后B发生的条件概率,被称为B的后验概率; P(BA)ABB
P ( B ) 是 B 的 先 验 概 率 , 被 称 为 标 准 化 常 量 。 P(B)是B的先验概率,被称为标准化常量。 P(B)B

2 使用条件概率来分类

设 A = ( a 1 , . . . , a n ) ′ 设A=(a_1,...,a_n)' A=(a1,...,an)
应 用 贝 叶 斯 定 理 : P ( a i ∣ B ) = P ( B ∣ a i ) P ( a i ) P ( B ) 应用贝叶斯定理: P(a_i|B)= \frac{P(B|a_i)P(a_i)}{P(B)} P(aiB)=P(B)P(Bai)P(ai)
1 ) 如 果 P ( a 1 ∣ B ) > P ( a 2 ∣ B ) , 就 属 于 类 别 a 1 ; 1)如果P(a_1|B)>P(a_2|B),就属于类别a_1; 1)P(a1B)>P(a2B),a1;
2 ) 如 果 P ( a 2 ∣ B ) > P ( a 1 ∣ B ) , 就 属 于 类 别 a 2 ; 2)如果P(a_2|B)>P(a_1|B),就属于类别a_2; 2)P(a2B)>P(a1B),a2;

3 使用朴素贝叶斯进行文档分类

3.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]    #1 is abusive, 0 not
    return postingList, classVec
def createVocabList(dataSet):
    vocabSet = set([])  #create empty set
    for document in dataSet:
        vocabSet = vocabSet | set(document) #union of the two sets
    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 my Vocabulary!" % word)
    return returnVec
3.2 训练算法:利用词向量计算概率
def trainNB0(trainMatrix, trainCategory):
    numTrainDocs = len(trainMatrix)
    numWords = len(trainMatrix[0])
    pAbusive = sum(trainCategory)/float(numTrainDocs)
    p0Num = np.ones(numWords); p1Num = np.ones(numWords)      #change to np.ones()
    p0Denom = 2.0; p1Denom = 2.0                        #change to 2.0
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p1Vect = np.log(p1Num/p1Denom)          #change to np.log()
    p0Vect = np.log(p0Num/p0Denom)          #change to np.log()
    return p0Vect, p1Vect, pAbusive
3.3 测试算法
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
    p1 = sum(vec2Classify * p1Vec) + np.log(pClass1)    #element-wise mult
    p0 = sum(vec2Classify * p0Vec) + np.log(1.0 - pClass1)
    if p1 > p0:
        return 1
    else:
        return 0

def testingNB():
    listOPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listOPosts)
    trainMat = []
    for postinDoc in listOPosts:
        trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
    p0V, p1V, pAb = trainNB0(np.array(trainMat), np.array(listClasses))
    testEntry = ['love', 'my', 'dalmation']
    thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
    print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
    testEntry = ['stupid', 'garbage']
    thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
    print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
3.4 词袋模型
def bagOfWords2VecMN(vocabList, inputSet):
    returnVec = [0]*len(vocabList)
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] += 1
    return returnVec

下一节: 使用朴素贝叶斯过滤垃圾邮件。敬请关注!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值