AttributeError:“模块"对象没有属性“得分" [英] AttributeError: ‘module’ object has no attribute 'scores'

查看:230
本文介绍了AttributeError:“模块"对象没有属性“得分"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用nltk.metrics.scores中的功能precision时出现错误.我尝试了许多不同的进口产品,但没有成功.

I am getting an error when trying to use the function precision from nltk.metrics.scores. I have tried many different imports but with no success.

我查看了我的python目录中的文件(请参见下文),并且该函数在那里,但仅仅是无法触摸此/那个".我看着:

I looked into the files on my python directories (see below) and the function is there, but is just "can't touch this/that". 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:

  • 造成这种情况的明显原因是settings.py没有INSTALLED_APPS中列出的包含blah的目录.
  • 一个不太明显的原因:如果目录中没有文件__init__.py,也会出现此错误.
  • The obvious cause of this is that the settings.py doesn’t have the directory containing blah listed in INSTALLED_APPS.
  • A less obvious cause: you’ll also get this error if the directory doesn’t contain a file __init__.py.

推荐答案

简而言之:

from nltk import precision


冗长:

这很棘手.发生此问题的原因在于NLTK的包装方式.如果我们查看dir(nltk.metrics),除了alignment_error_rate

This is tricky. The issue occurred because of how NLTK was packaged. If we look at dir(nltk.metrics), there's nothing inside it, other than alignment_error_rate

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

BTW,在NLTK的最新版本中,alignment_error_rate已移至nltk.translate.metrics,请参见

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 . The nltk.translate package is a little unstable because it's still under-development.

https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py 返回到指标包,我们看到这个:

Going back to the metrics package, from https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py, we see this:

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)

基本上,这意味着度量标准包中的功能已经过手动编码,并已上推至nltk.metrics.__init__.py.因此,如果在此处停止导入dir(metrics),则将列出在此处导入的所有指标.

basically, this means that the functions from the metrics package has been manually coded and pushed up to nltk.metrics.__init__.py. So if the imports stop here, dir(metrics), would have listed all the metrics imported here.

但是由于在更高的级别上,位于nltk.__init__.py https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131 ,则使用以下方式导入软件包:

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:

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

但是您仍然可以访问nltk.metrics中未导入到nltk.metrics.__init__.py中的任何中间级模块.但是您必须使用正确的名称空间作为将函数保存在各自目录中的方式.请注意,这些将不会显示在dir(nltk.metrics)中,但是是导入函数的有效方法:

But you can still access any intermediate level modules that are within nltk.metrics that are not imported in 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:

>>> 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>

这篇关于AttributeError:“模块"对象没有属性“得分"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆