How do I find out if a numpy array contains integers?

后端 未结 5 1130
一个人的身影
一个人的身影 2021-02-03 18:46

I know there is a simple solution to this but can\'t seem to find it at the moment.

Given a numpy array, I need to know if the array contains integers.

Checking

相关标签:
5条回答
  • 2021-02-03 19:17

    While the accepted answer from 2009 is still valid, there is a new and enhanced solution as of Numpy v0.19, released in September 2014:

    All numerical numpy types are now registered with the type hierarchy in the python numbers module.

    This allows for checking the dtype against Python's Numeric abstract base classes.

    isinstance(np.dtype('int8'), numbers.Integral)
    issubclass(np.dtype('int32').type, numbers.Integral)
    

    You can test against numbers.Complex, numbers.Real and numbers.Integral.

    P.S. As you don't need to access .type anymore you can shorten your line by a few characters now. ;)

    0 讨论(0)
  • 2021-02-03 19:23

    Found it in the numpy book! Page 23:

    The other types in the hierarchy define particular categories of types. These categories can be useful for testing whether or not the object returned by self.dtype.type is of a particular class (using issubclass).

    issubclass(n.dtype('int8').type, n.integer)
    >>> True
    issubclass(n.dtype('int16').type, n.integer)
    >>> True
    
    0 讨论(0)
  • 2021-02-03 19:34

    Checking for an integer type does not work for floats that are integers, e.g. 4. Better solution is np.equal(np.mod(x, 1), 0), as in:

    >>> import numpy as np
    >>> def isinteger(x):
    ...     return np.equal(np.mod(x, 1), 0)
    ... 
    >>> foo = np.array([0., 1.5, 1.])
    >>> bar = np.array([-5,  1,  2,  3, -4, -2,  0,  1,  0,  0, -1,  1])
    >>> isinteger(foo)
    array([ True, False,  True], dtype=bool)
    >>> isinteger(bar)
    array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True], dtype=bool)
    >>> isinteger(1.5)
    False
    >>> isinteger(1.)
    True
    >>> isinteger(1)
    True
    
    0 讨论(0)
  • 2021-02-03 19:35

    This also works:

      n.dtype('int8').kind == 'i'
    
    0 讨论(0)
  • 2021-02-03 19:39

    Numpy's issubdtype() function can be used as follows:

    import numpy as np
    
    size=(3,3)
    A = np.random.randint(0, 255, size)
    B = np.random.random(size)
    
    print 'Array A:\n',  A
    print 'Integers:', np.issubdtype(A[0,0], int)
    print 'Floats:', np.issubdtype(A[0,0], float)
    
    print '\nArray B:\n',  B
    print 'Integers:', np.issubdtype(B[0,0], int)
    print 'Floats:', np.issubdtype(B[0,0], float)
    

    Results:

    Array A:
    [[  9 224  33]
     [210 117  83]
     [206 139  60]]
    Integers: True
    Floats: False
    
    Array B:
    [[ 0.54221849  0.96021118  0.72322367]
     [ 0.02207826  0.55162813  0.52167972]
     [ 0.74106348  0.72457807  0.9705301 ]]
    Integers: False
    Floats: True
    

    PS. Keep in mind that the elements of an array are always of the same datatype.

    0 讨论(0)
提交回复
热议问题