2

When importing sklearn datasets eg.

from sklearn.datasets import fetch_mldata
from sklearn.datasets import fetch_openml

I get the error

Traceback (most recent call last):
  File "numbers.py", line 1, in <module>
    from sklearn.datasets import fetch_openml
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/__init__.py", line 64, in <module>
    from .base import clone
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/base.py", line 11, in <module>
    import numpy as np
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/__init__.py", line 93, in <module>
    from . import numerictypes as nt
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/numerictypes.py", line 86, in <module>
    import numbers
  File "/Users/airocoop/repos/Numbers/numbers.py", line 1, in <module>
    from sklearn.datasets import fetch_openml
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/datasets/__init__.py", line 6, in <module>
    from .base import load_breast_cancer
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/datasets/base.py", line 20, in <module>
    from ..utils import Bunch
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/utils/__init__.py", line 10, in <module>
    from scipy.sparse import issparse
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/scipy/__init__.py", line 72, in <module>
    from numpy.random import rand, randn
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/random/__init__.py", line 143, in <module>
    from .mtrand import *
  File "numpy.pxd", line 87, in init mtrand
AttributeError: module 'numpy' has no attribute 'dtype'

I am not sure why I get this

I don't get this error when running things from a jupyter notebook, which is also weird. Any help on this issue would be greatly appreciated

4
  • 2
    Seems like you have inconsistent versions of sklearn and numpy. Commented Mar 11, 2019 at 19:20
  • How, exactly is this being run? Commented Mar 11, 2019 at 19:20
  • It is also possible that the numbers environment inherited a different version of numpy from anaconda2 base environment Commented Mar 11, 2019 at 20:38
  • @juanpa.arrivillaga I am running the command $ python numbers.py from a terminal on mac OS, in the numbers environment. runing $ conda list gives me (among other things): { numpy 1.16.2 py36hacdab7b_0 }, { numpy-base 1.16.2 py36h6575580_0}, { python 3.6.8 haf84260_0 }, { scikit-learn 0.20.2 py36h27c97d8_0} Commented Mar 12, 2019 at 20:26

4 Answers 4

8

I figured this out. The answer is that the file I was running was named numbers.py. This screws the whole thing up. If you have this problem check to make sure you don't have a file in the directory called numbers.py

Easy way to check is move the file with the import statement to a different directory and try running it.

Sign up to request clarification or add additional context in comments.

1 Comment

I had this issue as well but the file causing the problem was called "signal.py" in my case. Rather than saying "make sure you don't have a file in the directory called numbers.py", you must say something like "check any recent .py file you've added and try changing its name". When I had the issue I read your answer several times but I didn't pay much attention to it because I didn't have any file called "numbers.py"...
7

Broken installation.

Do this:

1)

conda install numpy=1.13

or 2)

pip install numpy --upgrade

2 Comments

what if the OP isn't using pip to manage this particular dependency? pip was notoriously problematic when trying to install numpy/scipy on windows, requiring at one point having people install a fortran compiler. I hear the situation has gotten a lot better, but a lot of people use conda. Note, OP is working with /anaconda2/envs/numbers/lib/python3.5
Hey guys thanks for the replies. Your right I am using conda but no luck with trying: $ conda remove -n numbers numpy $ conda install numpy (numbers is my env) also tried the $ conda install numpy=1.13
1

If you use google colab with the runtime T4, you may face this issue many times. Try changing your runtime. It fixes all the versioning issues.

Comments

0

I got the same problem, but for a very different reason. I post here at this is the first question coming out when searching for 'AttributeError: module 'numpy' has no attribute 'dtype' and that may help people like me.

Using:

assert not isinstance(param.dtype, np.dtypes.StrDType)

is valid with Numpy 2.0 but not in Numpy 1.21 that I was using. I put instead:

np.issubdtype(param.dtype, np.str_)  # OK for Numpy 1.21

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.