简体   繁体   中英

AttributeError: ‘module’ object has no attribute 'scores'

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.

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:

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

In short:

from nltk import precision

In long:

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

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)

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.

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

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>

Replace import of nltk.metrics by this :

from nltk.metrics import *

Now call precision or scores or recall directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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