def spamTest(): docList = []; classList=[]; fullText=[] for i in range(1, 26): wordList = textParse(open('./email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('./email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocalList = bayes.createVocabList(docList) trainingSet = range(50); testSet=[] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat=[];trainClass=[]; for docIndex in trainingSet: trainMat.append(bayes.addWordInList(vocalList, docList[docIndex])) trainClass.append(classList[docIndex]) p0V,p1V,ps = bayes.trainNB0(array(trainMat), array(trainClass)) errorCount = 0 for doc in testSet: wordV = bayes.addWordInList(vocalList, docList[doc]) if bayes.classifyNB(array(wordV), p0V, p1V, ps) != classList[doc]: errorCount+=1 print 'err count is %d' % errorCount
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = textParse(open('./email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('./email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocalList = bayes.createVocabList(docList) trainingSet = range(50) testSet = [] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClass = [] for docIndex in trainingSet: trainMat.append(bayes.addWordInList(vocalList, docList[docIndex])) trainClass.append(classList[docIndex]) p0V, p1V, ps = bayes.trainNB0(array(trainMat), array(trainClass)) errorCount = 0 for doc in testSet: wordV = bayes.addWordInList(vocalList, docList[doc]) if bayes.classifyNB(array(wordV), p0V, p1V, ps) != classList[doc]: errorCount += 1 print 'err count is %d' % errorCount
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = createVocabList(docList) trainingSet = range(50) testSet = [] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainingMat = [] trainingClasses = [] for docIndex in trainingSet: trainingMat.append(setOfWords2Vec(vocabList, docList[docIndex])) trainingClasses.append(classList[docIndex]) p0V, p1V, pSpam = trainNBO(trainingMat, trainingClasses) errorCount = 0 for docIndex in testSet: wordVector = setOfWords2Vec(vocabList, docList[docIndex]) if classifyNB(wordVector, p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print ("the error rate is: ", float(errorCount)/len(testSet))
def spamTest(): docList = []; classList = []; fullText = [] for i in range(1,26): wordList = textParse(open('email/spam/%d.txt' %i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) # range对象不支持del,所以要转成list trainingSet = list(range(50)); testSet = [] # 随机取10组数据作为测试机 for i in range(10): # 循环10次 # 在0到49之间(包括0,49)取随机整数 randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) # 删除数组的引用 # 把剩下的40组数据作为训练集 trainMat = []; trainClasses = [] for docIndex in trainingSet: # 由于上面删除了10个引用,还剩40个 trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNBO(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: # 转成词向量 wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(wordVector, p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print('the error rate is: ', float(errorCount)/len(testSet))
def testingNB(): # 1. 加载数据集 listOPosts, listClasses = bayes.loadDataSet() print('listOPosts: ', listOPosts, '\n************************************\nlistClasses: ', listClasses) # 2. 创建单词集合 myVocabList = bayes.createVocabList(listOPosts) # 3. 计算单词是否出现并创建数据矩阵 trainMat = [] for postinDoc in listOPosts: # 返回m * len(myVocabList)的矩阵,记录的都是0,1信息 # print('postinDoc:', postinDoc) trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) # 4. 训练数据 p0V, p1V, pAb = trainNB0(np.array(trainMat), np.array(listClasses)) # 5. 测试数据 testEntry = ['love', 'my', 'dalmatioin'] thisDoc = np.array(bayes.setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) testEntry = ['stupid', 'garbage'] thisDoc = np.array(bayes.setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): # 导入并解析文本 wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = range(50) testSet = [] # 随机构建训练集 for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 # 对测试集分类 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is:', float(errorCount) / len(testSet)
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = textParse(open('xxx/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('xxx/%d,txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = range(50) testSet = [] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: wordVector = bayes.setOfWord2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is: ', float(errorCount) / len(testSet)
def spamTest(): docList = [] classList =[] fullText = [] for i in range(1, 26): wordList = bayes.textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = bayes.textParse(open('email/ham/%d.txt' % i,errors='ignore').read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) dataSet = list(range(50)) testSet = [] for i in range(10): randIndex = int(np.random.uniform(0, len(dataSet))) testSet.append(dataSet[randIndex]) del(dataSet[randIndex]) trainMat = [] trainClasses = [] for doc in dataSet: trainMat.append(bayes.bagOfWord2Vec(vocabList, docList[doc])) trainClasses.append(classList[doc]) pAbusive, p1Vect, p0Vect = bayes.trainNB0(trainMat, trainClasses) errorCount = 0.0 for i in testSet: vec2Classify = bayes.bagOfWord2Vec(vocabList, docList[i]) if bayes.classifyNB(vec2Classify, p1Vect, p0Vect, pAbusive) != classList[i]: errorCount += 1 return errorCount/10
def test_bagOfWords2VecMN(self): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) features = bayes.bagOfWords2VecMN(myVocabList, listOPosts[0]) expected = [ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 ] self.assertEqual(features, expected)
def test_train(): listOposts,listClasses=bayes.loadDataSet() myVocabList=bayes.createVocabList(listOposts) trainMat=[] for postinDoc in listOposts: trainMat.append(bayes.setOfWords2Vec(myVocabList,postinDoc)) p0v,p1v,pab=bayes.trainNB0(trainMat,listClasses) print p1v
def test_createVocabList(self): dataSet = [ ['stupid', 'garbage'], ['love', 'puppies'] ] expected = ['stupid', 'garbage', 'love', 'puppies'] word_list = bayes.createVocabList(dataSet) self.assertTrue(set(word_list) == set(expected)) # don't want duplicate elements self.assertTrue(len(word_list) == len(expected))
def test_setOfWords2Vec(self): # listOPosts is actually... # listClasses is actually a list of labels for the data in listOPosts listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) features = bayes.setOfWords2Vec(myVocabList, listOPosts[0]) expected = [ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 ] self.assertEqual(features, expected)
def test_createVocablist(self): data_set, _ = bayes.loadDataSet() vocab_list = bayes.createVocabList(data_set) print("\n vocab_list == %s" % (vocab_list)) # 根据数据集第0行输出对应的向量表 # (即,第0行中所有单词,在整个data_set词汇表中出现的单词位置设置为1) vec = bayes.setOfWords2Vec(vocab_list, data_set[0]) print("\n vec == %s" % (vec)) vec = bayes.setOfWords2Vec(vocab_list, data_set[3]) print("\n vec == %s" % (vec))
def test_train_nb(self): data_set, listClasses = bayes.loadDataSet() vocab_list = bayes.createVocabList(data_set) print("\n vocab_list == %s" % (vocab_list)) trainMat = [] for postinDoc in data_set: trainMat.append(bayes.setOfWords2Vec(vocab_list, postinDoc)) p0Vect, p1Vect, pAbusive = bayes.trainNB0(trainMat, listClasses) print("\n p0Vect == %s\n p1Vect == %s\n pAbusive == %s\n" % (p0Vect, p1Vect, pAbusive))
def localWords(feed1, feed0): import feedparser docList = [] classList = [] fullText = [] minLen = min(len(feed1['entries']), len(feed0['entries'])) # print('minlen=%d'%minLen) for i in range(minLen): # visit one rss source every time wordList = bayes.textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) # visit rss 0 wordList = bayes.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) # remove most frequent words vocabList = bayes.createVocabList(docList) top30words = calcMostFreq(vocabList, fullText) for pairW in top30words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) trainingSet = list(range(2 * minLen)) testSet = [] for i in range(20): randIdx = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIdx]) del (trainingSet[randIdx]) trainMat = [] trainClasses = [] for docIdx in trainingSet: # print("doc idx:%d, len=%d" %( docIdx, len(docList))) trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIdx])) trainClasses.append(classList[docIdx]) p0V, p1V, pSpam = bayes.trainNB0(numpy.array(trainMat), numpy.array(trainClasses)) errorCount = 0 for docIdx in testSet: wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIdx]) if bayes.classifyNB(numpy.array(wordVector), p0V, p1V, pSpam) != classList[docIdx]: errorCount += 1 print('the error rate is: %.2f' % (float(errorCount) / len(testSet))) return vocabList, p0V, p1V
def testingNB(): listOPosts, listClasses = loadDataSet() myVocabList = createVocabList(listOPosts) trainMat = [] for postinDoc in listOPosts: trainMat.append(setOfWords2Vec(myVocabList, postinDoc)) p0V, p1V, pAb = trainNB0(array(trainMat), array(listClasses)) testEntry = ['love', 'my', 'dalmation'] tesDoc = array(setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(tesDoc, p0V, p1V, pAb)) testEntry = ['stupid', 'garbage'] tesDoc = array(setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(tesDoc, p0V, p1V, pAb))
def testSimpTrain(): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) trainMat = [] for postinDoc in listOPosts: trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) print "trainMat:", trainMat print "listClasses:", listClasses p0V,p1V,pAb = bayes.trainNB0(trainMat, listClasses) print "pAb:",pAb print "p0V:",p0V print "p1V:",p1V
def testingNB(): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) trainMat = [] for postinDoc in listOPosts: trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) p0V,p1V,pAb = bayes.trainNB0(trainMat, listClasses) testEntry = ['love', 'my', 'dalmation', 'stupid'] thisDoc = array(bayes.setOfWords2Vec(myVocabList, testEntry)) print testEntry,'classified as: ', bayes.classifyNB(thisDoc, p0V, p1V, pAb) testEntry = ['quit', 'stupid'] thisDoc = array(bayes.setOfWords2Vec(myVocabList, testEntry)) print testEntry,'classified as: ', bayes.classifyNB(thisDoc, p0V, p1V, pAb)
def localWords(feed1, feed0): docList = [] classList = [] fullText = [] minLen = min(len(feed1['entries']), len(feed0['entries'])) for i in range(minLen): wordList = st.textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = st.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) #remove top frequent words top30Words = calcMostFreq(vocabList, fullText) for pairW in top30Words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) #build training and testing set trainingSet = range(2*minLen) testSet = [] for i in range(20): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) #training p0V, p1V, pLocal = bayes.trainNB0(array(trainMat), array(trainClasses)) #testing errorCount = 0 for docIndex in testSet: wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pLocal) != classList[docIndex]: errorCount+=1 print 'the error rate is: ', float(errorCount)/len(testSet) return vocabList, p0V, p1V
def spamTest(): docList = []; classList = []; fullText = [] # 读取spam下面的26个文件 for i in range(1, 26): # 垃圾邮件的分类 wordList = textParse(open('./data/spam/%d.txt' % i).read()) # 添加到docList中 docList.append(wordList) # extend是将list1的元素添加进来 fullText.extend(wordList) classList.append(1) # 正常邮件的分类 wordList = textParse(open('./data/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) # 创建词汇表:需要文档列表 postingList。 读取所有的txt并进行词汇切分 vocabList = bayes.createVocabList(docList) docMartix = np.zeros((50, len(vocabList))) i = 0 for document in docList: # 需要将每个文档进行转换成向量 docVec = bayes.setOfWords2Vec(vocabList, document) # 添加类别 docMartix[i, :] = docVec i += 1 print('词汇表长度:', len(vocabList)) print('词汇表内容:', vocabList) print('文档内容:', docList[0], '\n词汇数', len(docList[0])) print('文档矩阵:', docMartix[0], '\n词汇数', sum(docMartix[0])) # 训练贝叶斯 p0Vec, p1Vec, pAbusive = bayes.trainNBC(docMartix, classList) # print(p1Vec) # print(p0Vec) # print(pAbusive) # 测试函数 i = 0 errorCount = 0 for document in docList: testVec = bayes.setOfWords2Vec(vocabList, document) testClass = bayes.classifyNBC(np.array(testVec), p0Vec, p1Vec, pAbusive) if testClass != classList[i]: errorCount += 1 i += 1 print('error percent: ', errorCount/float(len(docList)))
def testingNB(): postList, classList = bayes.loadDataSet() myVocabList = bayes.createVocabList(postList) trainMat = [] for post in postList: trainMat.append(bayes.setOfWords2Vec(myVocabList, post)) p0V, p1V, pAb = bayes.trainNB0(trainMat, classList) testEntry = ['love', 'my', 'dalmation'] thisDoc = bayes.setOfWords2Vec(myVocabList, testEntry) print testEntry, 'classified as: ', bayes.classifyNB( thisDoc, p0V, p1V, pAb) testEntry = ['stupid', 'garbage'] thisDoc = bayes.setOfWords2Vec(myVocabList, testEntry) print testEntry, 'classified as: ', bayes.classifyNB( thisDoc, p0V, p1V, pAb)
def test_trainNBO(self): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) trainMat = [] # list of lists, e.g., [[...], ..., [...]] for postinDoc in listOPosts: trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) # this is interesting as the names sent to the funtion imply # different types than the names received by the function. # Compare sending trainCategory to receiving listClasses. # There isn't even a hint of meaning between those two names # at the program (self-referentiall) perspective. # p0Vect, p1Vect, pAbusive = trainNBO(trainMatrix, trainCategory) p0V, p1V, pAb = bayes.trainNBO(trainMat, listClasses) # print p0V, p1V, pAb self.assertAlmostEqual(pAb, 0.5)
def localWord(feed1, feed0): docList = [] classList = [] fullText = [] minLength = min(len(feed1['entries']), len(feed0['entries'])) for i in range(minLength): wordList = bayes.textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = bayes.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) top30words = calMostFeq(vocabList, fullText) for pairW in top30words: if (pairW[0] in vocabList): vocabList.remove(pairW[0]) trainingSet = range(2 * minLength) testSet = [] for i in range(20): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMatrix = [] trainClass = [] for docIndex in trainingSet: trainMatrix.append(bayes.bagOfWordsToVec(vocabList, docList[docIndex])) trainClass.append(classList[docIndex]) p0V, p1V, pSapm = bayes.trainNB0(trainMatrix, trainClass) errorCount = 0 for docIndex in testSet: calculatedClass = bayes.classifyNB( bayes.bagOfWordsToVec(vocabList, docList[docIndex]), p0V, p1V, pSapm) if (calculatedClass != classList[docIndex]): errorCount += 1 print "error rate is: ", float(errorCount) / len(testSet) return vocabList, p0V, p1V
def spamTest(): ''' 对贝叶斯垃圾邮件分类器进行自动化处理 对测试集中的每封邮件进行分类,若邮件分类错误,测错误数加1,最后返回总的错误百分比 ''' docList = [] classList = [] fullText = [] for i in range(1, 26): #切分,解析数据,并归类为1的类别 wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) #创建词汇表 vocabList = bayes.createVocabList(docList) trainingSet = list(range(50)) testSet = [] #随机取10个邮件测试 for i in range(10): #random.uniform(x,y)随机产生一个范围为x-y的实数 randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 errorDoc = [] for docIndex in testSet: wordVector = bayes.setWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 errorDoc.append(docList[docIndex]) print(vocabList) print(trainMat) print('the errorCount is: ', errorCount) print('the testSet length is: ', len(testSet)) print('the error rate is; ', float(errorCount) / len(testSet)) print(errorDoc)
def localWords(feed1, feed0): import feedparser docList = [] classList = [] fullText = [] minLen = min(len(feed1['entries']), len(feed0['entries'])) for i in range(minLen): # 每次访问一条RSS源 wordList = bayes.textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = bayes.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) top30Words = calcMostFreq(vocabList, fullText) # 去掉出现频数最高的词 for pairW in top30Words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) trainingSet = list(range(2 * minLen)) testSet = [] for i in range(20): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print('the error rate is:', float(errorCount) / len(testSet)) return vocabList, p0V, p1V
def testingNB(): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) print(myVocabList) trainMat = [] for postinDoc in listOPosts: trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) p0V, p1V, pAb = bayes.trainNB0(trainMat, listClasses) testEntry = ['love', 'my', 'dalmation'] print thisDoc = np.array(bayes.setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) testEntry = ['stupid', 'garbage'] thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
def testNB(): listOPosts, listClasses = bayes.loadDataSet() #加载数据 myVocabList = bayes.createVocabList(listOPosts) trainMat = [] for postinDoc in listOPosts: trainMat.append(bayes.setOfWords2Vec(myVocabList, postinDoc)) p0V, p1V, pAb = bayes.trainNB0(trainMat, listClasses) resultLabel = {0: 'Not garbage', 1: 'Garbage'} testEntry = ['love', 'my', 'dalmation'] thisDoc = array(bayes.setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as:', resultLabel[bayes.classifyNB(thisDoc, p0V, p1V, pAb)]) testEntry = ['stupid', 'garbage'] thisDoc = array(bayes.setOfWords2Vec(myVocabList, testEntry)) print(testEntry, 'classified as:', resultLabel[bayes.classifyNB(thisDoc, p0V, p1V, pAb)])
def localWords(feed1, feed0): # 两份RSS文件分别经feedparser解析,得到2个字典 docList = [] # 一条条帖子组成的List, 帖子拆成了单词 classList = [] # 标签列表 fullText = [] # 所有帖子的所有单词组成的List # entries条目包含多个帖子,miNLen记录帖子数少的数目,怕越界 minLen = min(len(feed1['entries']), len(feed0['entries'])) for i in range(minLen): wordList = bayes.textParse(feed1['entries'][i]['summary']) # 取出帖子内容,并拆成词 docList.append(wordList) # ['12','34'].append(['56','78']) ==> [ ['12','34'], ['56','78'] ] fullText.extend(wordList) # ['12','34'].extend(['56','78']) ==> ['12','34','56','78'] classList.append(1) # 纽约的标签是1 wordList = bayes.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) # 旧金山的标签是0 vocabList = bayes.createVocabList(docList) # 创建词汇表 # 从fulltext中找出最高频的30个单词,并从vocabList中去除它们 top30Words = calcMostFreq(vocabList, fullText) for (word, count) in top30Words: if word in vocabList: vocabList.remove(word) trainingSet = range(2 * minLen); testSet = [] # 创建训练集、测试集 for i in range(minLen / 10): # 随机选取10%的数据,建立测试集 randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = []; trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIndex])) # 将训练集中的每一条数据,转化为词向量 trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(np.array(trainMat), np.array(trainClasses)) # 开始训练 # 用测试数据,测试分类器的准确性 errorCount = 0 for docIndex in testSet: wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIndex]) if bayes.classifyNB(np.array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is: ', float(errorCount) / len(testSet) return vocabList, p0V, p1V
def spamTest(): """ 将文件夹spam和ham中分别的25篇右键导入解析为词列表,再构建一个测试集与训练集, 50篇中再随机选10篇作为测试集,其余20篇作为测试集(留存交叉验证) :return: """ docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = bayes.textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = bayes.textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = list(range(50)) testSet = [] for i in range(10): #随机选出10篇 randIndex = int(random.uniform( 0, len(trainingSet))) #random.uniform(x, y) 方法将随机生成一个实数,它在 [x,y] 范围内。 testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: #遍历训练集中所有的文档 trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIndex])) #构建词向量 trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) #计算分类所需的概率 errorCount = 0 for docIndex in testSet: #遍历测试集 wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print('the error rate is :', float(errorCount) / len(testSet)) return vocabList, p0V, p1V
def spamTest(): docList=[]; classList = []; fullText =[] for i in range(1,26): str=open('email/spam/%d.txt' % i).read() print "str" print str wordList = bayes.textParse(str) print "wordlist" print wordList docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = bayes.textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) print "doclist" print len(docList) print docList print "fulllist" print len(fullText) print fullText print classList vocabList = bayes.createVocabList(docList)#create vocabulary trainingSet = range(50); testSet=[] #create test set for i in range(10): randIndex = int(random.uniform(0,len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat=[]; trainClasses = [] for docIndex in trainingSet:#train the classifier (get probs) trainNB0 trainMat.append(bayes.bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V,p1V,pSpam = bayes.trainNB0(array(trainMat),array(trainClasses)) errorCount = 0 for docIndex in testSet: #classify the remaining items wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]: errorCount += 1 print "classification error",docList[docIndex] print 'the error rate is: ',float(errorCount)/len(testSet)
def spamTest(): docList = [] classList = [] fullText = [] # parse text from email for i in range(1, 26): wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) #parse text wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) #build training set and test set trainingSet = range(50) testSet = [] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) #select testset and remove the testset from all dataset testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) #classify and test precision errorCount = 0 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is: ', float(errorCount) / len(testSet)
def localWords(feed1, feed0): docList = [] classList = [] fullText = [] minLen = min(len(feed1['entries']), len(feed0['entries'])) for i in range(minLen): wordList = textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = createVocabList(docList) top30Words = calcMostFreq(vocabList, fullText) for pairW in top30Words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) trainingSet = list(range(2 * minLen)) testSet = [] for i in range(20): randomIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randomIndex]) del trainingSet[randomIndex] trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bagOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = trainNB0(trainMat, trainClasses) errorCount = 0 for docIndex in testSet: wordVector = bagOfWords2Vec(vocabList, docList[docIndex]) if classifyNB(wordVector, p0V, p1V, pSpam) != classList[docIndex]: print(docList[docIndex]) errorCount += 1 print('the error rate is: ', float(errorCount) / len(testSet)) return vocabList, p0V, p1V
def localWords(feed1, feed0): import feedparser docList = [] classList = [] fullText = [] minLen = min(len(feed1['entries']), len(feed0['entries'])) print feed1['entries'] print feed0['entries'] for i in range(minLen): wordList = bayes.textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) #NY is class 1 wordList = bayes.textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) #create vocabulary top30Words = calcMostFreq(vocabList, fullText) #remove top 30 words for pairW in top30Words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) trainingSet = list(range(2 * minLen)) testSet = [] #create test set for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: #train the classifier (get probs) trainNB0 trainMat.append(bayes.bagOfWord2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: #classify the remaining items wordVector = bayes.bagOfWord2VecMN(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print('the error rate is: ', float(errorCount) / len(testSet)) return vocabList, p0V, p1V
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): try: wordList = textParse( open('data/Ch04/email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('data/Ch04/email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) except Exception as e: print(str(e)) traceback.print_exc() print(i) exit() vocabList = createVocabList(docList) trainingSet = list(range(50)) testSet = [] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del trainingSet[randIndex] trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: wordVector = setOfWords2Vec(vocabList, docList[docIndex]) if classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print(docList[docIndex]) print('the error rate is: ', float(errorCount) / len(testSet))
def run(): print 'begin--->run()' postingList, classVec = bayes.loadDataSet() myVocabList = bayes.createVocabList(postingList) # print myVocabList # print bayes.words2Vec(myVocabList,postingList[0]) # trainMat = [] # for postinDoc in postingList: # trainMat.append(bayes.words2Vec(myVocabList, postinDoc)) # p0V, p1V, pAb = bayes.trainNB0(trainMat, classVec) # bayes.testingNB() # bayes.spamTest() # print pAb # print p0V # print p1V import feedparser ny = feedparser.parse( 'https://newyork.craigslist.org/search/res?format=rss') sf = feedparser.parse('https://sfbay.craigslist.org/search/apa?format=rss') # bayes.localWords(ny,sf) bayes.getTopWords(ny, sf)
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = range(50) testSet = [] # randomly split data set into 2 sets: test set, and training set for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) # random int 0~len testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) # split trainMat = []; trainClasses = []; for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print "error word: %s" % (docList[docIndex]) print "error rate is: %f", float(errorCount) / len(testSet)
def spamTest(): """ 对贝叶斯垃圾邮件分类器进行自动化处理 :return: """ docList = [] classList = [] fullText = [] for i in range(1, 26): # 1. 导入并解析文本文件 wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.append(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.append(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = range(50) testSet = [] for i in range(10): # 2. 随机构建训练集 randIndex = int(random.uniform(0, len(trainingSet))) testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: # train the classifier (get probs) trainNB0 trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) errorCount = 0 # 3. 对测试集分类 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is: ', float(errorCount) / len(testSet)
def spamTest(): docList = [] classList = [] fullText = [] # parse text from email for i in range(1,26): wordList = textParse(open('email/spam/%d.txt' %i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) #parse text wordList = textParse(open('email/ham/%d.txt' %i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) #build training set and test set trainingSet = range(50); testSet =[] for i in range(10): randIndex = int(random.uniform(0, len(trainingSet))) #select testset and remove the testset from all dataset testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) #classify and test precision errorCount = 0 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print 'the error rate is: ', float(errorCount)/len(testSet)
def spamTest(): docList = [] classList =[] fullText = [] #导人文件夹spam与ham下的文本文件,并将它们解析为词列表 for i in range(1,26): wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = range(50);#本例中共有50封电子邮件,其中的值从0到49 testSet = [] '''选择出的数字所对应的文档被添加到测试集, 同时也将其从训练集中剔除。 ''' for i in range(10):#10封电子邮件被随机选择为测试集。 randIndex = int(random.uniform(0,len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMatrix=[]; trainClasses = [] for docIndex in trainingSet: trainMatrix.append(bayes.setOfWords2Vec(vocabList,docList[docIndex])) trainClasses.append(classList[docIndex]) p0V,p1V,pSpam = bayes.trainNB(array(trainMatrix),array(trainClasses)) errorCount=0 for docIndex in testSet: #如果邮件分类错误,则错误数加1,最后给出总的错误百分比 wordVector = bayes.setOfWords2Vec(vocabList,docList[docIndex]) if bayes.classifyNB(array(wordVector),p0V,p1V,pSpam) !=classList[docIndex]: errorCount +=1 print 'the error rate is :',float(errorCount) /len(testSet)
def spamTest(): docList = [] classList = [] #fullText = [] #没起作用 for i in range(1,26): #此案例中样本集名为1.txt~25.txt wordList = textParse(open('email/spam/%d.txt' % i).read()) #解析邮件,分隔成一个个词汇 docList.append(wordList) #将样本内容存储到docList中 #fullText.extend(wordList) classList.append(1) #spam下对应的类别标签设置为1 wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) #fullText.extend(wordList) classList.append(0) #ham下对应的类别标签设置为0 vocabList = bayes.createVocabList(docList) #通过docList获取全部的词汇表 trainingSet = list(range(50)) #此处共50个案例,与classList长度对应 testSet = [] #存储测试样本集 for i in list(range(10)): randIndex = int(random.uniform(0,len(trainingSet))) #随机提取样本作为测试样本 testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) #把测试样本从训练样本中剔除 trainMat = [] trainClasses = [] for docIndex in trainingSet:#遍历训练样本集 trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) #获取样本中使用词汇情况向量 trainClasses.append(classList[docIndex]) #获取当前样本的类别标签 p0V,p1V,pSpam = bayes.trainNB0(array(trainMat), array(trainClasses)) #训练算法,得到概率 errorCount = 0 for docIndex in testSet: #遍历测试样本集 wordVector=bayes.setOfWords2Vec(vocabList, docList[docIndex]) resultFlag = bayes.classifyNB(array(wordVector), p0V, p1V, pSpam) #使用分类函数进行分类 if(resultFlag != classList[docIndex]): #如果得到结果不正确,则错误数加上1 errorCount += 1 print('the error rate is: ', float(errorCount)/len(testSet))
def spamTest(): docList = [] classList = [] fullText = [] for i in range(1, 26): wordList = textParse(open('email/spam/%d.txt' % i, 'r').read()) docList.append(wordList) fullText.append(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i, 'r').read()) docList.append(wordList) fullText.append(wordList) classList.append(0) vocabList = bayes.createVocabList(docList) trainingSet = list(range(50)) testSet = [] # 创建存储训练集的索引值的列表和测试集的索引值的列表 for i in range(10): # 从50个邮件中,随机挑选出40个作为训练集,10个做测试集 randIndex = int(random.uniform(0, len( trainingSet))) #从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high. testSet.append(trainingSet[randIndex]) del (trainingSet[randIndex]) trainMat = [] trainClasses = [] for docIndex in trainingSet: trainMat.append(bayes.setOfWords2Vec(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V, p1V, pSpam = bayes.trainNBO(np.array(trainMat), np.array(trainClasses)) errorCount = 0 for docIndex in testSet: wordVector = bayes.setOfWords2Vec(vocabList, docList[docIndex]) if bayes.classifyNB(np.array(wordVector), p0V, p1V, pSpam) != classList[docIndex]: errorCount += 1 print('wrong testSet:', docList[docIndex]) print('wrong rate:%.2f%%' % (float(errorCount) / len(testSet) * 100))
def simptestTest(): listOPosts, listClasses = bayes.loadDataSet() myVocabList = bayes.createVocabList(listOPosts) print myVocabList print listOPosts[0] print bayes.setOfWords2Vec(myVocabList, listOPosts[0])
import bayes; listOPosts,listClasses = bayes.loadDataSet() print listOPosts; print listClasses; myVocabList = bayes.createVocabList(listOPosts); print myVocabList print bayes.setOfWords2Vec(myVocabList,listOPosts[0])
def testSetOfWords2Vec(self): trainSet, classVec = BayesTestCase.loadDataSet() vocabList = bayes.createVocabList(trainSet) vec = bayes.setOfWords2Vec(vocabList, trainSet[0]) theVec = [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1] self.assertEqual(theVec, vec)
#!/usr/bin/python #encoding:utf-8 import bayes from numpy import * postingList , classList = bayes.loadDataSet() myVocabList = bayes.createVocabList(postingList) trainMat = [] for a in postingList: trainMat.append( bayes.setOfWords( myVocabList, a ) ) pc, p0, p1 = bayes.trainNB0(trainMat, classList) test = ['stupid', 'garbage'] thisDoc = array( bayes.setOfWords( myVocabList, test ) ) print bayes.classifyNB(thisDoc, p0, p1, pc)