AttributeError: 'module' object has no attribute 'percentile'

14,005

Solution 1

The percentile function was added in version 1.5.x. You will need to upgrade to at least that version.

Did you try:

sudo pip install numpy==1.7.1 --upgrade

To check which version you are running, start the python console and run:

>>> import numpy
>>> print numpy.__version__

You can also do:

sudo pip freeze | grep numpy

The Ubuntu 9.10 numpy package uses version 1.3.03. It is likely that installing version 1.7.0 vai pip was successful, but your machine is defaulting to the python-numpy version instead. You can remove by running:

sudo apt-get remove python-numpy

Solution 2

Please upgrade your numpy package

pip install --upgrade numpy==1.19.1

or

sudo pip install --upgrade numpy==1.19.1
Share:
14,005

Related videos on Youtube

mongotop
Author by

mongotop

BY DAY: Data Engineer. BY NIGHT: I like to hit the gym for a bit and cook some nice traditional Moroccan food. FOR FUN: camping during the weekend, watching movies in the theater and reading outdoors.

Updated on June 04, 2022

Comments

  • mongotop
    mongotop over 1 year

    I use this function to calculate percentile from here:

    import numpy as np
    a = [12, 3, 45, 0, 45, 47, 109, 1, 0, 3]
    np.percentile(a, 25)
    

    But I get this error :

    AttributeError: 'module' object has no attribute 'percentile'
    

    I also tried

    import numpy.percentile as np
    

    but it didn't I got the same error.

    my numpy version is 1.3.0 I tried to upgrade but it seems like it won't I used : [sudo pip install --upgrade scipy][2] but I found that there's no upgrade.

    my ubuntu version 9.10

    my python version is : 2.6.4

    i also tried to go arround the numpy.percentile module and I found this here:

    >>> def percentile(N, P):
    ...     n = int(round(P * len(N) + 0.5))
    ...     if n > 1:
    ...         return N[n-2]
    ...     else:
    ...         return 0
    ...
    >>> a = [1, 23, 5, 45, 676, 2, 0, 4,3]
    >>> a = sorted(a)
    >>> a
    [0, 1, 2, 3, 5, 4, 23, 45, 676]
    #When I call the function using 
    >>> percentile(a,0.5)
    3
    

    but when I tried to find 0.5 percentile manually I found 5

    Can anyone help explain to me why this is happening in any of those cases?

  • mongotop
    mongotop over 10 years
    Thanks alot for your replay and the link . No I didn't try sudo pip install numpy==1.7.1 --upgrade, but I just did, and i still get the same error message.
  • mongotop
    mongotop over 10 years
    the version is 1.3.0, mean that the upgrade won't happened.
  • Nathan Villaescusa
    Nathan Villaescusa over 10 years
    Uninstall that version using sudo apt-get remove python-numpy
  • mongotop
    mongotop over 10 years
    done! do you recommend using apt-get install python-numpy to install numpy?
  • Nathan Villaescusa
    Nathan Villaescusa over 10 years
    I would if you were using a recent version of Ubuntu, but you are using 9.10 which uses a very old (likely 1.3.0) version of numpy.
  • mongotop
    mongotop over 10 years
    any idea how to install a newer version?
  • Nathan Villaescusa
    Nathan Villaescusa over 10 years
    You will need to install a newer version using pip. I would guess it is already installed and that you had two versions before.
  • mongotop
    mongotop over 10 years
  • mongotop
    mongotop over 10 years
    removing and reinstaling numpy and I changed import numpy.percentile with from numpy import percentile and that done the job