Dtype在FROM中工作,但在IMPORT中工作

And*_* GS 5 numpy scipy python-3.x

我发誓,在问这个问题之前,我已经阅读了几乎所有“ FROM vs IMPORT”问题。

在阅读NumPy教程时,我在使用:

import numpy as np
Run Code Online (Sandbox Code Playgroud)

但是在声明矩阵的dtype时遇到麻烦:

a = np.ones((2,3),dtype=int32)
Run Code Online (Sandbox Code Playgroud)

我不断收到“ NameError:未定义名称'int32'”。我使用的是Python v3.2,并遵循其附带的实验性教程。我用了:

from numpy import *
a = ones((2,3),dtype=int32)
Run Code Online (Sandbox Code Playgroud)

哪个有效。对于为什么这样做的任何见解将不胜感激。先感谢您!

abd*_*q-e 5

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) 
Run Code Online (Sandbox Code Playgroud)

回到你的例子:

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) 
Run Code Online (Sandbox Code Playgroud)

我倾向于使用字符串定义数组b中的类型