[英]How do I check if a numpy dtype is integral?
How do I check if a numpy dtype is integral?如何检查 numpy dtype 是否完整? I tried:
我试过:
issubclass(np.int64, numbers.Integral)
but it gives False
.但它给出了
False
。
Update: it now gives True
.更新:它现在给出
True
。
Numpy has a hierarchy of dtypes similar to a class hierarchy (the scalar types actually have a bona fide class hierarchy that mirrors the dtype hierarchy). Numpy 有一个类似于类层次结构的 dtypes 层次结构(标量类型实际上有一个真正的类层次结构,它反映了 dtype 层次结构)。 You can use
np.issubdtype(some_dtype, np.integer)
to test if a dtype is an integer dtype.您可以使用
np.issubdtype(some_dtype, np.integer)
来测试np.issubdtype(some_dtype, np.integer)
是否为整数 dtype。 Note that like most dtype-consuming functions, np.issubdtype()
will convert its arguments to dtypes, so anything that can make a dtype via the np.dtype()
constructor can be used.请注意,与大多数使用
np.issubdtype()
函数一样, np.issubdtype()
会将其参数转换为 dtypes,因此可以使用任何可以通过np.dtype()
构造函数生成 dtype 的东西。
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#specifying-and-constructing-data-types 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
In a future version of numpy, we will make sure that the scalar types are registered with the appropriate numbers
ABCs.在 numpy 的未来版本中,我们将确保使用适当的
numbers
ABCs 注册标量类型。
Note that np.int64
is not a dtype, it's a Python type.请注意,
np.int64
不是np.int64
,而是 Python 类型。 If you have an actual dtype (accessed through the dtype
field of an array), you can make use of the np.typecodes
dict you discovered:如果你有一个实际的D型(通过访问
dtype
数组的字段),你可以利用的np.typecodes
快译通,你发现:
my_array.dtype.char in np.typecodes['AllInteger']
If you only have a type such as np.int64
, you can first obtain a dtype that corresponds to the type, then query it as above:如果只有
np.int64
这样的类型,可以先获取该类型对应的np.int64
,然后如上查询:
>>> np.dtype(np.int64).char in np.typecodes['AllInteger']
True
Building off previous answers and comments, I have settled on using the type
attribute of the dtype
object with Python's builtin issubclass()
method and the numbers
module:建立关以前的答案和评论,我已经定居在使用
type
的属性dtype
对象与Python的内置issubclass()
方法和numbers
模块:
import numbers
import numpy
assert issubclass(numpy.dtype('int32').type, numbers.Integral)
assert not issubclass(numpy.dtype('float32').type, numbers.Integral)
Since this question was asked, NumPy has added the appropriate registration with numbers
, so this works:由于这个问题被问到,NumPy 已经添加了适当的注册
numbers
,所以这有效:
issubclass(np.int64, numbers.Integral)
issubclass(np.int64, numbers.Real)
issubclass(np.int64, numbers.Complex)
This is more elegant than diving down to a more esoteric NumPy interface.这比深入研究更深奥的 NumPy 界面更优雅。
To perform this check on a dtype instance, use its .type
property:要对 dtype 实例执行此检查,请使用其
.type
属性:
issubclass(array.dtype.type, numbers.Integral)
issubclass(array.dtype.type, numbers.Real)
issubclass(array.dtype.type, numbers.Complex)
Depending on the use case the ducktyping根据用例,ducktyping
import operator
int = operator.index(number)
is a good method in my opinion.在我看来是一个很好的方法。 Plus it needs nothing numpy specific.
另外它不需要任何特定的numpy。
The only disadvantage is that in some cases you would have to try
/ except
it.唯一的缺点是在某些情况下您必须
try
/ except
。
Do you mean line 17?你的意思是第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
This is not a great answer, but one way to check if a class some_dtype
is an integral dtype (eg, np.int32
) is by calling it and casting it to string: 这不是一个很好的答案,但是检查类
some_dtype
是否为整数np.int32
(例如, np.int32
)的一种方法是通过调用它并将其转换为字符串:
str(dtype()) == '0'
Floating point dtypes give 0.0
instead. 浮点dtypes代替
0.0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.