原始代码
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] #1 代表 侮辱性 文字, 0 代表 正常 言论
return postingList, classVec
def createVocabList(dataSet):
#❶ 创建 一个 空 集
vocabSet = set([])
for document in dataSet:
#❷ 创建 两个 集合 的 并 集
vocabSet = vocabSet|set(document)
return list(vocabSet)
def setOfWords2Vec(vocabList,inputSet):
#❸ 创建 一个 其中 所含 元素 都为 0 的 向量
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
执行的时候
>>> import importlib
>>> importlib.reload(bayes)
<module 'bayes' from '/Users/zhaowenqian/Desktop/机器学习实战/bayes/bayes.py'>
>>> myVocabList=bayes.createVocabList(list0Posts)
>>> myVocabList
['take', 'him', 'please', 'love', 'posting', ' to', 'maybe', 'worthless', 'food', 'how', 'flea', 'my', 'quit', 'ate', 'dalmation', 'stupid', 'steak', 'to', 'licks', 'garbage', 'cute', 'has', 'so', 'problems', 'park', 'mr', 'dog', 'I', 'is', 'stop', 'not', ' my', 'buying', 'help']
>>> bayes.set0fWords2Vec(myVocabList,postingList[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'bayes' has no attribute 'set0fWords2Vec'
报错显示module 'bayes' has no attribute 'set0fWords2Vec',但是在代码里面已经用def定义过了啊。