问题描述
是否有一种方法可以指定与np.array(1.)
之类的结构一起使用的默认dtype?
Is there a way to specify default dtype that's used with constructs like np.array(1.)
?
尤其是我希望np.array(1.)
是np.float32
,而np.array(1)
是np.int32
.相反,我得到了np.float64
和np.int64
In particular I want np.array(1.)
to be np.float32
and np.array(1)
to be np.int32
. Instead I'm getting np.float64
and np.int64
推荐答案
默认值取决于您的系统.在64位系统上,默认类型为64位.在32位系统上,默认类型为32位.无法更改默认值,即使用其他系统C标头重新编译numpy.
The default depends on your system. On a 64-bit system, default types will be 64-bit. On a 32-bit system, default types will be 32-bit. There is no way to change the default short of re-compiling numpy with a different system C header.
您当然可以明确指定dtype,例如
You can of course specify dtypes explicitly, e.g.
>>> x = np.array(1, dtype='int32')
正如kazemakase在下面提到的那样,以上内容仅适用于int32/int64.在最新的numpy版本中,无论系统如何,浮点的默认值为float64.
as kazemakase mentions below, the above is only true for int32/int64. In recent numpy versions, the default for floating-point is float64 regardless of the system.
这篇关于为np.array(1.)指定默认dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!