weixin_40884083 2019-09-09 13:31 采纳率: 78.6%
浏览 453
已采纳

机器学习朴素贝叶斯显示属性错误

原始代码

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定义过了啊。

  • 写回答

2条回答

  • weixin_40884083 2019-09-11 11:16
    关注

    找到了,O达成0了,改一下就行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题