朴素贝叶斯算法

目录

基于贝叶斯决策理论的分类方法

使用Python进行文本分类

准备数据:从文本中构建词向量

训练算法:从词向量计算概率

测试算法:根据现实情况修改分类器


基于贝叶斯决策理论的分类方法


优点:在数据较少的情况下仍然有效,可以处理多类别问题。

缺点:对于输入数据的准备方式较为敏感。

适用数据:标称型数据。


使用Python进行文本分类


准备数据:从文本中构建词向量

python 复制代码
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
python 复制代码
def createVocabList(dataSet):
  vocabSet = set([])
  for document in dataSet:
    vocabSet = vocabSet | set(document)
  return list(vocabSet)
python 复制代码
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
python 复制代码
listOPosts, listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
myVocabList

output

复制代码
['quit',
 'him',
 'is',
 'food',
 'to',
 'so',
 'please',
 'maybe',
 'love',
 'problems',
 'flea',
 'park',
 'stop',
 'not',
 'how',
 'take',
 'dog',
 'has',
 'i',
 'my',
 'dalmation',
 'garbage',
 'ate',
 'buying',
 'steak',
 'mr',
 'worthless',
 'stupid',
 'cute',
 'help',
 'licks',
 'posting']
python 复制代码
setOfWords2Vec(myVocabList, listOPosts[0])

output

复制代码
[0,
 0,
 0,
 0,
 0,
 0,
 1,
 0,
 0,
 1,
 1,
 0,
 0,
 0,
 0,
 0,
 1,
 1,
 0,
 1,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 1,
 0,
 0]

训练算法:从词向量计算概率

python 复制代码
from numpy import *
def trainNB0(trainMatrix, trainCategory):
  numTrainDocs = len(trainMatrix)
  numWords = len(trainMatrix[0])
  pAbusive = sum(trainCategory) / float(numTrainDocs)
  p0Num = ones(numWords)
  p1Num = ones(numWords)
  p0Denom = 2.0
  p1Denom = 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 = log(p1Num / p1Denom)
  p0Vect = log(p0Num / p0Denom)
  return p0Vect, p1Vect, pAbusive
python 复制代码
listOPosts, listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
trainMat = []
for postinDoc in listOPosts:
  trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
python 复制代码
listOPosts

output

复制代码
[['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']]

python 复制代码
trainMat
python 复制代码
len(trainMat[0])

output

复制代码
32

python 复制代码
  trainMatrix = trainMat
  trainCategory = listClasses
  numTrainDocs = len(trainMatrix)
  numWords = len(trainMatrix[0])
  pAbusive = sum(trainCategory) / float(numTrainDocs)
  p0Num = ones(numWords)
  p1Num = ones(numWords)
  p0Denom = 2.0
  p1Denom = 2.0
  for i in range(numTrainDocs):
    if trainCategory[i] == 1:
      p1Num += trainMatrix[i]
      print(p1Num)
      p1Denom += sum(trainMatrix[i])
      print(p1Denom)
    else:
      p0Num += trainMatrix[i]
      print(p0Num)
      p0Denom += sum(trainMatrix[i])
      print(p0Denom)
  p1Vect = log(p1Num / p1Denom)
  p0Vect = log(p0Num / p0Denom)

output

复制代码
[1. 1. 1. 1. 1. 1. 2. 1. 1. 2. 2. 1. 1. 1. 1. 1. 2. 2. 1. 2. 1. 1. 1. 1.
 1. 1. 1. 1. 1. 2. 1. 1.]
9.0
[1. 2. 1. 1. 2. 1. 1. 2. 1. 1. 1. 2. 1. 2. 1. 2. 2. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 2. 1. 1. 1. 1.]
10.0
[1. 2. 2. 1. 1. 2. 2. 1. 2. 2. 2. 1. 1. 1. 1. 1. 2. 2. 2. 3. 2. 1. 1. 1.
 1. 1. 1. 1. 2. 2. 1. 1.]
17.0
[1. 2. 1. 1. 2. 1. 1. 2. 1. 1. 1. 2. 2. 2. 1. 2. 2. 1. 1. 1. 1. 2. 1. 1.
 1. 1. 2. 3. 1. 1. 1. 2.]
15.0
[1. 3. 2. 1. 2. 2. 2. 1. 2. 2. 2. 1. 2. 1. 2. 1. 2. 2. 2. 4. 2. 1. 2. 1.
 2. 2. 1. 1. 2. 2. 2. 1.]
26.0
[2. 2. 1. 2. 2. 1. 1. 2. 1. 1. 1. 2. 2. 2. 1. 2. 3. 1. 1. 1. 1. 2. 1. 2.
 1. 1. 3. 4. 1. 1. 1. 2.]
21.0

python 复制代码
p0V, p1V, pAb = trainNB0(trainMat, listClasses)
python 复制代码
pAb

output

复制代码
0.5

python 复制代码
p0V

output

复制代码
array([-3.25809654, -2.15948425, -2.56494936, -3.25809654, -2.56494936,
       -2.56494936, -2.56494936, -3.25809654, -2.56494936, -2.56494936,
       -2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936,
       -3.25809654, -2.56494936, -2.56494936, -2.56494936, -1.87180218,
       -2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936,
       -2.56494936, -3.25809654, -3.25809654, -2.56494936, -2.56494936,
       -2.56494936, -3.25809654])
python 复制代码
p1V

output

复制代码
array([-2.35137526, -2.35137526, -3.04452244, -2.35137526, -2.35137526,
       -3.04452244, -3.04452244, -2.35137526, -3.04452244, -3.04452244,
       -3.04452244, -2.35137526, -2.35137526, -2.35137526, -3.04452244,
       -2.35137526, -1.94591015, -3.04452244, -3.04452244, -3.04452244,
       -3.04452244, -2.35137526, -3.04452244, -2.35137526, -3.04452244,
       -3.04452244, -1.94591015, -1.65822808, -3.04452244, -3.04452244,
       -3.04452244, -2.35137526])

测试算法:根据现实情况修改分类器

python 复制代码
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
  p1 = sum(vec2Classify * p1Vec) + log(pClass1)
  p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)
  if p1 > p0:
    return 1
  else:
    return 0
python 复制代码
def testingNB():
  listOPosts, listClasses = loadDataSet()
  myVocabList = createVocabList(listOPosts)
  trainMat = []
  for postinDoc in listOPosts:
    trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
  p0V, p1V, pAb = trainNB0(trainMat, listClasses)
  testEntry = ['love', 'my', 'dalmation']
  thisDoc = setOfWords2Vec(myVocabList, testEntry)
  print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
  testEntry = ['stupid', 'garbage']
  thisDoc = setOfWords2Vec(myVocabList, testEntry)
  print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
  testEntry = ['stupid']
  thisDoc = setOfWords2Vec(myVocabList, testEntry)
  print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
python 复制代码
testingNB()

output

复制代码
['love', 'my', 'dalmation'] classified as:  0
['stupid', 'garbage'] classified as:  1
['stupid'] classified as:  1
相关推荐
浊酒南街22 分钟前
决策树python实现代码1
python·算法·决策树
FreedomLeo11 小时前
Python机器学习笔记(十三、k均值聚类)
python·机器学习·kmeans·聚类
星光樱梦1 小时前
32. 线程、进程与协程
python
阿正的梦工坊1 小时前
深入理解 PyTorch 的 view() 函数:以多头注意力机制(Multi-Head Attention)为例 (中英双语)
人工智能·pytorch·python
冠位观测者2 小时前
【Leetcode 热题 100】208. 实现 Trie (前缀树)
数据结构·算法·leetcode
西猫雷婶2 小时前
python学opencv|读取图像(十九)使用cv2.rectangle()绘制矩形
开发语言·python·opencv
海绵波波1072 小时前
flask后端开发(10):问答平台项目结构搭建
后端·python·flask
赵谨言3 小时前
基于python网络爬虫的搜索引擎设计
爬虫·python·搜索引擎
code04号3 小时前
python脚本:批量提取excel数据
开发语言·python·excel
小王爱吃月亮糖3 小时前
C++的23种设计模式
开发语言·c++·qt·算法·设计模式·ecmascript