人工智能-过滤网站的恶意留言

通过学习贝叶斯(https://www.cnblogs.com/TimVerion/p/11197043.html)解决案例:

过滤网站的恶意留言

from numpy import *
from sklearn.metrics import r2_score
#过滤网站的恶意留言   侮辱性:1   非侮辱性:0
#创建一个实验样本
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
def createVocabList(dataSet):
    vocabSet = set([])
    for document in dataSet:
        vocabSet = vocabSet | set(document)  #创建两个集合的并集
    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

#朴素贝叶斯分类器训练函数,从词向量计算概率
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])
            # print("trainMatrix[i] = ",trainMatrix[i])
            # print("p1Num=",p1Num)
            # print('p1Denom=',p1Denom)
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p1Vect = p1Num / p1Denom
    p0Vect = p0Num / p0Denom
    # print("p1Vect=",p1Vect)
    # print("p0Vect=",p0Vect)
    # print('pAbusive=',pAbusive)
    return p0Vect , p1Vect , pAbusive

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

def testingNB():
    listOPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listOPosts)
    trainMat = []
    for postinDoc in listOPosts:
        trainMat.append(setOfWords2Vec(myVocabList,postinDoc))
    #print(trainMat)
    p0V,p1V,pAb = trainNB0(array(trainMat), array(listClasses))
    testEntry = ['love' , 'my','dalmation']
    thisDoc = array(setOfWords2Vec(myVocabList,testEntry))
    print(testEntry,'classified as:',classifyNB(thisDoc,p0V,p1V,pAb))
    testEntry = ['stupid','garbage']
    thisDoc = array(setOfWords2Vec(myVocabList,testEntry))
    print(testEntry,'classified as:',classifyNB(thisDoc,p0V,p1V,pAb))

testingNB()
['love', 'my', 'dalmation'] classified as: 0
['stupid', 'garbage'] classified as: 1

 

posted @ 2019-07-19 08:18  Timcode  阅读(340)  评论(0编辑  收藏  举报