numpy - Dtype 在 FROM 但不是 IMPORT 中工作

标签 numpy python-3.x scipy

我发誓我在问这个问题之前阅读了几乎所有的“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/

相关文章:

python - 如何将两个列表中的所有值相乘并获得相应的矩阵

python - 返回数组的函数的 numpy.vectorize

python - 将 ctypes 类型数组转换为 void 指针时出现 Numpy 错误

python - 在 python3 中将文本输出到控制台的最快方法是什么

python-3.x - 如何在每个间隔时间调用一个函数 Odoo 11

python - 访问 Scipy ode 求解器的内部步骤

python - 如何快速将 '001100' 之类的字符串转换为 numpy.array([0,0,1,1,0,0])?

python - 正确索引字符串列表

python - Python 中的约束最小二乘估计

python - 如何在 Python 中进行指数和对数曲线拟合?我发现只有多项式拟合