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


    通过学习贝叶斯(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
  • 相关阅读:
    通过securecrt跳板机登录linux服务器
    python2.x提示这个错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position
    使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
    linux 中文乱码解决
    工作中常用到的命令
    LR学习文档整理
    Django入门示例之被解放的姜戈——02 庄园疑云(数据库及模型)
    Django入门示例之被解放的姜戈——01 初试天涯(安装及启动)
    格式化字符串format函数
    Python之print 格式化输出
  • 原文地址:https://www.cnblogs.com/TimVerion/p/11211076.html
Copyright © 2020-2023  润新知