简体   繁体   中英

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

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 the dtype per-se is not enough, as there are multiple int dtypes (int8, int16, int32, int64 ...).

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

Checking for an integer type does not work for floats that are integers, eg 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

这也有效:

  n.dtype('int8').kind == 'i'

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.

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 .

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

If you are looking to determine if a dtype is integral, then you look at the type hierarchy as the other answers suggest. However, if you want to check floats that may contain integers, you can use (x % 1) == 0 , or the ufunc I recently wrote just for the purpose: https://github.com/madphysicist/isint_ufunc . After installing, you could run it with

from isint_ufunc import isint

isint(x)

It operates on integers and floating point types equally. Integers always return all True, while floating point numbers return a mask of integer values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM