如果我有一个numpy dtype,我该如何自动将其转换为最接近的python数据类型?例如,
numpy.float32 -> "python float"
numpy.float64 -> "python float"
numpy.uint32 -> "python int"
numpy.int16 -> "python int"
我可以尝试提出所有这些情况的映射,但是numpy是否提供了一些自动方式将其dtypes转换为最接近的可能的本机python类型?这种映射不一定是详尽无遗的,但它应该转换具有close python模拟的常见dtypes。我认为这已经发生在numpy的某个地方了。
使用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'>
...
(另一种方法是np.asscalar(val)
,但是自NumPy 1.16以来它已被弃用)。
为了好奇,为您的系统构建一个NumPy array scalars转换表:
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
有些NumPy类型在某些系统上没有本机Python等价物,包括:clongdouble
,clongfloat
,complex192
,complex256
,float128
,longcomplex
,longdouble
和longfloat
。在使用.item()
之前,需要将它们转换为最接近的NumPy等效值。
我的方法有点强大,但似乎对所有情况都很好:
def type_np2py(dtype=None, arr=None):
'''Return the closest python type for a given numpy dtype'''
if ((dtype is None and arr is None) or
(dtype is not None and arr is not None)):
raise ValueError(
"Provide either keyword argument `dtype` or `arr`: a numpy dtype or a numpy array.")
if dtype is None:
dtype = arr.dtype
#1) Make a single-entry numpy array of the same dtype
#2) force the array into a python 'object' dtype
#3) the array entry should now be the closest python type
single_entry = np.empty([1], dtype=dtype).astype(object)
return type(single_entry[0])
用法:
>>> type_np2py(int)
<class 'int'>
>>> type_np2py(np.int)
<class 'int'>
>>> type_np2py(str)
<class 'str'>
>>> type_np2py(arr=np.array(['hello']))
<class 'str'>
>>> type_np2py(arr=np.array([1,2,3]))
<class 'int'>
>>> type_np2py(arr=np.array([1.,2.,3.]))
<class 'float'>
翻译整个ndarray而不是一个单元数据对象:
def trans(data):
"""
translate numpy.int/float into python native data type
"""
result = []
for i in data.index:
# i = data.index[0]
d0 = data.iloc[i].values
d = []
for j in d0:
if 'int' in str(type(j)):
res = j.item() if 'item' in dir(j) else j
elif 'float' in str(type(j)):
res = j.item() if 'item' in dir(j) else j
else:
res = j
d.append(res)
d = tuple(d)
result.append(d)
result = tuple(result)
return result
但是,处理大型数据帧需要几分钟时间。我也在寻找更有效的解决方案。希望有更好的答案。
发现自己有混合的numpy类型和标准python。因为所有numpy类型派生自numpy.generic
,这里是你如何将所有内容转换为python标准类型:
if isinstance(obj, numpy.generic):
return numpy.asscalar(obj)
怎么样:
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'>}
如果你想转换(numpy.array或numpy标量或原生类型OR numpy.ndarray)到本机类型,你可以简单地做:
converted_value = getattr(value, "tolist", lambda x=value: x)()
tolist会将您的标量或数组转换为python本机类型。默认的lambda函数负责处理值已经是本机的情况。
您也可以调用要转换的对象的item()
method:
>>> from numpy import float32, uint32
>>> type(float32(0).item())
<type 'float'>
>>> type(uint32(0).item())
<type 'long'>
我想你可以像这样编写通用类型转换函数:
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'>)
这意味着没有固定列表,您的代码将扩展为更多类型。
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]]
numpy将这些信息保存在typeDict
公开的映射中,因此您可以执行以下操作::
>>> import __builtin__
>>> import numpy as np
>>> {v: k for k, v in np.typeDict.items() if k in dir(__builtin__)}
{numpy.object_: 'object',
numpy.bool_: 'bool',
numpy.string_: 'str',
numpy.unicode_: 'unicode',
numpy.int64: 'int',
numpy.float64: 'float',
numpy.complex128: 'complex'}
如果你想要实际的python类型而不是它们的名字,你可以做::
>>> {v: getattr(__builtin__, k) for k, v in np.typeDict.items() if k in vars(__builtin__)}
{numpy.object_: object,
numpy.bool_: bool,
numpy.string_: str,
numpy.unicode_: unicode,
numpy.int64: int,
numpy.float64: float,
numpy.complex128: complex}
很抱歉迟到的部分,但我正在考虑将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
听起来像float(npValue)
似乎要快得多。