确定numpy的array
中的数据点是否为整数的最简单方法是什么?目前,我使用numpy.dtype(x[i]).type
返回数组i
的元素x
的类型,然后
`if numpy.dtype(x[i]).type is numpy.int*`
实现此目标,其中*
可以是8
,32
或64
。但它也可能会返回uint
,因此if
方式可以返回False
。我想知道是否存在一种简单的方法来确定它是否是整数,而不管确切的int
类型是什么。浮动怎么样?
答案 0 :(得分:3)
您可以使用:
issubdtype(var, type)
用法:
numpy.issubdtype(var_to_check, np.integer)
此处提供更多信息How to determine if a number is any type of int (core or numpy, signed or not)?
答案 1 :(得分:2)
这可能有所帮助(这有效地测试有符号整数,类似fashion' u'将是无符号整数等):
x[i].dtype.kind == 'i'
答案 2 :(得分:0)
使用:
issubclass(x[i].dtype.type, np.integer)
或
np.issubdtype(x[i].dtype, np.integer)
np.dtype(x)
的代码不太可能按照您的要求执行 - 但不能获得x
的dtype,但会尝试将x解释为新dtype的描述。如果你想要一个非numpy对象,你可以使用np.asanyarray(x).dtype