【问题标题】:How do I check if a numpy dtype is integral?如何检查 numpy dtype 是否是整数?
【发布时间】:2014-04-23 16:16:32
【问题描述】:

如何检查 numpy dtype 是否是整数?我试过了:

issubclass(np.int64, numbers.Integral)

但它给出了False


更新:它现在提供True

【问题讨论】:

    标签: python numpy integer abc


    【解决方案1】:

    Numpy 具有类似于类层次结构的 dtype 层次结构(标量类型实际上具有反映 dtype 层次结构的真实类层次结构)。您可以使用np.issubdtype(some_dtype, np.integer) 测试数据类型是否为整数数据类型。请注意,与大多数使用 dtype 的函数一样,np.issubdtype() 会将其参数转换为 dtype,因此任何可以通过 np.dtype() 构造函数生成 dtype 的东西都可以使用。

    http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#specifying-and-constructing-data-types

    >>> import numpy as np
    >>> np.issubdtype(np.int32, np.integer)
    True
    >>> np.issubdtype(np.float32, np.integer)
    False
    >>> np.issubdtype(np.complex64, np.integer)
    False
    >>> np.issubdtype(np.uint8, np.integer)
    True
    >>> np.issubdtype(np.bool, np.integer)
    False
    >>> np.issubdtype(np.void, np.integer)
    False
    

    在 numpy 的未来版本中,我们将确保标量类型注册到适当的 numbers ABC。

    【讨论】:

    【解决方案2】:

    请注意,np.int64 不是 dtype,它是 Python 类型。如果你有一个实际的 dtype(通过数组的 dtype 字段访问),你可以使用你发现的 np.typecodes dict:

    my_array.dtype.char in np.typecodes['AllInteger']
    

    如果你只有np.int64这样的类型,可以先获取该类型对应的dtype,然后如上查询:

    >>> np.dtype(np.int64).char in np.typecodes['AllInteger']
    True
    

    【讨论】:

      【解决方案3】:

      在之前的答案和 cmets 的基础上,我决定将 dtype 对象的 type 属性与 Python 的内置 issubclass() 方法和 numbers 模块一起使用:

      import numbers
      import numpy
      
      assert issubclass(numpy.dtype('int32').type, numbers.Integral)
      assert not issubclass(numpy.dtype('float32').type, numbers.Integral)
      

      【讨论】:

        【解决方案4】:

        自从提出这个问题以来,NumPy 已经添加了使用numbers 的适当注册,所以这是可行的:

        issubclass(np.int64, numbers.Integral)
        issubclass(np.int64, numbers.Real)
        issubclass(np.int64, numbers.Complex)
        

        这比深入到更深奥的 NumPy 界面更优雅。

        要对 dtype 实例执行此检查,请使用其 .type 属性:

        issubclass(array.dtype.type, numbers.Integral)
        issubclass(array.dtype.type, numbers.Real)
        issubclass(array.dtype.type, numbers.Complex)
        

        【讨论】:

          【解决方案5】:

          根据用例进行鸭式打字

          import operator
          int = operator.index(number)
          

          在我看来是一个很好的方法。另外,它不需要任何特定的 numpy。

          唯一的缺点是在某些情况下你必须try/except它。

          【讨论】:

            【解决方案6】:

            你是指第 17 行吗?

            In [13]:
            
            import numpy as np
            A=np.array([1,2,3])
            In [14]:
            
            A.dtype
            Out[14]:
            dtype('int32')
            In [15]:
            
            isinstance(A, np.ndarray) #A is not an instance of int32, it is an instance of ndarray
            Out[15]:
            True
            In [16]:
            
            A.dtype==np.int32 #but its dtype is int32
            Out[16]:
            True
            In [17]:
            
            issubclass(np.int32, int) #and int32 is a subclass of int
            Out[17]:
            True
            

            【讨论】:

            • 当然,但不幸的是,这不适用于np.int64
            • 有趣,实际上它只适用于int_intcintpint32
            • 我猜你使用的是 Python 2?
            • (对我来说 intp 和 int_ 是 int64。)
            • 看起来它甚至依赖于平台/版本,我在 Win32 Python 2.7.6Numpy 1.8.1。文件提到intcintp可以是int32int64,我相信。
            猜你喜欢
            • 2017-11-14
            • 2015-06-13
            • 2012-07-02
            • 1970-01-01
            • 1970-01-01
            • 2020-06-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多