如果我有一个numpy dtype,我该如何自动将其转换为最接近的python数据类型?例如,
numpy.float32 -> "python float"
numpy.float64 -> "python float"
numpy.uint32 -> "python int"
numpy.int16 -> "python int"
Run Code Online (Sandbox Code Playgroud)
我可以尝试提出所有这些情况的映射,但是numpy是否提供了一些自动方式将其dtypes转换为最接近的可能的本机python类型?这种映射不一定是详尽无遗的,但它应该转换具有close python模拟的常见dtypes.我认为这已经发生在numpy的某个地方了.
Mik*_*e T 273
使用val.item()最NumPy的值转换成原来的Python类型:
import numpy as np
# for example, numpy.float32 -> python float
val = np.float32(0)
pyval = val.item()
print(type(pyval)) # <class 'float'>
# and similar...
type(np.float64(0).item()) # <class 'float'>
type(np.uint32(0).item()) # <class 'long'>
type(np.int16(0).item()) # <class 'int'>
type(np.cfloat(0).item()) # <class 'complex'>
type(np.datetime64(0, 'D').item()) # <class 'datetime.date'>
type(np.datetime64('2001-01-01 00:00:00').item()) # <class 'datetime.datetime'>
type(np.timedelta64(0, 'D').item()) # <class 'datetime.timedelta'>
...
Run Code Online (Sandbox Code Playgroud)
(另一种方法是np.asscalar(val),自NumPy 1.16以来它已被弃用).
为了好奇,为您的系统构建一个NumPy数组标量转换表:
for name in dir(np):
obj = getattr(np, name)
if hasattr(obj, 'dtype'):
try:
if 'time' in name:
npn = obj(0, 'D')
else:
npn = obj(0)
nat = npn.item()
print('{0} ({1!r}) -> {2}'.format(name, npn.dtype.char, type(nat)))
except:
pass
Run Code Online (Sandbox Code Playgroud)
有迹象表明,有没有原生的Python相当于在某些系统上几个NumPy的类型,包括:clongdouble,clongfloat,complex192,complex256,float128,longcomplex,longdouble和longfloat.在使用之前,需要将它们转换为最接近的NumPy等效值.item().
tm_*_*_lv 39
发现自己有混合的numpy类型和标准python.因为所有numpy类型派生自numpy.generic,以下是如何将所有内容转换为python标准类型:
if isinstance(obj, numpy.generic):
return numpy.asscalar(obj)
Run Code Online (Sandbox Code Playgroud)
v.t*_*rey 11
如果你想转换(numpy.array或numpy标量或本机类型OR numpy.darray)到本机类型,你可以简单地做:
converted_value = getattr(value, "tolist", lambda: value)()
Run Code Online (Sandbox Code Playgroud)
tolist会将您的标量或数组转换为python本机类型.默认的lambda函数负责处理值已经是本机的情况.
unu*_*tbu 10
怎么样:
In [51]: dict([(d, type(np.zeros(1,d).tolist()[0])) for d in (np.float32,np.float64,np.uint32, np.int16)])
Out[51]:
{<type 'numpy.int16'>: <type 'int'>,
<type 'numpy.uint32'>: <type 'long'>,
<type 'numpy.float32'>: <type 'float'>,
<type 'numpy.float64'>: <type 'float'>}
Run Code Online (Sandbox Code Playgroud)
您还可以调用要转换的对象的item()方法:
>>> from numpy import float32, uint32
>>> type(float32(0).item())
<type 'float'>
>>> type(uint32(0).item())
<type 'long'>
Run Code Online (Sandbox Code Playgroud)
小智 7
tolist()是实现此目的的更通用的方法。它可以在任何原始dtype中工作,也可以在数组或矩阵中工作。
如果从原始类型调用,我实际上不会产生一个列表:
numpy的= = 1.15.2
>>> import numpy as np
>>> np_float = np.float64(1.23)
>>> print(type(np_float), np_float)
<class 'numpy.float64'> 1.23
>>> listed_np_float = np_float.tolist()
>>> print(type(listed_np_float), listed_np_float)
<class 'float'> 1.23
>>> np_array = np.array([[1,2,3.], [4,5,6.]])
>>> print(type(np_array), np_array)
<class 'numpy.ndarray'> [[1. 2. 3.]
[4. 5. 6.]]
>>> listed_np_array = np_array.tolist()
>>> print(type(listed_np_array), listed_np_array)
<class 'list'> [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
Run Code Online (Sandbox Code Playgroud)
很抱歉部分来晚了,但我正在考虑仅转换numpy.float64为常规 Python的问题float。我看到了 3 种方法:
npValue.item()npValue.astype(float)float(npValue)以下是来自 IPython 的相关时间:
In [1]: import numpy as np
In [2]: aa = np.random.uniform(0, 1, 1000000)
In [3]: %timeit map(float, aa)
10 loops, best of 3: 117 ms per loop
In [4]: %timeit map(lambda x: x.astype(float), aa)
1 loop, best of 3: 780 ms per loop
In [5]: %timeit map(lambda x: x.item(), aa)
1 loop, best of 3: 475 ms per loop
Run Code Online (Sandbox Code Playgroud)
听起来float(npValue)似乎要快得多。
我想你可以像这样编写通用类型转换函数:
import numpy as np
def get_type_convert(np_type):
convert_type = type(np.zeros(1,np_type).tolist()[0])
return (np_type, convert_type)
print get_type_convert(np.float32)
>> (<type 'numpy.float32'>, <type 'float'>)
print get_type_convert(np.float64)
>> (<type 'numpy.float64'>, <type 'float'>)
Run Code Online (Sandbox Code Playgroud)
这意味着没有固定列表,您的代码将扩展为更多类型.
numpy 将这些信息保存在公开的映射中,以便typeDict您可以执行如下操作:
>>> import __builtin__ as builtins # if python2
>>> import builtins # if python3
Run Code Online (Sandbox Code Playgroud)
然后::
>>> import numpy as np
>>> {v: k for k, v in np.typeDict.items() if k in dir(builtins)}
{numpy.object_: 'object',
numpy.bool_: 'bool',
numpy.string_: 'str',
numpy.unicode_: 'unicode',
numpy.int64: 'int',
numpy.float64: 'float',
numpy.complex128: 'complex'}
Run Code Online (Sandbox Code Playgroud)
如果你想要实际的 python 类型而不是它们的名称,你可以这样做::
>>> {v: getattr(builtins, k) for k, v in np.typeDict.items() if k in vars(builtins)}
{numpy.object_: object,
numpy.bool_: bool,
numpy.string_: str,
numpy.unicode_: unicode,
numpy.int64: int,
numpy.float64: float,
numpy.complex128: complex}
Run Code Online (Sandbox Code Playgroud)