【发布时间】:2017-01-24 01:05:35
【问题描述】:
你好!
我有两个代码块,一个可以工作,一个不能工作。唯一的区别是我不使用的 numpy 模块的注释代码行。为什么我从不引用“npm”时需要导入该模型?
此命令有效:
import numpy as np
import numpy.matlib as npm
V = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1
此命令不起作用:
import numpy as np
#import numpy.matlib as npm
V = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1
上面得到这个错误:
AttributeError: 'module' object has no attribute 'matlib'
提前致谢!
【问题讨论】:
-
“此命令不起作用” 不是有效的问题陈述。 如何它不起作用?它会给出不正确的结果吗?它有例外吗?错误消息的存在是有原因的:为您提供有关错误原因的线索。如果您排除它们,您只是浪费了仔细编写该错误消息的程序员以及我们的时间。
-
没问题,这里是错误信息:-------------------------------- ------------------------------ AttributeError Traceback(最近一次调用最后)
在 () 3 4 V = np.array([[1,2,3],[4,5,6],[7,8,9]]) - ---> 5 P1 = np.matlib.identity(V.shape[1], dtype=int) 6 P1 AttributeError: 'module' object has no attribute 'matlib' -
这是一个常见的成语。默认情况下,包不导入可选的子包。在使用它之前你必须这样做。
-
但您确实引用了
np.matlib,只是没有使用别名npm(可选) -
关于子包的 cmets 帮助我理解了为什么会出现错误。谢谢!