我想知道是否有一种方法可以找出最大值,以便numpy
在运行时支持某些特定的数据,例如整数类型(或无符号整数,浮点数或复数-任何“固定大小”类型)。也就是说,假设(从文档中)我知道当前版本的numpy
is中最大的无符号整数类型,np.uint64
并且我有一行代码,例如:
y = np.uint64(x)
我希望我的代码使用我的代码使用的版本中可用numpy
的最大的无符号整数类型。也就是说,我有兴趣将上述硬编码类型替换为如下形式:
y = np.largest_uint_type(x)
有这种方法吗?
您可以使用np.sctypes
:
>>> def largest_of_kind(kind):
... return max(np.sctypes[kind], key=lambda x: np.dtype(x).itemsize)
...
>>> largest_of_kind('int')
<class 'numpy.int64'>
>>> largest_of_kind('uint')
<class 'numpy.uint64'>
>>> largest_of_kind('float')
<class 'numpy.float128'>
>>> largest_of_kind('complex')
<class 'numpy.complex256'>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句