计算机视觉
图像处理

机器学习—朴素贝叶斯分类器

  1. def loadDataSet():
  2.     postingList=[[‘my’‘dog’‘has’‘flea’‘problems’‘help’‘please’],
  3.                  [‘maybe’‘not’‘take’‘him’‘to’‘dog’‘park’‘stupid’],
  4.                  [‘my’‘dalmation’‘is’‘so’‘cute’‘I’‘love’‘him’],
  5.                  [‘stop’‘posting’‘stupid’‘worthless’‘garbage’],
  6.                  [‘mr’‘licks’‘ate’‘my’‘steak’‘how’‘to’‘stop’‘him’],
  7.                  [‘quit’‘buying’‘worthless’‘dog’‘food’‘stupid’]]
  8.     classVec = [0,1,0,1,0,1]    #1 is abusive, 0 not
  9.     return postingList,classVec

  1. def createVocabList(dataSet):
  2.     vocabSet = set([])  #create empty set
  3.     for document in dataSet:
  4.         vocabSet = vocabSet | set(document) #union of the two sets
  5.     return list(vocabSet)

  1. def setOfWords2Vec(vocabList, inputSet):
  2.     returnVec = [0]*len(vocabList)
  3.     for word in inputSet:
  4.         if word in vocabList:
  5.             returnVec[vocabList.index(word)] = 1
  6.         elseprint “the word: %s is not in my Vocabulary!” % word
  7.     return returnVec

  1. def trainNB0(trainMatrix,trainCategory):
  2.     numTrainDocs = len(trainMatrix)
  3.     numWords = len(trainMatrix[0])
  4.     pAbusive = sum(trainCategory)/float(numTrainDocs)
  5.     p0Num = ones(numWords); p1Num = ones(numWords)      #change to ones() 
  6.     p0Denom = 2.0; p1Denom = 2.0                        #change to 2.0
  7.     for i in range(numTrainDocs):
  8.         if trainCategory[i] == 1:
  9.             p1Num += trainMatrix[i]
  10.             p1Denom += sum(trainMatrix[i])
  11.         else:
  12.             p0Num += trainMatrix[i]
  13.             p0Denom += sum(trainMatrix[i])
  14.     p1Vect = log(p1Num/p1Denom)          #change to log()
  15.     p0Vect = log(p0Num/p0Denom)          #change to log()
  16.     return p0Vect,p1Vect,pAbusive

  1. def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
  2.     p1 = sum(vec2Classify * p1Vec) + log(pClass1)    #element-wise mult
  3.     p0 = sum(vec2Classify * p0Vec) + log(1.0 – pClass1)
  4.     if p1 > p0:
  5.         return 1
  6.     else:
  7.         return 0

d.     完整的测试流程

  1. def testingNB():
  2.     listOPosts,listClasses = loadDataSet()
  3.     myVocabList = createVocabList(listOPosts)
  4.     trainMat=[]
  5.     for postinDoc in listOPosts:
  6.         trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
  7.     p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses))
  8.     testEntry = [‘love’‘my’‘dalmation’]
  9.     thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
  10.     print testEntry,‘classified as: ‘,classifyNB(thisDoc,p0V,p1V,pAb)
  11.     testEntry = [‘stupid’‘garbage’]
  12.     thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
  13.     print testEntry,‘classified as: ‘,classifyNB(thisDoc,p0V,p1V,pAb)

执行结果:

  1. def textParse(bigString):    #input is big string, #output is word list
  2.     import re
  3.     listOfTokens = re.split(r‘\W*’, bigString)
  4.     return [tok.lower() for tok in listOfTokens if len(tok) > 2]


  1. def bagOfWords2VecMN(vocabList, inputSet):
  2.     returnVec = [0]*len(vocabList)
  3.     for word in inputSet:
  4.         if word in vocabList:
  5.             returnVec[vocabList.index(word)] += 1
  6. return returnVec

下面给出垃圾邮件预测的完整代码:

  1. def spamTest():
  2.     docList=[]; classList = []; fullText =[]
  3.     for i in range(1,26):
  4.         wordList = textParse(open(’email/spam/%d.txt’ % i).read())
  5.         docList.append(wordList)
  6.         fullText.extend(wordList)
  7.         classList.append(1)
  8.         wordList = textParse(open(’email/ham/%d.txt’ % i).read())
  9.         docList.append(wordList)
  10.         fullText.extend(wordList)
  11.         classList.append(0)
  12.     vocabList = createVocabList(docList)#create vocabulary
  13.     trainingSet = range(50); testSet=[]           #create test set
  14.     for i in range(10):
  15.         randIndex = int(random.uniform(0,len(trainingSet)))
  16.         testSet.append(trainingSet[randIndex])
  17.         del(trainingSet[randIndex])
  18.     trainMat=[]; trainClasses = []
  19.     for docIndex in trainingSet:#train the classifier (get probs) trainNB0
  20.         trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
  21.         trainClasses.append(classList[docIndex])
  22.     p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses))
  23.     errorCount = 0
  24.     for docIndex in testSet:        #classify the remaining items
  25.         wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
  26.         if classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]:
  27.             errorCount += 1
  28.             print “classification error”,docList[docIndex]
  29.     print ‘the error rate is: ‘,float(errorCount)/len(testSet)
  30.     return vocabList,fullText

执行结果:

  1. def calcMostFreq(vocabList,fullText):
  2.     import operator
  3.     freqDict = {}
  4.     for token in vocabList:
  5.         freqDict[token]=fullText.count(token)
  6.     sortedFreq = sorted(freqDict.iteritems(), key=operator.itemgetter(1), reverse=True)
  7.     return sortedFreq[:30]
  8. def localWords(feed1,feed0):
  9.     import feedparser
  10.     docList=[]; classList = []; fullText =[]
  11.     minLen = min(len(feed1[‘entries’]),len(feed0[‘entries’]))
  12.     for i in range(minLen):
  13.         wordList = textParse(feed1[‘entries’][i][‘summary’])
  14.         docList.append(wordList)
  15.         fullText.extend(wordList)
  16.         classList.append(1#NY is class 1
  17.         wordList = textParse(feed0[‘entries’][i][‘summary’])
  18.         docList.append(wordList)
  19.         fullText.extend(wordList)
  20.         classList.append(0)
  21.     vocabList = createVocabList(docList)#create vocabulary
  22.     top30Words = calcMostFreq(vocabList,fullText)   #remove top 30 words
  23.     for pairW in top30Words:
  24.         if pairW[0in vocabList: vocabList.remove(pairW[0])
  25.     trainingSet = range(2*minLen); testSet=[]           #create test set
  26.     for i in range(20):
  27.         randIndex = int(random.uniform(0,len(trainingSet)))
  28.         testSet.append(trainingSet[randIndex])
  29.         del(trainingSet[randIndex])
  30.     trainMat=[]; trainClasses = []
  31.     for docIndex in trainingSet:#train the classifier (get probs) trainNB0
  32.         trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
  33.         trainClasses.append(classList[docIndex])
  34.     p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses))
  35.     errorCount = 0
  36.     for docIndex in testSet:        #classify the remaining items
  37.         wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
  38.         if classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]:
  39.             errorCount += 1
  40.     print ‘the error rate is: ‘,float(errorCount)/len(testSet)
  41.     return vocabList,p0V,p1V

执行结果:

  1. def getTopWords(ny,sf):
  2.     import operator
  3.     vocabList,p0V,p1V=localWords(ny,sf)
  4.     topNY=[]; topSF=[]
  5.     for i in range(len(p0V)):
  6.         if p0V[i] > –6.0 : topSF.append((vocabList[i],p0V[i]))
  7.         if p1V[i] > –6.0 : topNY.append((vocabList[i],p1V[i]))
  8.     sortedSF = sorted(topSF, key=lambda pair: pair[1], reverse=True)
  9.     print “SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**”
  10.     for item in range(15):
  11.         print sortedSF[item][0]
  12.     sortedNY = sorted(topNY, key=lambda pair: pair[1], reverse=True)
  13.     print “NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**”
  14.     for item in range(15):
  15.         print sortedNY[item][0]

执行结果:了解下算法实现原理及应用即可

转载注明来源:CV视觉网 » 机器学习—朴素贝叶斯分类器

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 0超赞 0无聊 0扯淡 0不解 0路过

评论 2

评论前必须登录!