我发誓我在问这个问题之前阅读了几乎所有的“FROM vs IMPORT”问题。
在阅读我使用的 NumPy 教程时:
import numpy as np
但是在声明矩阵的 dtype 时遇到了麻烦,例如:
a = np.ones((2,3),dtype=int32)
我一直收到“NameError: name 'int32' 未定义。”我正在使用 Python v3.2,并且正在遵循随附的暂定教程。我用了:
from numpy import *
a = ones((2,3),dtype=int32)
哪个有效。任何有关为什么会这样的见解将不胜感激。
先感谢您!
最佳答案
import numpy as np
#this will work because int32 is defined inside the numpy module
a = np.ones((2,3), dtype=np.int32)
#this also works
b = np.ones((2,3), dtype = 'int32')
#python doesn't know what int32 is because you loaded numpy as np
c = np.ones((2,3), dtype=int32)
回到你的例子:
from numpy import *
#this will now work because python knows what int32 is because it is loaded with numpy.
d = np.ones((2,3), dtype=int32)
我倾向于使用字符串定义类型,如数组 b
关于numpy - Dtype 在 FROM 但不是 IMPORT 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12980088/