AttributeError:导入sklearn库时,模块“ numpy”没有属性“ testing”

时间:2019-05-15 04:09:44

标签: python numpy scikit-learn

我已经将numpy和sklearn库一起导入,但是出现错误  AttributeError: module 'numpy' has no attribute 'testing'

如果我从代码中删除了sklearn库,它可能会运行良好。

代码就像这样:

import numpy as np
from kumparanian import ds
from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer, CountVectorizer, HashingVectorizer

跟踪:

File "<ipython-input-37-76f2395d81c0>", line 1, in <module>
    runfile('C:/Users/LENOVO/Downloads/ds_assessment_v2/model.py', wdir='C:/Users/LENOVO/Downloads/ds_assessment_v2')

  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/LENOVO/Downloads/ds_assessment_v2/model.py", line 41, in <module>
    from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer, CountVectorizer, HashingVectorizer

  File "C:\Users\LENOVO\AppData\Roaming\Python\Python36\site-packages\sklearn\__init__.py", line 76, in <module>
    from .base import clone

  File "C:\Users\LENOVO\AppData\Roaming\Python\Python36\site-packages\sklearn\base.py", line 16, in <module>
    from .utils import _IS_32BIT

  File "C:\Users\LENOVO\AppData\Roaming\Python\Python36\site-packages\sklearn\utils\__init__.py", line 13, in <module>
    from scipy.sparse import issparse

  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\scipy\sparse\__init__.py", line 228, in <module>
    from .base import *

  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\scipy\sparse\base.py", line 9, in <module>
    from scipy._lib._numpy_compat import broadcast_to

  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\scipy\_lib\_numpy_compat.py", line 17, in <module>
    _assert_warns = np.testing.assert_warns
AttributeError: module 'numpy' has no attribute 'testing'

每个建议都非常感谢。

2 个答案:

答案 0 :(得分:0)

第一次,您可以通过测试以下代码来检查@是否存在问题:

numpy.training

应该返回:

import numpy as np
print(numpy.testing)

如果您遇到与以前相同的错误(<module 'numpy.testing' from '[Python_directory]\\lib\\site-packages\\numpy\\testing\\__init__.py'> ),则可能会尝试更新numpy,因为看起来好像缺少模块AttributeError: module 'numpy' has no attribute 'testing'

告诉我们该操作的结果,以便我们为您提供帮助。

答案 1 :(得分:0)

运行其他导入代码,例如:

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