-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2024朴素贝叶斯分类算法手写.py
More file actions
131 lines (79 loc) · 4 KB
/
Copy path2024朴素贝叶斯分类算法手写.py
File metadata and controls
131 lines (79 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
######################################################
#
# python 朴素贝叶斯分类算法手写
#
#####################################################
##通过loadDataSet函数创建数据集和分类结果;
def loadDataSet():
postingList= [['my','dog','has','problems','help','please'],
['my','dog','has','problems','help','please'],
['my','dog','has','problems','help','please'],
['my','dog','has','problems','help','please'],
['my','dog','has','problems','help','please'],
['my','dog','has','problems','help','please']]
classVec = [0,1,0,1,0,1]
return postingList,calssVec
##通过createVocabList函数统计所有文档中出现的词条列表。在函数中,新建一个存放词条,遍历文档中的每一篇文章,
###将文档列表转换为集合的形式,保证每个词条的唯一性,然后与vocabSet取并集,向vocabSet中添加没有出现的心得词条,
###再将集合转换为列表
def createVocabList(dataSet):
vocabList=set([])
for document in dataSet:
vocabSet = vocabSet | set(document)
return list(vocabSet)
## 通过setOfWords2Vec() 函数根据词条列表中的词条是否在文档中出现,将文档转换为词条向量,在函数中,新建一个长度为
### vocabSet的列表,并且各维度元素初始化为0,遍历文档中的每一个词条,如果词条在词条列表中出现,通过列表获取当前Word
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
## 通过trainNB0函数训练师案发,从词向量计算概率。
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(numTrainDcos):
if trainCategory[i] == 1:
p1Num += rainMatrix[i]
p1Denom += sum(trainMatrix[i])
else:
p0Num += trainMatrix[i]
p0Denom += sum(trainMatrix[i])
p1Vect = log(p1Num/p1Denom)
p0Vect = log(p0Num/p0Denom)
return p0Vect,p1Vect,pAbusive
# 第五定义朴素贝叶斯分类函数,classifyNB(),其中vec2Classify表示待测试分类的词条向量,P0Vec表示类比为0所有文档
##中各词条出现的频数;
def calssfiyNB(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
# 通过testingNB函数,定义分类测试整体函数;在函数中由数据集获取文档矩阵和标签向量,统计所有文档中出现的词条,存入词条列表,然后创建
##新的列表,将每篇文档利用wordsVec函数转换为词条向量,存入文档矩阵中,将文档矩阵和类标签向量转换为Numpy的数组形式;
def testingNB():
listOPosts,listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
trainMat=[]
for postinDoc in listOPosts:
trainMat.append(setOfWords2Vec(myVocabList,postinDoc))
p0V,p1V,pAb = trainNB(array(trainMat),array(listClasses))
testEntry = ['love','my','dalmation']
thisDoc = array(setOfWordsVec(myVocabList,testEntry))
print(testEntry,'classified as:',classifyNB(thisDoc,p0V,p1V,pAb))
testEntry = ['stupid','garbage']
thisDoc = array(setOfWords2Vec(myVocabList,testEntry))
print(testEntry,'calssified as:',classifyNB(thisDoc,p0V,p1V,pAb))
##第七 在实验中用textParse 函数处理数据长字符串,对称行字符串进行分割,分隔符为除单词和数字之外的任意符号串。
def textParse(bigString):
import re
listOfTokens = re.split(r'\W*',bigString)
return [tok.lower() for tok in listOfTokens if len(tok) >2 ]
def testingNb()