朴素贝叶斯算法

目录

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

使用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
相关推荐
kali-Myon4 小时前
ctfshow-web入门-JWT(web345-web350)
前端·学习·算法·web安全·node.js·web·jwt
shiming88795 小时前
机器学习中的分类:决策树、随机森林及其应用
决策树·机器学习·分类
神洛华6 小时前
datawhale11月组队学习 模型压缩技术3:2:4结构稀疏化BERT模型
深度学习·算法·bert
阿万音玲羽6 小时前
李沐《动手学深度学习》kaggle树叶分类(ResNet18无预训练)python代码实现
人工智能·python·深度学习·机器学习·分类·cnn
南宫生6 小时前
力扣-Hot100-二叉树其二【算法学习day.33】
java·数据结构·学习·算法·leetcode·动态规划
trueEve7 小时前
SQL,力扣题目1126,查询活跃业务
算法·leetcode·职场和发展
别开生面的阿杰7 小时前
蓝桥杯-洛谷刷题-day3(C++)
c++·算法·蓝桥杯
cts6187 小时前
NLP开发常见问题
人工智能·机器学习·自然语言处理
Mr.W.T7 小时前
JVM垃圾回收详解(重点)
java·jvm·算法
___Dream7 小时前
算法闭关修炼百题计划(八)
算法