码迷,mamicode.com
首页 > 编程语言 > 详细

NBC朴素贝叶斯分类器 ————机器学习实战 python代码

时间:2017-08-08 20:02:49      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:sam   返回   tin   dal   append   post   lis   create   贝叶斯分类   

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 07 23:40:13 2017

@author: mdz
"""
import numpy as np
def loadData():
    vocabList=[[‘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‘]]
    classList=[0,1,0,1,0,1]#1 侮辱性文字,0 正常言论
    return vocabList,classList

#对vocabList已经拆分过的句子进行筛选,筛选掉重复的单词,最后再返回list
#该list的length即属性的个数
def filterVocabList(vocabList):
    vocabSet=set([])
    for document in vocabList:
        vocabSet=vocabSet|set(document)
    return list(vocabSet)    

#对测试样本进行0-1处理
def zero_one(vocabList,input):
    returnVec=[0]*len(vocabList)
    for word in input:
       if word in vocabList:
         returnVec[vocabList.index(word)]=1
       else:
         print "the word: %s is not in my Vocabulary!"%word
    return returnVec

def trainNbc(trainSamples,trainCategory):
    numTrainSamp=len(trainSamples)
    numWords=len(trainSamples[0])
    pAbusive=sum(trainCategory)/float(numTrainSamp)
    #y=1 or 0下的特征计数
    p0Num=np.ones(numWords)
    p1Num=np.ones(numWords)
    #y=1 or 0下的类别计数
    p0NumTotal=numWords
    p1NumTotal=numWords
    for i in range(numTrainSamp):
        if trainCategory[i]==1:
             p0Num+=trainSamples[i]
             p0NumTotal+=sum(trainSamples[i])
        else:
             p1Num+=trainSamples[i]
             p1NumTotal +=sum(trainSamples[i])
    p1Vec=np.log(p1Num/p1NumTotal)
    p0Vec=np.log(p0Num/p0NumTotal)
    return p1Vec,p0Vec,pAbusive

def classifyOfNbc(testSamples,p1Vec,p0Vec,pAbusive):
    p1=sum(testSamples*p1Vec)+np.log(pAbusive)
    p0=sum(testSamples*p0Vec)+np.log(1-pAbusive)
    if p1>p0:
        return 1
    else:
        return 0
def testingNbc():
    vocabList,classList=loadData()
    vocabSet=filterVocabList(vocabList)
    trainList=[]
    for term in vocabList:
        trainList.append(zero_one(vocabSet,term))
    p1Vec,p0Vec,pAbusive=trainNbc(np.array(trainList),np.array(classList))
    testEntry=[‘love‘,‘my‘,‘daughter‘]
    testSamples=np.array(zero_one(vocabSet,testEntry))
    print testEntry,‘classified as :‘,classifyOfNbc(testSamples,p0Vec,p1Vec,pAbusive)
    testEntry=[‘stupid‘,‘garbage‘]
    testSamples=np.array(zero_one(vocabSet,testEntry))
    print testEntry,‘classified as :‘,classifyOfNbc(testSamples,p0Vec,p1Vec,pAbusive)


       


 
    

  

NBC朴素贝叶斯分类器 ————机器学习实战 python代码

标签:sam   返回   tin   dal   append   post   lis   create   贝叶斯分类   

原文地址:http://www.cnblogs.com/mdz-great-world/p/7308210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!