如果我有一个numpy dtype,我如何自动将其转换为最接近的python数据类型?例如,
numpy.float32 -> "python float"
numpy.float64 -> "python float"
numpy.uint32 -> "python int"
numpy.int16 -> "python int"
我可以尝试提出所有这些情况的映射,但是numpy是否提供了一些自动的方法来将其dtypes转换为尽可能接近的本机python类型?这种映射不需要是详尽的,但它应该转换具有类似python的常见dtype。我想这已经在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'>
对于那些不需要自动转换并且知道值的numpy dtype的人来说,关于数组标量的一个旁注:
Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists and tuples). There are some exceptions, such as when code requires very specific attributes of a scalar or when it checks specifically whether a value is a Python scalar. Generally, problems are easily fixed by explicitly converting array scalars to Python scalars, using the corresponding Python type function (e.g., int, float, complex, str, unicode).
源
因此,在大多数情况下,可能根本不需要转换,可以直接使用数组标量。效果应该与使用Python scalar相同:
>>> np.issubdtype(np.int64, int)
True
>>> np.int64(0) == 0
True
>>> np.issubdtype(np.float64, float)
True
>>> np.float64(1.1) == 1.1
True
但是,如果出于某种原因,需要显式转换,则使用相应的Python内置函数是正确的方法。正如另一个答案所示,它也比数组标量item()方法快。
Tolist()是一种更通用的实现方法。它适用于任何基元dtype,也适用于数组或矩阵。
如果从基本类型调用I,实际上不会产生一个列表:
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的问题。float64只适用于常规Python浮点数。我看到了3种方法:
npValue.item ()
npValue.astype(浮动)
浮动(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)似乎更快。