image.png
    image.png

    1. def loadDataSet():
    2. postingList = [['my', 'dog', 'has', 'flea', \
    3. 'problems', 'help', 'please'],
    4. ['maybe', 'not', 'take', 'him', \
    5. 'to', 'dog', 'park', 'stupid'],
    6. ['my', 'dalmation', 'is', 'so', 'cute', \
    7. 'I', 'love', 'him'],
    8. ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
    9. ['mr', 'licks', 'ate', 'my', 'steak', 'how', \
    10. 'to', 'stop', 'him'],
    11. ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    12. classVec = [0,1,0,1,0,1] #1 代表侮辱性文字,0代表正常言论
    13. return postingList,classVec
    14. def createVocabList(dataSet):
    15. vocabSet = set([])
    16. for document in dataSet:
    17. vocabSet = vacabSet | set(document)
    18. return list(vocabSet)
    19. def setOfWords2Vec(vocabList, inputSet):
    20. returnVec = [0]*len(vocabList)
    21. for word in inputSet:
    22. if word in vocabList:
    23. returnVec[vocabList.index(word)] = 1
    24. else: print('the word: {}is not in my Vocabulary!' .format(word))
    25. return returnVec
    26. def trainNB0(trainMatrix, trainCategory):
    27. numTrainDocs = len(trainMatrix)
    28. numWords = len(trainMatrix[0])
    29. pAbusive = sum(trainCategory)/float(numTrainDocs)
    30. p0Num = zeros(numWords); p1Num = zeros(numWords)
    31. p0Denom = 0.0; p1Denom = 0.0
    32. for i in range(numTrainDocs):
    33. if trainCategory[i] ==1:
    34. p1Num += trainMatrix[i]
    35. p1Denom += sum(trainMatrix[i])
    36. else:
    37. p0Num += trainMatrix[i]
    38. p0Denom += sum(trainMatrix[i])
    39. p1Vect = p1Num/p1Denom
    40. p0Vect = p0Num/p0Denom
    41. return p0Vect, p1Vect, pAbusive
    42. def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
    43. p1 = sum(vec2Classify * p1Vec) + log(pClass1)
    44. p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)
    45. if p1 > p0:
    46. return 1
    47. else:
    48. return 0
    49. def testingNB():
    50. listOPosts, listClasses = loadDataSet()
    51. myVocabList = createVocabList(listOPosts)
    52. trainMat = []
    53. for postinDoc in listOPosts:
    54. trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
    55. p0V, p1V, pAb = trainNB0(trainMat, listClasses)
    56. testEntry = ['love', 'my', 'dalmation']
    57. thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
    58. print(testEntry,'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
    59. testEntry = ['stupid', 'garbage']
    60. thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
    61. print(testEntry,'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
    62. def bagOfWords2VecMN(vocabList, inputSet):
    63. returnVec = [0]*len(vocabList)
    64. for word in inputSet:
    65. if word in vocabList:
    66. returnVec[vocabList.index(word)] += 1
    67. return returnVec