【问题标题】:Converting numpy dtypes to native python types将 numpy dtypes 转换为原生 python 类型
【发布时间】:2012-03-16 04:52:40
【问题描述】:

如果我有一个 numpy dtype,我如何自动将其转换为最接近的 python 数据类型?例如,

numpy.float32 -> "python float"
numpy.float64 -> "python float"
numpy.uint32  -> "python int"
numpy.int16   -> "python int"

我可以尝试提出所有这些情况的映射,但是 numpy 是否提供了某种自动方式将其 dtype 转换为最接近的可能的本机 python 类型?这种映射不需要详尽,但它应该转换具有接近 python 模拟的常见 dtype。我认为这已经发生在 numpy 的某个地方。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    使用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 'int'>
    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 等效项,包括:clongdoubleclongfloatcomplex192complex256float128longcomplexlongdouble 和 @ 987654335@。在使用 .item() 之前,需要将它们转换为最接近的 NumPy 等效项。

    【讨论】:

    • 我正在使用熊猫 (0.23.0)。至少对于那个版本,np.str 没有 .item() 方法,所以我看到的唯一方法是将 .item() 包装在 try 块中。
    • @RobertLugg np.str 不是 Numpy 类型,即np.str is str,因此它只是标准 Python 类型的别名。与np.floatnp.intnp.boolnp.complexnp.object 相同。 Numpy 类型有一个尾随 _,例如np.str_.
    • 我明白了。所以问题是“如果”我能做到,那就太好了:np.float64(0).item()np.float(0).item()。换句话说,对于知道要做什么的情况,支持.item() 方法,即使它只是返回相同的值。这样我就可以在没有特殊大小写的情况下将.item() 应用于更多的 numpy 标量。事实上,看似平行的概念因底层实现而有所不同。我完全理解为什么要这样做。但这对图书馆用户来说很烦人。
    • item() 似乎是一个出乎意料的直观名称。有没有办法考虑一下,所以我失踪对我来说是有意义的?
    【解决方案2】:

    发现自己混合了一组 numpy 类型和标准 python。由于所有 numpy 类型都派生自 numpy.generic,因此您可以将所有内容都转换为 python 标准类型:

    if isinstance(obj, numpy.generic):
        return numpy.asscalar(obj)
    

    【讨论】:

    • 从 numpy v1.6 开始,asscalar 方法已经贬值
    • 您可以轻松地将答案替换为if isinstance(o, numpy.generic): return o.item() raise TypeError,然后它又会变成不推荐使用的答案:D
    【解决方案3】:

    如果你想将 (numpy.array OR numpy scalar OR native type OR numpy.darray) 转换为 native type,你可以简单地做:

    converted_value = getattr(value, "tolist", lambda: value)()
    

    tolist 会将您的标量或数组转换为 Python 原生类型。默认的 lambda 函数会处理 value 已经是原生的情况。

    【讨论】:

    • 混合类型(本地和非本地)的最干净的方法,干得好!对于那些想知道的人,是的,当您在单个值上调用它时,tolist 只返回一个值(标量),而不是您可能认为的列表。值得注意的是,编写 lambda 的更简单方法是 lambda: value,因为我们不需要任何输入。
    • getattr + tolist 组合不仅是通用的,甚至是矢量化的! (unlinke .item())
    • 这应该是一个可以接受的答案,它干净且适用于所有人
    【解决方案4】:

    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]]
    

    【讨论】:

    • 好建议! .tolist() 也适用于 np.float32
    【解决方案5】:

    怎么样:

    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 将来添加更多 dtypes,您的解决方案就会中断。所以我对这个解决方案不满意。
    • 可能的 dtype 的数量是无限的。对于任何正整数m,请考虑np.dtype('mint8')。不可能有详尽的映射。 (我也不相信有一个内置函数可以为你做这个转换。我可能是错的,但我不这么认为:))
    • Python 将 numpy dtypes 映射到 python 类型,我不确定如何,但我想使用他们所做的任何方法。我认为这必须允许,例如,numpy dtypes 和 python 类型之间的乘法(和其他操作)。我猜他们的方法并没有详尽地映射所有可能的 numpy 类型,但至少是最常见的有意义的类型。
    • 它无法始终如一地工作:&gt;&gt;&gt; print([numpy.asscalar(x) for x in numpy.linspace(1.0, 0.0, 21)]) [1.0, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.6499999999999999, 0.6, 0.55, 0.5, 0.44999999999999996, 0.3999999999999999, 0.35, 0.29999999999999993, 0.25, 0.19999999999999996, 0.1499999999999999, 0.09999999999999998, 0.04999999999999993, 0.0] 如您所见,并非所有值都已正确转换。
    • 在我之前的评论之后,奇怪的是这个工作,虽然我会虽然你需要把回合放在 Python 原生类型而不是 Numpy 原生类型上:&gt;&gt;&gt; print([numpy.asscalar(round(x,2)) for x in numpy.linspace(1.0, 0.0, 21)]) [1.0, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.65, 0.6, 0.55, 0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.0]
    【解决方案6】:

    您也可以调用要转换的对象的item() method

    >>> from numpy import float32, uint32
    >>> type(float32(0).item())
    <type 'float'>
    >>> type(uint32(0).item())
    <type 'long'>
    

    【讨论】:

      【解决方案7】:

      我认为您可以像这样编写通用类型转换函数:

      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'>)
      

      这意味着没有固定的列表,您的代码将随着更多类型进行扩展。

      【讨论】:

      • 您知道将 numpy 类型映射到 python 类型的 tolist() 方法部分的源代码在哪里吗?我快速浏览了一下,但没有找到。
      • 这有点像我正在做的是使用zeros() 生成一个带有1 个零的numpy.ndarray 并调用ndarrays tolist() 函数以转换为本机类型。一旦进入本机类型,我会要求返回类型。 tolist()ndarray 的函数
      • 是的,我明白了——它适用于我想要的,所以我接受了你的解决方案。但我想知道 tolist() 是如何决定要转换成什么类型​​的,我不知道如何找到源代码。
      • numpy.sourceforge.net/numdoc/HTML/numdoc.htm#pgfId-36588 是记录函数的位置。我认为检查可能能够帮助找到更多信息,但没有乐趣。下一步我尝试克隆github.com/numpy/numpy.git 并运行grep -r 'tolist' numpy。 (仍在进行中,numpy 非常庞大!)
      【解决方案8】:

      很抱歉来晚了,但我正在研究将numpy.float64 转换为常规 Python float 的问题。我看到了 3 种方法:

      1. npValue.item()
      2. npValue.astype(float)
      3. 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) 似乎要快得多。

      【讨论】:

        【解决方案9】:

        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}
        

        【讨论】:

          【解决方案10】:

          我的方法有点强硬,但似乎适用于所有情况:

          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'>
          

          【讨论】:

          • 我看到这与马特·阿尔科克的回答基本相同。
          【解决方案11】:

          对于那些不需要自动转换并且知道值的 numpy dtype 的人来说,关于数组标量的旁注:

          数组标量与 Python 标量不同,但在大多数情况下,它们可以互换使用(主要例外是 v2.x 之前的 Python 版本,其中整数数组标量不能用作索引对于列表和元组)。有一些例外,例如当代码需要非常特定的标量属性时,或者当它专门检查一个值是否是 Python 标量时。通常,通过使用相应的 Python 类型函数(例如,int、float、complex、str、unicode)将数组标量显式转换为 Python 标量,可以轻松解决问题。

          Source

          因此,在大多数情况下,可能根本不需要转换,可以直接使用数组标量。效果应该和使用 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() 方法更快。

          【讨论】:

            【解决方案12】:

            如果您有一个 numpy 类型的数组 list_numpy_numbers,请执行以下操作:

            list_native_numbers = [i.item() for i in list_numpy_numbers]
            

            【讨论】:

              【解决方案13】:

              翻译整个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
              

              但是,处理大型数据帧需要几分钟时间。我也在寻找更有效的解决方案。 希望有更好的答案。

              【讨论】:

                猜你喜欢
                • 2013-09-05
                • 2014-03-20
                • 2011-09-25
                • 2021-12-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-04-16
                相关资源
                最近更新 更多