【问题标题】:AttributeError: module 'numpy' has no attribute 'testing'AttributeError:模块“numpy”没有属性“testing”
【发布时间】:2019-03-11 16:32:53
【问题描述】:

上周我能够在 Python 3.7.2 中运行程序就好了。

今天早上我进来,运行同样的程序,得到错误

AttributeError: module 'numpy' has no attribute 'testing'

我重新卸载并安装了 python 3.7.2

然后我做了pip3 install -U scikit-learn scipy matplotlib,运行了相同的程序,但仍然出现此错误。

整个上午都在谷歌上搜索,尝试不同的东西(当然,重启机器,检查 PATH)

请帮忙!!!!!!

代码(其他代码触发器也是如此)

# Assigning features and label variables

# First Feature
weather=['Sunny','Sunny','Overcast','Rainy','Rainy','Rainy','Overcast','Sunny','Sunny',
'Rainy','Sunny','Overcast','Overcast','Rainy']

# Second Feature
temp=['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild','Cool','Mild','Mild','Mild','Hot','Mild']

# Label or target varible
play=['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']


# Import LabelEncoder

from sklearn import preprocessing

# creating labelEncoder

le = preprocessing.LabelEncoder()

# converting string labels into numbers

weather_encoded = le.fit_transform(weather)

print(weather_encoded)

# converting string labels into numbers

temp_encoded = le.fit_transform(temp)

label = le.fit_transform(play)

# combining weather and temp into a single list of tuples

features = list(zip(weather_encoded,temp_encoded))


from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=3)

# Train the model using the training sets

model.fit(features, label)

# Predict Output

predicted = model.predict([[0,2]]) #0:Overcast, 2:Mild

print(predicted)

追溯

e:\ML>python knn.py
Traceback (most recent call last):
  File "knn.py", line 16, in <module>
    from sklearn import preprocessing
  File "C:\Python37-32\lib\site-packages\sklearn\__init__.py", line 64, in <module>
    from .base import clone
  File "C:\Python37-32\lib\site-packages\sklearn\base.py", line 11, in <module>
    import numpy as np
  File "C:\Python37-32\lib\site-packages\numpy\__init__.py", line 187, in <module>
    from .testing import Tester
  File "C:\Python37-32\lib\site-packages\numpy\testing\__init__.py", line 12, in <module>
    from ._private.utils import *
  File "C:\Python37-32\lib\site-packages\numpy\testing\_private\utils.py", line 16, in <module>
    from tempfile import mkdtemp, mkstemp
  File "C:\Python37-32\lib\tempfile.py", line 45, in <module>
    from random import Random as _Random
  File "e:\ML\random.py", line 4, in <module>
    from sklearn import datasets
  File "C:\Python37-32\lib\site-packages\sklearn\datasets\__init__.py", line 6, in <module>
    from .base import load_breast_cancer
  File "C:\Python37-32\lib\site-packages\sklearn\datasets\base.py", line 20, in <module>
    from ..utils import Bunch
  File "C:\Python37-32\lib\site-packages\sklearn\utils\__init__.py", line 10, in <module>
    from scipy.sparse import issparse
  File "C:\Python37-32\lib\site-packages\scipy\sparse\__init__.py", line 230, in <module>
    from .base import *
  File "C:\Python37-32\lib\site-packages\scipy\sparse\base.py", line 9, in <module>
    from scipy._lib._numpy_compat import broadcast_to
  File "C:\Python37-32\lib\site-packages\scipy\_lib\_numpy_compat.py", line 16, in <module>
    _assert_warns = np.testing.assert_warns
AttributeError: module 'numpy' has no attribute 'testing'

【问题讨论】:

  • 我们至少需要完整的回溯,最好是触发异常的代码。
  • 但是,最可能的错误原因是您创建了一个本地文件numpy.py 或包目录numpy/ 来屏蔽已安装的库。回溯将帮助我们确认这一点。
  • Remove are rename "e:\ML\random.py",它掩盖了内置的random模块。它在此处触发循环导入,这就是 numpy.testing 模块未完成导入的原因。
  • 哦,我的@MartijnPieters ....我今天早上确实添加了新文件...谁知道呢!现在可以了,谢谢!

标签: python numpy scikit-learn


【解决方案1】:

运行额外的导入代码,例如:

import np.testing as npt
npt.assert_array_almost_equal(answer1, answer2 )

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2018-10-22
    • 1970-01-01
    • 2017-08-18
    • 2018-09-30
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多