Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

nltk

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.
like image 533
user5496322 Avatar asked Oct 28 '15 17:10

user5496322


People also ask

How do I fix Python module has no attribute?

To solve the Python "AttributeError: module has no attribute", make sure you haven't named your local modules with names of remote modules, e.g. datetime.py or requests.py and remove any circular dependencies in import statements.

How do you fix an object that has no attribute?

In the example above, object b has the attribute disp , so the hasattr() function returns True. The list doesn't have an attribute size , so it returns False. If we want an attribute to return a default value, we can use the setattr() function. This function is used to create any missing attribute with the given value.

What is the attribute error in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.


1 Answers

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>
like image 77
alvas Avatar answered Oct 17 '22 03:10

alvas