简体   繁体   English

AttributeError:'module'对象没有属性'score'

[英]AttributeError: ‘module’ object has no attribute 'scores'

I am getting an error when trying to use the function precision from nltk.metrics.scores . 尝试使用nltk.metrics.scores的函数precisionnltk.metrics.scores I have tried many different imports but with no success. 我尝试了很多不同的进口但没有成功。

I looked into the files on my python directories (see below) and the function is there, but is just "can't touch this/that". 我查看了我的python目录中的文件(见下文),函数就在那里,但只是“不能触及这个/那个”。 I looked at: 我在看:

/usr/local/lib/python2.7/dist-packages/nltk/metrics
/usr/local/lib/python2.7/dist-packages/nltk/metrics/scores.py

This is what my terminal is showing me: 这是我的终端向我展示的内容:

File "/home/login/projects/python-projects/test.py", line 39, in <module>
  precision = nltk.metrics.scores.precision(correct[CLASS_POS], predicted[CLASS_POS])
AttributeError: 'module' object has no attribute 'scores'

In my searches I stumbled on this link , which gives me two options, but I don't know how to proceed to either of those: 在我的搜索中,我偶然发现了这个链接 ,这给了我两个选项,但我不知道如何进行其中任何一个:

  • The obvious cause of this is that the settings.py doesn't have the directory containing blah listed in INSTALLED_APPS . 显而易见的原因是settings.py没有在INSTALLED_APPS列出的包含blah的目录。
  • A less obvious cause: you'll also get this error if the directory doesn't contain a file __init__.py . 一个不太明显的原因:如果目录不包含文件__init__.py您也会收到此错误。

In short: 简而言之:

from nltk import precision

In long: 长期:

This is tricky. 这很棘手。 The issue occurred because of how NLTK was packaged. 出现问题的原因是NLTK的打包方式。 If we look at dir(nltk.metrics) , there's nothing inside it, other than alignment_error_rate 如果我们看一下dir(nltk.metrics) ,除了alignment_error_rate之外,里面什么也没有

>>> import nltk
>>> dir(nltk.metrics)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'alignment_error_rate']

BTW, in the bleeding edge version of NLTK, alignment_error_rate has been moved to nltk.translate.metrics , see https://github.com/nltk/nltk/blob/develop/nltk/translate/metrics.py#L10 . BTW,在最新版本的NLTK中, alignment_error_rate已移至nltk.translate.metrics ,请参阅https://github.com/nltk/nltk/blob/develop/nltk/translate/metrics.py#L10 The nltk.translate package is a little unstable because it's still under-development. nltk.translate包有点不稳定,因为它仍处于开发阶段。

Going back to the metrics package, from https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py , we see this: 回到指标包,来自https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py ,我们看到:

from nltk.metrics.scores import          (accuracy, precision, recall, f_measure,
                                          log_likelihood, approxrand)
from nltk.metrics.confusionmatrix import ConfusionMatrix
from nltk.metrics.distance        import (edit_distance, binary_distance,
                                          jaccard_distance, masi_distance,
                                          interval_distance, custom_distance,
                                          presence, fractional_presence)
from nltk.metrics.paice           import Paice
from nltk.metrics.segmentation    import windowdiff, ghd, pk
from nltk.metrics.agreement       import AnnotationTask
from nltk.metrics.association     import (NgramAssocMeasures, BigramAssocMeasures,
                                          TrigramAssocMeasures, ContingencyMeasures)
from nltk.metrics.spearman        import (spearman_correlation, ranks_from_sequence,
                                      ranks_from_scores)

basically, this means that the functions from the metrics package has been manually coded and pushed up to nltk.metrics.__init__.py . 基本上,这意味着度量包中的函数已经手动编码并推送到nltk.metrics.__init__.py So if the imports stop here, dir(metrics) , would have listed all the metrics imported here. 因此,如果导入在此处停止,则dir(metrics)会列出此处导入的所有指标。

But because on the higher level, at nltk.__init__.py https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131 , the packages was imported using: 但是因为在更高级别,在nltk.__init__.py https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131 ,包使用以下方式导入:

from nltk.metrics import *

Now all metrics score has been imported to the top level meaning you can do: 现在,所有指标得分都已导入顶级,这意味着您可以执行以下操作:

>>> from nltk import precision
>>> from nltk import spearman_correlation
>>> from nltk import NgramAssocMeasures

But you can still access any intermediate level modules that are within nltk.metrics that are not imported in nltk.metrics.__init__.py . 但您仍然可以访问nltk.metrics中未在nltk.metrics.__init__.py导入的任何中间级模块。 But you have to use the correct namespaces as how the functions are saved in their respective directory. 但是您必须使用正确的名称空间作为函数保存在各自目录中的方式。 Note that these will not show in dir(nltk.metrics) but are valid ways to import a function: 请注意,这些不会显示在dir(nltk.metrics)但是是导入函数的有效方法:

>>> from nltk.metrics import spearman
>>> from nltk.metrics import paice
>>> from nltk.metrics import scores
<function precision at 0x7fb584a34938>
>>> scores.precision
>>> spearman.spearman_correlation
<function spearman_correlation at 0x7fb5842b3230>
>>> from nltk.metrics.scores import precision
>>> precision
<function precision at 0x7fb584a34938>

Replace import of nltk.metrics by this : 用这个替换nltk.metrics的导入:

from nltk.metrics import *

Now call precision or scores or recall directly. 现在调用精确度或分数或直接召回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM