无法从Pool.py获取方法“学习”

时间:2018-08-30 12:48:37

标签: python-3.x machine-learning naivebayes

我正在执行代码

     Set rsAccountNumber = CurrentDb.OpenRecordset("SELECT DISTINCT AccountNumber, email_address1 FROM [P3_DVP_UnAffirmed_Report_for_En Query]",dbOpenSnapShot)

NaiveBayes模块包含具有功能import sys sys.path.append('/home/stepfourward/naivebayes/Naive-Bayes/') from NaiveBayes import * import os DClasses = ["python", "java", "hadoop", "django", "datascience", "php"] base = "learn/" p = Pool() for i in DClasses: p.learn(base + i, i) 的{​​{1}}:

Pool.py

但是当我应用上面代码中提到的方法learn()时,出现属性错误。

def learn(self, directory, dclass_name):
        """
        directory is a path, where the files of the class with the name dclass_name can be found
        """
        x = DocumentClass(self.__vocabulary)
        dir = os.listdir(directory)
        for file in dir:
            d = Document(self.__vocabulary)
            print(directory + "/" + file)
            d.read_document(directory + "/" + file, learn=True)
            x = x + d
        self.__document_classes[dclass_name] = x
        x.SetNumberOfDocs(len(dir))

如何消除此错误。谢谢。

1 个答案:

答案 0 :(得分:0)

按照elsewhere的解释,在文件夹Naive-Bayes中克隆了存储库后,以下是使用上述NaiveBayes库的正确步骤:

你做什么

import sys
sys.path.append('Naive-Bayes/')  # your own path here
from NaiveBayes import *         # NO error here

p = Pool()

产生错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Pool' is not defined

您应该做什么:

import sys
sys.path.append('Naive-Bayes/')
from NaiveBayes.Pool import Pool # correct import

p = Pool()  # runs OK now

DClasses = ["python",  "java",  "hadoop",  "django",  "datascience",  "php"]

base = "learn/"

for i in DClasses:
    p.learn(base + i, i)

点(但不是在此之前),我收到了 expected 错误,原因仅仅是我的目录中没有您的目录(例如learn/python)机器:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/herc/SO/Naive-Bayes/NaiveBayes/Pool.py", line 29, in learn
    dir = os.listdir(directory)
FileNotFoundError: [Errno 2] No such file or directory: 'learn/python'

但明确的消息是Pool中的learn对象和Pool.py方法确实可以访问。

在Ubuntu中使用Python 3.4.3进行了测试...